Prompts as code: versioning
Treat every prompt as a deployable artifact: templated, versioned with its model and parameters, reviewed, eval-gated in CI, rolled out gradually and logged with every trace so you can roll back in seconds.
Analogy: Database migrations: nobody edits the production schema by hand. Changes are numbered, reviewed, tested and reversible.
How it works
- Version the whole prompt config: template, variables, model snapshot, parameters, tool and output schemas.
- Gate every change on a golden set of ~50–200 cases that finishes in minutes, or people skip it.
- Versions are immutable; environments are labels like
productionpointing to one, so rollback means moving a label. - Roll out gradually behind a flag, ~1–5% → 25–50% → 100%, reading online metrics at each step.
- Pin model snapshots, not aliases: an alias can move and silently change behavior, and snapshots still retire.
Check yourself
Answer to mark it learned; your answers aren't scored.
Your prompts live in a registry with immutable versions and staging/production labels. A PM wants to fix a customer complaint today by editing the live production prompt directly in the registry UI. What do you require?
Go deeper
Full explanation
A prompt is code that runs on a model: a one-word edit can change behavior as much as a code change, so it gets the same discipline.
The unit you version is not the text alone but a prompt config: template + variables + model snapshot + parameters (temperature, max tokens) + tool and output schemas. Change any of them and outputs change.
The lifecycle:
- Templates, not string concatenation: one file or registry entry per prompt, with named variables (Jinja, Mustache).
- Review the diff in a PR, like code.
- Evals on every change: a golden set runs in CI (Evals in CI/CD & release gates) and a regression blocks the merge.
- Immutable versions in git or a registry (Langfuse, LangSmith, PromptLayer, MLflow). Environments are labels (
staging,production) that point to a version. - Gradual rollout behind a feature flag (5% → 50% → 100%), watching online metrics.
- Version on every trace: log prompt version and model id (LLM observability & tracing) so any bad output maps to the exact prompt that produced it.
Pin model snapshots (gpt-4o-2024-08-06, not gpt-4o): an alias can move under you and silently change behavior.
The anti-pattern: f-strings scattered across 30 files, edited in hotfixes, and nobody can say which prompt is live.
Code Fetch a labeled prompt version and trace it
from langfuse import Langfuse
from openai import OpenAI
langfuse, llm = Langfuse(), OpenAI()
DEFAULT_MODEL = "gpt-4o-2024-08-06" # pinned snapshot, never a floating alias
def triage(ticket: str) -> str:
# Resolved by label on EVERY call (served from the SDK's local cache), so
# moving "production" to another version = deploy or rollback within ~60 s
prompt = langfuse.get_prompt(
"support-triage",
label="production",
cache_ttl_seconds=60, # keep the registry off the hot path
fallback="Classify this ticket: {{ticket}}", # served if the registry is down
)
cfg = prompt.config or {} # model + params versioned with the text
resp = llm.chat.completions.create(
model=cfg.get("model", DEFAULT_MODEL),
temperature=cfg.get("temperature", 0),
messages=[{"role": "user", "content": prompt.compile(ticket=ticket)}],
)
out = resp.choices[0].message.content
# Every trace records exactly which prompt and model produced it
log_trace(prompt=f"{prompt.name}@v{prompt.version}", model=resp.model, output=out)
return outReal example
A fintech's chargeback classifier prompt lived as an f-string in three services. A "small wording fix" shipped in a hotfix and misrouted ~8% of disputes for two days, and nobody could tell which version had run. After moving to a registry, each change runs a 300-case golden set in CI, goes to 5% of traffic behind a flag, and every trace carries dispute-triage@v14. The next regression was caught at 5% and rolled back in under a minute by moving the production label.
Trade-offs +4 · −3
+ You gain
- Every output is traceable to an exact prompt, model and parameter set.
- Rollback is a label move, not a redeploy: seconds instead of a release cycle.
- Regressions are caught in CI before users see them.
- PMs and domain experts can iterate safely inside the same guardrails.
− You pay
- Upfront cost: an eval set, CI wiring and a registry or file convention before the first win.
- A runtime registry is another dependency on the request path; it needs caching and a fallback.
- Prompts stored apart from code can drift from the parser that consumes their output.
Use it when
As soon as a prompt serves real users or more than one person edits it. Add a registry when non-engineers iterate weekly or you need rollback without a deploy.
Avoid it when
Throwaway notebooks and one-off scripts. And skip a heavy registry for one prompt owned by one engineer: a file in git plus an eval test is enough.
Numbers to know 4
- Rollout ladder: ~1–5% → 25–50% → 100%, holding at each step long enough to read online metrics.
- Start the CI golden set at ~50–200 cases per prompt; it must finish in minutes or people skip it.
- Registry fetches: client-side cache (TTL ~60 s, Langfuse's default) plus a bundled fallback, so an outage never blocks requests.
- Pinned snapshots still retire: in Oct 2026 OpenAI gave 3 months' notice that gpt-4o and gpt-4.1 snapshots shut down on Jan 14, 2027. Budget a re-eval per migration.
Common pitfalls 4
- Versioning the text but not the model and temperature: the same "v12" behaves differently next month.
- Floating model aliases in production, then blaming the prompt when the provider updates the snapshot.
- Editing the live prompt in a registry UI with no eval gate: a hotfix by another name.
- Not logging the prompt version on traces, so incidents can't be bisected.
Interview questions 2
Answer out loud first, then reveal.
Should prompts live in git or in a prompt registry?Show answer
It depends on who edits them and how fast I need to roll back. My default is git: prompts as template files next to the code that parses their output, reviewed in PRs and eval-gated in CI, so prompt and parser change atomically. I add a registry when PMs or domain experts iterate weekly, or when I need rollback without a deploy. Then versions are immutable, promotion is gated on the same evals, fetches are cached with a bundled fallback, and the version is still logged on every trace.
A prompt change shipped and quality dropped. What should have caught it, and how do you recover?Show answer
Three layers. Offline, a golden-set eval in CI comparing new against current on the same cases, blocking on regressions in the metrics we care about. Online, a gradual rollout behind a flag with dashboards split by prompt version, so a drop at 5% shows before it reaches everyone. And traces tagged with prompt and model version. To recover, I move the production label back to the last good version, which takes seconds and no deploy, then add the failing cases to the golden set so this regression can't recur silently.