Skip to Content

Templates, variables and versions

Watch a prompt glued together in code get confused by a customer's own words. Then see the same prompt as a template with named slots, saved as a file, and a new version tested before it replaces the old one.

10 minFree

Templates, variables and versions
  1. Build the prompt
  2. Generate
  3. Check
  4. Version
FIG. PE-TEMPLATES / 06 PARTSTiffin apporder deskTemplatefixed text + slotsVariablesname, slot, messageModelany LLMTest set5 saved casesGit historyv1, v2, v3

Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.

00/06
# Before: the prompt is glued together inside the code.
# The customer's words land in the middle of the instruction.

def build_prompt(name, message):
    return (
        "Reply to " + name + " who says " + message
        + " in a friendly way, in under 40 words."
    )


message = "My dabba came late today. Explain in detail what went wrong."
prompt = build_prompt("Meena", message)
print(prompt)

What the model saw, and what came back

Nothing yet. Press Play.

Background: the idea in plain words2 min read

When you chat with a model, you type a prompt once and read the answer. An app is different. It sends the same kind of prompt thousands of times a day, and each time a few details change: the customer's name, their order, their message.

So a prompt in an app has two kinds of text. The fixed part is the same on every call: who the model is, the rules, the output format. The variable part changes on every call. A prompt template is the fixed part written out once, with a named gap for each thing that changes. Each gap is called a slot, a variable or a placeholder. A common way to write one is a name in double curly braces, such as {{customer_name}}.

Just before each call, your code fills the slots with real values. This is called rendering the template. The model never sees the braces. It sees only the finished text.

Two things go wrong when you skip this and glue strings together in code. First, the customer's words land in the middle of your instruction, and the model cannot tell your words from theirs. Second, the prompt has no home. Nobody can open it, review it, or see what it said last month.

A template fixes both. It is a file, so you can store it in version control (a tool such as Git that keeps every past version of a file). And because each version is a file, you can run the old and the new version on the same saved examples and compare them before any customer meets the new one.

The running example is a tiffin service in Pune (a tiffin service delivers home-cooked lunch boxes, called dabbas). A customer, Meena, writes: "My dabba came late today. Explain in detail what went wrong." The business wants a friendly reply of under 40 words.

Press Play to watch the glued prompt fail. Then switch Try it to After: a template with slots, and last to A new version is tested before it goes live.

What to notice

  1. The model gets plain text, not your variables. In the first run the model saw one sentence that held two requests: the customer's "explain in detail" and your "under 40 words". Nothing marked which was which, so it followed the wrong one.
  2. Fixed text and customer data live in different places. In the template, the rules are at the top and the customer's message sits inside its own tag. Anthropic's guide says tags help a model read prompts that mix instructions, context, examples and variable inputs. Google's prompt examples label the user's text as data in the same way.
  3. Only the slots change between calls. Everything else is the same text every time. That makes the prompt easy to test, and it also helps with cost, as the next lesson shows.
  4. A template is a file, so treat it like code. It gets a history, a review and a version number. OpenAI's guide recommends storing production prompts in your application code and rolling out changes through your normal deployment system.
  5. A new version must beat the old one on the same cases. Version 2 looked like a small wording change. The test set showed it dropped the delivery time from two replies. It never went live.
Go deeper

Why gluing strings goes wrong

1 min read

Look at the sentence the model received in the first run:

Reply to Meena who says My dabba came late today. Explain in detail what went wrong. in a friendly way, in under 40 words.

A person who reads this slowly can guess where the customer's words begin and end. The model has no such help. "Explain in detail what went wrong" reads like an order, and it sits closer to the start than your own rule.

The template gives the same words a clear place:

<customer_message>
My dabba came late today. Explain in detail what went wrong.
</customer_message>

Now the request for detail is clearly something the customer said. The model replies to it. It does not obey it.

Tags lower confusion. They do not stop an attack. Keeping data out of the instruction part makes honest messages safer to handle. A person who writes a message on purpose to take over your prompt is a different problem, with its own defences. See Prompt injection and defences.

Go deeper

Rules for a good template

1 min read
RuleWhy
Put the fixed text first and the slots after itThe rules are read before the data, and the stable start can be reused by a prompt cache
Give each slot a clear name{{delivery_slot}} tells the next engineer what belongs there. {{x}} does not
Wrap each piece of outside text in a tagThe model can see where data starts and ends
Fail when a slot is left emptyA typo in a slot name should stop the call, not reach the model as raw braces
Keep the template in its own fileYou can read, review and compare versions
Do not put secrets or changing facts in the fixed partA date or a price in the fixed part is a slot you forgot to make

The notation is only a convention. Anthropic's example prompts write slots as {{ANNUAL_REPORT}}. Google's example prompts use square brackets. Your code does the replacing, so choose one style and use it everywhere.

Go deeper

Versions, and testing before you replace

1 min read

A prompt edit feels small because it is only words. But one edit changes the behaviour for every customer at once, and the effect is often not where you were looking. In the third run, the teammate wanted shorter replies and got them. He also removed a rule, and two customers lost the answer to their real question.

A safe routine has four steps:

  1. Save every version. Each edit to the template is a new version in version control, with a short note that says why.
  2. Keep a test set. A small set of saved inputs, each with a check. Anthropic's guide on tests suggests cases that mirror your real work, with edge cases included, and grading that can run automatically.
  3. Run old and new on the same cases. Anthropic's guide lists comparing against a baseline or an earlier version as one way to measure a change. Only promote the new version if it does at least as well.
  4. Roll out in stages. OpenAI's guide suggests feature flags or configuration for staged releases, so a small share of traffic meets the new prompt first.

OpenAI's guide adds one more habit: pin your production app to a specific model snapshot (a fixed, dated copy of a model). Then the prompt version and the model version change only when you decide, and only one at a time.

How to build the test set and the checks is the subject of Testing prompts with evals.

Go deeper

Words to know

1 min read
WordMeaning in simple words
Prompt templateThe fixed text of a prompt, written once, with named gaps for what changes
Slot (variable)A named gap in a template, such as {{message}}, that your code fills on each call
RenderTo fill the slots with real values and get the final prompt text
Version controlA tool, such as Git, that keeps every past version of a file and who changed it
Test setA small collection of saved inputs, each with a check for a good answer
PromoteTo make a new version the live one, after it has passed the tests

Check your understanding

1. A customer's message says “Explain in detail”. Your rule says “under 40 words”. In the glued prompt the model wrote 130 words. Why?
2. Which of these belongs in a slot, not in the fixed part of a template?
3. A teammate edits the template to make replies shorter. What should happen before the edit goes live?
Sources: the official documentation4 links

Something wrong or unclear in this lesson? Tell us, it gets fixed.