Why it matters
Prompts start as inline strings and quickly acquire conditionals, loops over retrieved chunks and per-customer variations. String concatenation hides structure, makes whitespace bugs common, and lets user input break the format. A template with declared slots and one renderer gives a single place to inspect the final prompt, escape variables and attach a version, which is what makes prompt changes reviewable.
Mental model
A template is a printed form with blank fields, filled by a clerk. The metaphor breaks because the clerk's handwriting can contain a new form, and the model will read it as part of the document. Nothing on the page marks which marks were authorised.
How it works
Rendered prompt · illustrative tokens
220 input+ 160 reserve= 380 of 700
320 free
Step 1The empty template
With no values rendered, the template is already 220 tokens: 160 of literal text and 60 of delimiters. That is the floor this template pays on every request, before any evidence or question is added.
Output reserve
160Pick a section to read its token count, its role and its state.
Figure. A template is charged twice: its fixed text and per-slot delimiters are sent on every render, and so are the values. The rendered prompt — not the template file — is what the token budget sees.
What to notice. Counts are authored for a 700-token window with a 160-token answer reserve; no template engine and no model were run. The template is drawn as six line items, so repeated per-message formatting and the model's own chat markers are omitted. The empty-slot step describes a failure mode, not a measured prompt.
Text version
A 700-token window with a 160-token answer reserve, so 540 tokens are available. The default first step is an empty template: 160 tokens of literal text and 60 tokens of delimiters around the slots, for 220 input tokens and 320 free. The second step fills four slots — system 70, question 40, retrieved document 180 and worked example 90 — so the rendered prompt is 600 tokens and, with the reserve, runs 60 over the window. A pill says over by 60 and the cells cross the cap rule. The third step highlights the literal text and the delimiters: 220 of the 600 rendered tokens are template, charged on every render. The fourth step removes the required example slot: the prompt now reads 510 tokens and fits with 30 free, even though the demonstration it needed rendered as an empty string. Selecting a section shows its token count, kind and role.
- Define slots with names, types and whether they carry trusted or untrusted content.
- Write the template with explicit delimiters around every slot, so the boundary survives unusual input.
- Render in one function that escapes or fences untrusted values and fails on missing required slots.
- Keep conditionals to a minimum and move branching into code that chooses a template variant.
- Snapshot the rendered prompt in tests, so structural changes appear in the diff.
- Version the template and record the version with every request.
Trade-offs
| Choice | Good for | Cost | Fails when |
|---|---|---|---|
| Single template with slots | Consistent structure, easy review | Variants become conditionals | Tasks diverge significantly |
| Template per variant | Clear and testable | Duplication across variants | Shared rules change often |
| Logic-heavy template | Flexible per request | Hard to test and reason about | More than a couple of branches |
| Inline string building | No abstraction | Escaping and whitespace bugs | Any untrusted input is involved |
Good for
Consistent structure, easy review
Cost
Variants become conditionals
Fails when
Tasks diverge significantly
- Tasks diverge significantly
- Shared rules change often
- More than a couple of branches
- Any untrusted input is involved
Figure. Each choice with what it buys, what it costs, and the case where it loses.
What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.
Text version
Four trade-offs: Single template with slots is good for Consistent structure, easy review, costs Variants become conditionals, and fails when Tasks diverge significantly. Template per variant is good for Clear and testable, costs Duplication across variants, and fails when Shared rules change often. Logic-heavy template is good for Flexible per request, costs Hard to test and reason about, and fails when More than a couple of branches. Inline string building is good for No abstraction, costs Escaping and whitespace bugs, and fails when Any untrusted input is involved
Failure modes
- Delimiter injection. User text closes the data block and adds instructions. Debug: render input containing the delimiter and inspect where the boundary lands.
- Silent slot omission. A missing variable renders as an empty string and the model answers without the evidence. Debug: make required slots fail loudly and assert rendered length.
- Template drift. Two services render the same prompt differently after one is updated. Debug: share one renderer and compare rendered hashes across services.
In production
Treat the template as a deployable artifact: version it, test the rendered output and log the version per request. Alert when rendered prompts exceed the token budget.
Common misconceptions
People think: Templates are just string formatting.
Actually: They are the boundary between trusted structure and untrusted values, so escaping is a security concern.
People think: A template makes the prompt model-agnostic.
Actually: Structure travels, but role markers and formatting expectations still depend on the model.
Interview angle
Describe slots, escaping and a single renderer, then explain why the rendered prompt is the artifact you test. Strong answers mention versioning and untrusted values. Weak answers describe a string with blank slots and never consider what happens when a value contains the delimiter.
Sources
- Jinja, Template Designer Documentation — templating, escaping and autoescape semantics.
- Anthropic, Use XML tags to structure prompts — delimiters that separate instructions from data.
- Schulhoff et al., The Prompt Report — prompt structure and templating in practice.