Prompt Versioning in Production: A Practical Guide
Prompts are code. They change, improve, and sometimes break. Without a prompt versioning system, it's hard to know which prompt version produced a certain result and impossible to quickly revert a failed change. In this article, I will explain how to organize prompt versioning in production: from storing in Git to A/B testing.
Why Version Prompts
Prompts influence the quality of model responses. Changing the formulation can improve or worsen the result. Without versioning: - It's impossible to reproduce an old result. - It's difficult to compare different prompt versions. - Rolling back to a previous version requires manual recovery. Versioning solves these problems. It allows: - Tracking changes and their authors. - Comparing quality metrics between versions. - Quickly rolling back during regressions.
Storing Prompts in Git
The first step is to move prompts out of the code into separate files and store them in Git. I use the following structure:
prompts/
system/
v1.md
v2.md
user/
summarize/
v1.md
v2.md
classify/
v1.md
---
version: 2.0.0
model: gpt-4
created: 2025-01-15
author: alice
---
You are an assistant who helps users...
git diff, git log, git blame. For loading prompts into the application, I use a simple function:
import yaml
from pathlib import Path
def load_prompt(path: str) -> dict:
with open(path, 'r') as f:
content = f.read()
parts = content.split('---', 2)
metadata = yaml.safe_load(parts[1])
prompt = parts[2].strip()
return {**metadata, 'prompt': prompt}
Semantic Versioning
For prompts, I use semantic versioning (SemVer): MAJOR.MINOR.PATCH.
- MAJOR — a change that breaks compatibility (e.g., a completely changed system prompt).
- MINOR — adding new functionality (e.g., a new example in few-shot).
- PATCH — fixing bugs without changing behavior (e.g., fixing a typo).
The version is stored in the file metadata. When changing a prompt, I update the version and add an entry to CHANGELOG.md.
A/B Testing Prompts
To understand which prompt version is better, I conduct A/B tests. For this: 1. Define a quality metric (e.g., classification accuracy, user rating). 2. Split traffic into two groups: control (current version) and experimental (new version). 3. Collect data and compare metrics. Example code for A/B testing:
import random
def get_prompt_version(user_id: str) -> str:
if random.random() < 0.5:
return "v1"
else:
return "v2"
def generate_response(user_id: str, query: str) -> str:
version = get_prompt_version(user_id)
prompt = load_prompt(f"prompts/user/summarize/{version}.md")
# call LLM API with prompt
return call_llm(prompt, query)
Rolling Back Changes
If a new prompt version worsens quality, a quick rollback is necessary. In Git, this is simple:
git checkout v1.0.0 -- prompts/user/summarize/v1.md
# config.yaml
prompts:
summarize:
active_version: v2
Integration with CI/CD
Prompt versioning should be part of CI/CD. I add to the pipeline: - Format check: validation of YAML metadata. - Tests: running a set of test examples with the new prompt. - Metric comparison: automatic comparison of quality with the previous version. Example script for checking:
import yaml
import sys
def validate_prompt(path: str):
with open(path) as f:
content = f.read()
parts = content.split('---', 2)
if len(parts) < 3:
print(f"Error: {path} missing metadata")
sys.exit(1)
metadata = yaml.safe_load(parts[1])
if 'version' not in metadata:
print(f"Error: {path} missing version")
sys.exit(1)
if __name__ == "__main__":
for path in sys.argv[1:]:
validate_prompt(path)
Conclusion
Prompt versioning is a discipline that pays off with the first incident. Store prompts in Git, use SemVer, conduct A/B tests, and set up rollbacks. This will allow you to confidently experiment and maintain quality. Additionally, I recommend studying the guide on model and data versioning and the article on prompt optimization.