Prompt Templates & Few-Shot Formatting
Prompts as code, not strings
In any production LLM system, prompts stop being one-off strings and become versioned, parameterized templates — slots for variable content (user input, retrieved context, conversation history) embedded in a stable scaffold of instructions. Treating prompts this way enables the same engineering discipline applied to regular code: version control, diffing changes, A/B testing variants, and regression testing against an evaluation set before a template change ships.
Anatomy of a well-structured prompt template
A robust template typically separates distinct roles: a system/instruction block (stable across all calls, defines the task, persona, and constraints), a context block (variable, holds retrieved documents or relevant data), a few-shot examples block (optional, demonstrates the desired input-output pattern), and a user input slot (the actual variable request). Keeping these sections distinct — rather than one freeform paragraph — makes templates easier to maintain, and lets you reuse the stable parts across many different user queries while only substituting the variable parts.
TEMPLATE = """You are a support assistant for {product_name}.
Answer only using the context below. If the answer isn't in the context, say so.
Context:
{context}
Examples:
{few_shot_examples}
Question: {question}
Answer:"""
FEW_SHOT = """Q: How do I reset my password?
A: Go to Settings > Security > Reset Password, then follow the emailed link.
Q: What's the weather today?
A: I don't have information about that in the provided context."""
def build_prompt(product_name, context, question, few_shot=FEW_SHOT):
return TEMPLATE.format(
product_name=product_name,
context=context,
few_shot_examples=few_shot,
question=question,
)
prompt = build_prompt("Acme Cloud", context="Password reset: Settings > Security...", question="How do I reset my password?")Few-shot formatting: consistency matters more than quantity
Few-shot examples teach the model the desired output format and reasoning pattern by demonstration rather than instruction. What matters most is consistency: every example should follow the exact same structure (same field order, same delimiters, same level of detail) you want the model to reproduce, because the model is pattern-matching the shape of the examples as much as their content. Research on few-shot prompting has repeatedly found that the *format* of examples affects output quality as much as or more than how many examples you include — 2-3 well-formatted, representative examples routinely outperform 10 inconsistent ones.
Example selection and ordering both matter
Zero-shot vs few-shot vs dynamic few-shot
| Approach | Examples in prompt | Token cost | Best for |
|---|---|---|---|
| Zero-shot | None, instructions only | Lowest | Simple, well-understood tasks |
| Fixed few-shot | Same 2-5 examples every call | Moderate, constant | Stable task format, low input variance |
| Dynamic few-shot | Examples retrieved per-input by similarity | Moderate, variable | Diverse input types, higher-stakes accuracy needs |
Template injection and unescaped variables
What's Next?
Well-structured prompts and examples set the baseline for what a single call can do reliably — the next step up in reasoning quality comes from running that prompt multiple times and combining the results, rather than trusting any single generation.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.