liveBook · Manning

Template

As explained in Machine Learning Platform Engineering

A user prompt template is a parameterized structure for formatting user-provided content into a language-model request.

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

How Machine Learning Platform Engineering covers it

How deep it goes
Shows string interpolation, then versioned fetching, variable compilation, configuration, tracking, and fallback; stops short of detailed context optimization.
Examples
Uses a documentation question with retrieved context, a labeled Context/Question/Answer prompt, and a managed chat-template workflow.
What it assumes
Assumes a user query, retrieved documentation context, and an LLM; starts with a minimal interpolated prompt before managed deployment.
What you can do after
Apply it via Langfuse to compile runtime values, promote prompt versions, and track which version produced a response.
How it connects
Connects prompt templates to RAG, semantic retrieval, CI/CD, model configuration, and observability.

Also explained in

Compare how 21 books explain template →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Platform Engineering. The section links point to the parts of the book it draws on.