# Why Template and Version Your Prompts?

> Why keep prompts as templates in code with version ids, what goes wrong without them, and how to explain rollback and caching for your AI interview.

**In short:** A template keeps the fixed wording of a prompt in one place, with blanks for the changing parts. Versioning records each change, so you can test it, review it, and roll back when an update breaks a reply.

AI and LLM interview question Q087, Prompt and context engineering. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/why-template-and-version-your-prompts

## Say it in 30 seconds

A prompt template keeps the fixed wording of a prompt in one file, with blanks for the things that change, like the customer's data. Versioning saves each edit as a version with its own id, so I can test a change, review it, and roll back to the last good version when a reply goes wrong. Keeping the fixed text identical also helps caching, which keeps cost down. Without both, copies drift apart and nobody knows which wording gave the bad answer.

## Say it in 2 minutes

Most AI apps send the same instructions thousands of times a day, with different data each time. If those instructions are typed into several places, they drift apart. One service gets a fix, two services don't, and nobody knows which wording gave a bad answer. So I keep the fixed text in one template file, with named blanks such as the customer's account and the policy. Typed values fill the blanks, and a missing value stops the call instead of sending half a prompt.

Versioning is the other half. Every change to that file goes through a commit, a review and a test run. Each release gets a version id that goes into the log, so when a bank's support bot quotes an old card fee, I can see which wording ran, re-run the saved test cases, and roll back to the last version that passed. Before switching, I can also compare two versions on the same cases.

Two details matter for cost and quality. Put the fixed text first and the changing data last, because caching only reuses an identical start of a prompt. And record the model version next to the prompt version, because a new model can change the answers even when the prompt stays the same.

## What is a prompt template and a prompt version, in plain words?

A prompt is the text you send to a model: the rules, the context and the question. In a real app the rules barely change, but the question and the data do. A template keeps the rules in one place and leaves a blank for each thing that changes.

A bank uses a standard letter for declined card payments. The legal wording stays fixed, and the customer's name, the amount and the date go into the blanks. Nobody retypes the letter for each customer, and the bank can change the wording once, with a record of what it said before.

An AI version works the same way. A support assistant has fixed rules, and its blanks hold the customer's account, the current policy and the customer's message. A version is a saved copy of that template with its own id, so every reply can be traced back to the exact wording that produced it.

- **Template:** Fixed prompt text with named blanks.
- **Variable:** A blank that gets a real value on each call.
- **Prompt version:** One saved state of the template, with its own id.
- **Regression test:** A fixed set of past cases, run again after each change.
- **Rollback:** Switching back to an older version that worked.

## Why not just type the prompt into each place?

Typed copies look harmless at first. A claims triage agent might have one prompt in the intake service, another in the refunds service and a third in the call-back bot. All three start the same. A month later, one copy has a fix for ambiguous policy numbers, and the other two still send the old wording.

*Diagram, Copied prompts drift, one shared template does not: Copied prompts drift apart, so a fix lands in one service and the others keep the old wording. One shared template reaches every caller with a single edit.*
1. Each service pastes its own copy, so the same fix has to be made three times.
2. Every service imports one file, so one edit reaches all three.

A template also makes the changing values explicit. The values become typed function arguments in your code, so you can check them before the model sees them: a claim number must be a string, and a policy code must be on the known list. A missing blank raises an error, as the sketch below shows. OpenAI's own migration guide makes the same move, replacing prompt variables with typed function arguments.

> **Tip:** Asked why not just copy the prompt? Say copies drift apart, and that one template with typed inputs keeps every caller on the same wording.

## How does versioning make a bad release easy to undo?

