Python / Python Modern Generative AI and Agents Interview Questions
How do you manage, version, and reuse prompts in production LLM applications?
In production systems, prompts are first-class assets — they evolve through experimentation, need version control, and may be shared across teams. Hard-coding prompts in application code makes them difficult to update without deployment. Several strategies improve prompt management.
# ââ Approach 1: LangChain Hub (versioned, shareable prompt registry) from langchain import hub # Pull a community prompt by handle (owner/prompt-name:commit-hash) rag_prompt = hub.pull('rlm/rag-prompt') print(rag_prompt.messages[0].prompt.template) # ââ Approach 2: PromptTemplate with variables from langchain_core.prompts import PromptTemplate, ChatPromptTemplate from langchain_core.prompts import FewShotChatMessagePromptTemplate # Parameterised template qa_template = PromptTemplate.from_template( 'You are an expert in {domain}. Answer the following question concisely.\n\n' 'Question: {question}\n' 'Answer:' ) formatted = qa_template.format(domain='astrophysics', question='What is a black hole?') # ââ Few-shot template examples = [ {'input': 'happy', 'output': 'sad'}, {'input': 'tall', 'output': 'short'}, {'input': 'energetic','output': 'lethargic'}, ] example_prompt = ChatPromptTemplate.from_messages([ ('human', '{input}'), ('ai', '{output}'), ]) few_shot_prompt = FewShotChatMessagePromptTemplate( example_prompt=example_prompt, examples=examples, ) final_prompt = ChatPromptTemplate.from_messages([ ('system', 'Give the antonym of each word.'), few_shot_prompt, ('human', '{word}'), ]) print(final_prompt.invoke({'word': 'joyful'}).to_messages()) # ââ Approach 3: LangSmith for prompt tracing and experimentation # Set env vars: LANGCHAIN_API_KEY, LANGCHAIN_TRACING_V2=true # Every chain invocation is automatically logged to LangSmith dashboard # enabling side-by-side comparison of prompt versions
More Related questions...