Skip to content
AI Engineer Guide.

concept · concepts · intermediate · concept 8 of 26

Templates

0 of 26 complete

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

What a template costs on every renderInteractive

Rendered prompt · illustrative tokens

220 input+ 160 reserve= 380 of 700

320 free

Step 1/4 · The empty template

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.

reserve 160220 tokens

Output reserve

160
output reservefixedBar length is each block's token share of the window.

Pick 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.

  1. Define slots with names, types and whether they carry trusted or untrusted content.
  2. Write the template with explicit delimiters around every slot, so the boundary survives unusual input.
  3. Render in one function that escapes or fences untrusted values and fails on missing required slots.
  4. Keep conditionals to a minimum and move branching into code that chooses a template variant.
  5. Snapshot the rendered prompt in tests, so structural changes appear in the diff.
  6. Version the template and record the version with every request.

Trade-offs

ChoiceGood forCostFails when
Single template with slotsConsistent structure, easy reviewVariants become conditionalsTasks diverge significantly
Template per variantClear and testableDuplication across variantsShared rules change often
Logic-heavy templateFlexible per requestHard to test and reason aboutMore than a couple of branches
Inline string buildingNo abstractionEscaping and whitespace bugsAny untrusted input is involved
Trade-off mapInteractive

Good for

Consistent structure, easy review

Cost

Variants become conditionals

Fails when

Tasks diverge significantly

  1. Tasks diverge significantly
  2. Shared rules change often
  3. More than a couple of branches
  4. 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