A user prompt template is a reusable structure that inserts runtime values into an LLM request. In retrieval-augmented generation (RAG), those values commonly include retrieved documentation and the user’s question. Labeling Context, Question, and Answer helps distinguish retrieved facts from the request and guides the response format; it reduces, but does not eliminate, hallucination. section 12.2.6
Listing 12.12 Prompt template combining context and query
prompt = f"""
Based on the following DataKrypt documentation, answer the question:
Context: {context} #1
Question: {query} #2
Answer:""" #3
response = llm.invoke(prompt) #4
print(response.content) #5
The minimal implementation interpolates context and query into labeled fields, then sends the resulting string to the LLM. It demonstrates the basic RAG pattern before adding production controls. section 12.2.6
For a production application, treat the template as a deployable artifact rather than a hardcoded string. LLM deployment metadata may also include system prompts, few-shot examples, reasoning templates, model settings, and retrieval configuration. Existing deployment automation can carry prompt templates alongside model files and vector indexes. section 12.1.2
A managed prompt workflow separates editing from request handling:
- Store a versioned chat template with variables such as {{context}} and {{query}}, plus its model configuration. section 13.1.2
- Fetch the version labeled production at request time, apply its configuration, and compile it with retrieved context and the current query. section 13.1.2
- Record the prompt name and version with the LLM invocation so response quality, satisfaction, and cost can be compared across versions. section 13.1.2
Listing 13.6 Template compilation and tracked response generation
class DakkaBotCore:
async def process_query(self, query: str) -> Dict[str, Any]:
try:
docs = await self.retrieve_documents(query)
context = self.format_context(docs)
prompt_obj = self.langfuse.get_prompt(
"dakkabot-system",
type="chat",
label="production"
)
prompt_config = prompt_obj.config
self.llm.model = prompt_config.get("model", "gemini-2.5-flash")
self.llm.temperature = prompt_config.get("temperature", 0.0)
compiled_messages = prompt_obj.compile( #1
context=context, query=query) #1
metadata = {
"prompt_name": "dakkabot-system",
"prompt_version": prompt_obj.version #2
}
response = self.llm.invoke(
compiled_messages,
config={
"callbacks": [self.langfuse_handler],
"metadata": metadata
}
)
return {
"response": response.content,
"sources": docs,
"prompt_version": prompt_obj.version
}
The Langfuse integration shows the production path: retrieve and format documents, fetch the labeled prompt, apply model settings, compile runtime values, and attach version metadata. section 13.1.2
Compilation substitutes named variables instead of assembling the managed prompt through string concatenation. A production path also needs failure handling: if the primary request fails, a separate boundary template can return a helpful support direction. Changing a prompt’s production label allows a new version to take effect without an application restart. section 13.1.2




