A version is a saved state of the template with its own id. Keep it in the code repository, so each edit is a commit (a saved, named change in the code's history) with an author, a date and a reason, and each release goes through a pull request (a proposed change that a colleague reviews before it is merged). OpenAI's migration guide says to move versioning to your repo, using commits, review, and tests or evals.

> "Move prompt content into source code so prompt changes go through the same review and release process as product logic." (OpenAI)
>
> "Prompt templates can be versioned and used with generative models on Agent Platform." (Google Cloud)

*Diagram, Finding and undoing a bad prompt release: When a reply goes wrong, the version id in the log shows which wording ran. The team re-runs the saved cases, then rolls back to the last version that passed.*
1. A customer complaint starts the search.
2. The version id in the reply's log shows exactly which wording produced the answer.
3. The saved cases confirm the fault, and the team switches back to the last version that passed.

1. **Every change is a commit:** Each edit is saved with who made it, when, and why.
2. **Each release gets an id:** The id goes into the log of every reply, so a bad answer can be traced.
3. **Tests run before release:** A fixed set of past cases must still pass, and a new failure becomes a new case.
4. **Rollback is a switch back:** Point the app at the last version that passed. Nothing is retyped.

Here is the idea in a short Python sketch. A missing blank stops the call, and the version id is a hash of the wording, so the same text always gets the same id.

```python
# A versioned prompt for a claims triage agent. Standard library only.
import hashlib
from string import Template

TRIAGE_V3 = Template(
    "You triage insurance claims. Answer fast-track, review or decline.\n"
    "Policy: $policy\n"
    "Claim: $claim"
)

def version_id(template):
    # The same wording always gives the same id, so a log shows exactly which text ran.
    return "triage-" + hashlib.sha256(template.template.encode()).hexdigest()[:8]

def render(template, **values):
    # substitute() raises KeyError on a missing blank, so half a prompt never goes out.
    return template.substitute(**values)

print("version:", version_id(TRIAGE_V3))
print(render(TRIAGE_V3, policy="motor-comprehensive", claim="Rear bumper, parked car, 1,240 USD"))
try:
    render(TRIAGE_V3, policy="motor-comprehensive")
except KeyError as err:
    print("blocked, missing blank:", err)
```

Output:

```text
version: triage-92cb6a1f
You triage insurance claims. Answer fast-track, review or decline.
Policy: motor-comprehensive
Claim: Rear bumper, parked car, 1,240 USD
blocked, missing blank: 'claim'
```

## How do caching and model changes affect a template?

Caching is a cost saver. Tokens are the small pieces of text a model reads, roughly part of a word each. When a prompt starts with the same tokens as an earlier request, the model can reuse work it already did for that start. OpenAI's caching guide says reused input tokens get a reduced rate, and that the reuse covers the unchanged tokens at the beginning of a prompt.

> "Keep static content first and dynamic content later to preserve prompt caching benefits, since cache hits depend on exact prefix matches." (OpenAI)

So the order inside a template matters. Fixed rules and examples go first, and the customer's data goes last. A template that puts a timestamp or a random example at the top can break the cache on every call, and every call pays the full price for the prompt again.

A prompt version alone does not pin behaviour. OpenAI advises pinning production apps to specific model snapshots (fixed, dated builds of a model) to ensure consistent behaviour, and notes that even different snapshots within one model family could produce different results. So the release record should name the prompt version and the model snapshot together. Rolling back the prompt does not undo a model change.

> **Tip:** Asked how you would handle a model upgrade? Say you run the saved test cases on the new model before switching, and keep the old prompt and model pair ready to restore.

### How would you change a prompt in a live claims app?

A claims team wants a new rule for ambiguous policy numbers. The change is small, but a wrong triage sends a valid claim to decline.

- **Without versions: Edit and hope.** Someone edits the live prompt, and the complaints show up a week later with no record of what changed.
- **With versions: Test, then release.** The change is a commit, the saved cases must still pass, and the release gets an id. If complaints rise, the team rolls back to the last good id.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A template: fixed rules in one place, typed blanks for the changing data.
- [ ] A version: a saved state with its own id, shown in every reply's log.
- [ ] Why it matters: copies drift apart, and nobody knows which wording failed.
- [ ] Safe change: commits, a review, and regression tests before release.
- [ ] Rollback: switch back to the last version that passed.
- [ ] The honest point: a prompt version does not pin the model, so record both.

**Common traps**

- Trap: "Every team can keep its own copy of the prompt." Better: Copies drift apart. Keep one template that every caller imports.
- Trap: "A backup of the file is the same as versioning." Better: A backup has no reason, no test and no id in the log. Use commits, reviews and version ids.
- Trap: "Just edit the live prompt and watch the complaints." Better: Run the saved cases on the change first, then release it under an id you can roll back.

## Can you spot the safe way to call a prompt?

**Spot the safe call:** Which option is a safe way to send a claims triage prompt?

A. prompt = 'You triage claims. Claim: ' + claim_text
B. Each service pastes its own copy of the rules into its code.
C. render(TRIAGE_V3, policy=policy, claim=claim) and log the version id
D. Edit the live prompt string on the server, then check the complaints.

**Answer:** C. The call fills named blanks with typed values, and the log records which version ran. The other three glue strings together, copy the rules, or change a live prompt with no record.

**Quick check:** A bank's support bot starts quoting an old card fee after a prompt edit. What do you check first?

A. Edit the prompt in place and hope the complaints stop
B. Find the version id in the log, then re-run the saved cases
C. Switch to a bigger model
D. Add more examples to the prompt

**Answer:** B. The version id shows which wording ran, and the saved cases show whether it fails. Length, model size and examples change the wording again without showing the cause.

## Can you answer these from memory?

**Flash cards**

- **What is a prompt template?** Fixed prompt text with named blanks that get real values on each call.
- **What is a prompt version?** A saved state of the template with its own id, shown in each reply's log.
- **Why keep prompts in code?** So changes go through review, tests and the normal release process.
- **Why put fixed text first?** Caching reuses only an identical start of the prompt, so fixed text goes first.
- **Does a prompt version pin the model?** No. Record the model snapshot next to the prompt version too.
- **What makes a rollback safe?** Keeping the last version that passed its saved cases ready to restore.

## Likely follow-up questions

- Q088 What is prompt caching, and how does it save money? https://designervenkat.online/interview/ai/what-is-prompt-caching
- Q089 How do you order a prompt so caching hits? https://designervenkat.online/interview/ai/how-do-you-order-a-prompt-so-caching-hits
- Q100 How do you test prompts systematically? https://designervenkat.online/interview/ai/how-to-test-prompts-systematically
- Q102 Why do prompts break when you switch models? https://designervenkat.online/interview/ai/why-do-prompts-break-when-you-switch-models
- Q257 How do you regression-test prompts? https://designervenkat.online/interview/ai/how-to-regression-test-prompts
