Prompt Templates
Prompt templates are reusable prompt structures with variable placeholders filled in at runtime.
What Is a Prompt Template?
Hardcoding a slightly different prompt for every request doesn't scale. A prompt template is a reusable prompt structure with placeholders — for the user's question, retrieved context, conversation history — filled in with real values at request time. The template stays fixed and tested; only the placeholder values change per request.
Prompt Template
Fixed and TestedReused as-is across many requests.
Fill Placeholders (context, question, history)
Per-Request ValuesOnly the placeholder values change.
Final Prompt
Ready to SendThe template plus this request's real values.
Model
Consistent StructureSees the same structure every time.
Code Example
RAG_TEMPLATE = """You are a helpful assistant. Answer the question using
only the context below. If the answer isn't in the context, say you don't know.
Context:
{context}
Question:
{question}
"""
prompt = RAG_TEMPLATE.format(context=retrieved_context, question=user_question)Why Templates Matter in Production
- Consistency — every request goes through the same tested structure instead of ad-hoc string concatenation scattered through the codebase.
- Versioning — a template can be tracked, tested, and improved over time as a single artifact, rather than prompt logic being duplicated across the application.
- Separation of concerns — application code focuses on gathering the right values (retrieved context, history); the template focuses on how to present them to the model.
- Easier evaluation — since the template structure is fixed, evaluation can focus on whether specific input values produce good outputs, isolating the variable that actually changed.
Warning
Values inserted into a template — especially retrieved content or user input — should be treated the same way as any other untrusted content injected into a prompt.
Common Mistakes
Building prompts with ad-hoc string concatenation everywhere
Without a shared template, prompt logic gets duplicated and drifts inconsistently across the codebase.
Not versioning or testing prompt templates
A template that changes without any tracking or evaluation can silently regress output quality.
Assuming placeholder values are automatically safe
Content filled into a template — like retrieved documents or user input — still needs the same trust-boundary treatment as any other prompt content.
Making templates overly generic to cover every possible case
An overly flexible, catch-all template is often harder to reason about and test than a few focused templates for distinct use cases.
Interview Question
What is a prompt template, and why use one instead of building prompts ad hoc in code?
A prompt template is a reusable, fixed prompt structure with placeholders for values like the user's question, retrieved context, or conversation history, filled in at request time. Using one instead of ad-hoc string concatenation gives you consistency across requests, a single artifact you can version and evaluate over time, and a clean separation between application code, which gathers the right values, and the template, which defines how those values are presented to the model. It's not automatically safe, though — values inserted into a template, especially retrieved or user-provided content, still need the same untrusted-content handling as any other prompt input.
What an interviewer may ask next
- Why does versioning a prompt template matter for evaluation?
- Are values filled into a template automatically safe from prompt injection?
- When might having several focused templates be better than one flexible, catch-all template?
Explain It in 30 Seconds
A prompt template is a reusable, fixed prompt structure with placeholders for values like the user's question or retrieved context, filled in at request time. It gives you consistency across requests, a single artifact you can version and evaluate, and a clean separation between gathering the right values and defining how the model sees them. Values inserted into a template still need to be treated as untrusted content, the same as any other prompt input.