Sub topic 8
Pinning versions, aliases, and rollback
This chapter assumes you can store prompt templates as versioned files, such as prompts/<name>/<version>.yaml. It also assumes you can read basic Python and YAML and know how git revert works.
Why pin
A prompt file can exist in many versions. The application still needs to know which one to use. If code always loads "the newest file", every merged prompt change goes live at once. Nobody has chosen to release it, and nobody can say what production was running yesterday.
Pinning fixes this. A pin is an explicit record that says "this application uses version 1.2.0 of summarize". Changing a pin is a separate, reviewable act from writing a new version.
Pinning to an exact version
The simplest pin is a mapping in code or config:
PINNED = {"summarize": "1.2.0"}Code that asks for PINNED["summarize"] gets the same prompt on every run until someone edits that line. Two things make this safe:
- Version files are immutable. Once
1.2.0.yamlis released, you never edit it. Any change creates1.2.1.yamlor higher. - The pin lives in a file under version control, so the history of pin changes is the history of releases.
Aliases
Application code should not need to know version numbers. An alias is a stable name such as production or staging that points to a version. The alias file is the only place numbers appear.
Store the aliases in pins.yaml:
summarize:
production: "1.2.0"
staging: "1.3.0"Quote the versions. Unquoted values like 1.2 would be parsed as numbers, and 1.10 would silently become 1.1.
The loader resolves an alias first. If the reference is not an alias, it treats the reference as an exact version:
import yaml
from pathlib import Path
PROMPTS_DIR = Path("prompts")
def load_pins(path="pins.yaml"):
return yaml.safe_load(Path(path).read_text())
def resolve_version(name, ref, pins):
return pins.get(name, {}).get(ref, ref)
def load_prompt(name, ref, pins):
version = resolve_version(name, ref, pins)
path = PROMPTS_DIR / name / f"{version}.yaml"
if not path.exists():
raise FileNotFoundError(f"No prompt file for {name} {version}: {path}")
data = yaml.safe_load(path.read_text())
data["resolved_version"] = version
return data
pins = load_pins()
prompt = load_prompt("summarize", "production", pins)
print(prompt["resolved_version"])The loader attaches resolved_version to the result. Later logging should record that number, not the alias. The alias production means different things on different days, but 1.2.0 always means the same file.
Promoting a version
A release moves an alias forward. A typical sequence:
- Add
1.3.0.yamland merge it. Nothing changes in production, because no alias points to it. - Point
stagingat1.3.0and run the regression suite and manual checks. - Change
productionto1.3.0in a separate pull request.
Step 3 is a one-line diff, which is easy to review and easy to undo.
Rolling back
Because old version files stay in the repository, rollback needs no restoration. You change the pin back:
git revert <commit-that-moved-production-to-1.3.0>The revert restores production: "1.2.0". After deploying it, the application is back on the earlier prompt. The bad version file stays in history, so you can still examine why it failed.
Rollback speed depends on where the pins live. If they are in the application repository, you must redeploy. If they are in a config service or environment variable, you can roll back without a deploy. The second option is faster but harder to review, so record each change in an audit log.
Common errors
- Floating references. Using
latestas an alias defeats pinning. The prompt changes whenever someone adds a file. - Editing a released file. A small wording fix to
1.2.0.yamlchanges behavior for everyone pinned to it, and the version number no longer identifies the text. Create a new version instead. - Unquoted version strings in YAML. Numbers like
1.10lose digits when parsed. - Rolling back the prompt but not the code. If
1.3.0added a variable and the code was changed to supply it, rolling back to1.2.0may cause a "missing or extra variable" error from your validation step. Keep the declared variables compatible across versions that share an alias, or roll back both together.
Exercise
Production is pinned to 1.3.0, and users report worse summaries. The previous version, 1.2.0, is still in the repository. What is the fastest safe rollback, and why must you not edit 1.3.0.yaml to fix the wording?
Show answer
Change the production alias in pins.yaml back to "1.2.0" (for example with git revert of the commit that moved it) and deploy. Released version files must stay immutable. Editing 1.3.0.yaml would change the prompt for everything pinned to it, and the version number would no longer identify one specific text. A fix belongs in a new version such as 1.3.1.