Sub topic 13
Version and maintain prompts over time
A system prompt is part of your application's behavior, so it needs the same care as code. This chapter assumes you can read Python and have built a small test set, as in the evaluation chapter. It covers how to store, change, release, and retire prompts without surprises.
Treat the prompt as a versioned artifact
Teams often start by pasting a prompt into a string inside a function. That works until someone edits it to fix one complaint and quietly breaks three other behaviors. You then can't tell which prompt produced a bad reply last Tuesday.
Three habits prevent this:
- Keep each prompt in its own file under source control, not inside application code.
- Give every released prompt a version identifier.
- Record that identifier with every model call.
A model upgrade can change behavior even when the prompt text stays the same. Record the model name and sampling settings next to the prompt version.
Store and load prompts
A simple layout is one directory per prompt and one file per version:
prompts/support/v1.txt
prompts/support/v2.txt
prompts/support/v3.txtThe loader below reads a pinned version and computes a short hash of the text. The hash catches the case where someone edits a released file without changing its version.
import hashlib
from pathlib import Path
PROMPT_DIR = Path("prompts/support")
def load_prompt(version: str) -> tuple[str, str]:
"""Return the prompt text and a short content hash."""
text = (PROMPT_DIR / f"{version}.txt").read_text(encoding="utf-8")
digest = hashlib.sha256(text.encode("utf-8")).hexdigest()[:12]
return text, digestPin the version in configuration, such as an environment variable, rather than "latest". Then a release is a deliberate change to one setting, and a rollback is the same change reversed.
Change one thing, then measure
Revise a prompt in small steps. If you change the role, add three rules, and reorder sections at once, a score change tells you nothing about the cause.
Every real failure becomes a permanent test case before you fix it. That way a later edit cannot reintroduce the same bug unnoticed. The function below runs one version against the whole set and reports which cases failed, so you can compare versions case by case and not only by average.
from typing import Callable
def run_suite(
prompt: str,
cases: list[dict],
call_model: Callable[[str, str], str],
check: Callable[[str], list[str]],
) -> dict[str, list[str]]:
"""Map each failing case name to its problems. An empty dict means all passed."""
failures = {}
for case in cases:
reply = call_model(prompt, case["input"])
problems = check(reply)
if problems:
failures[case["name"]] = problems
return failuresRun it on the current and the candidate version with the same cases. Release the candidate only if it fixes the target case and does not fail any case the current version passes. Models are not fully deterministic, so for borderline cases run each one several times before trusting a single result.
Keep a changelog
Write a short entry for each version. Future you will want the reason, not just the diff.
| Version | Change | Reason | Test result |
|---|---|---|---|
| v2 | Added rule to refuse tax advice | Customer asked about deductions | 18/20 pass |
| v3 | Moved output format to the end | Replies had text outside the JSON | 20/20 pass |
Maintain prompts as the app changes
Prompts drift out of date for reasons unrelated to wording:
- Model changes. A new model version may follow instructions differently. Rerun the full test set before switching, and treat the switch like a prompt release.
- Product changes. If you add a tool or rename a refund policy, update the rules that mention it. Stale rules cause confident wrong answers.
- Rule buildup. Each fix adds a line, and over time rules conflict or repeat. Review the whole prompt periodically, remove rules that no test case needs, and confirm the pass rate holds.
- Ownership. Name who approves prompt changes, and review them as you review code.
Exercise
A teammate fixed a complaint by editing v3.txt directly and redeploying. Two days later the pass rate on your test set drops, and your logs all say "v3". Name two problems with this process and what you would do instead.
Show answer
First, the version label no longer identifies the text, so logs from before and after the edit are mixed and you can't tell which prompt caused the drop. Second, the change was released without comparing test results against the previous prompt. Instead, restore the original v3 from source control, put the edit in a new file v4.txt, add the complaint as a test case, run the full set against both versions, and release v4 only if it fixes the case without new failures. Log the content hash too, so edits to a released file are detectable.