Prompt Versioning in Production
Summary
Organize the process of prompt versioning for LLM applications: store in Git, test on datasets, canary deployment, and quick rollback.
1. Prompt Storage Structure
Create a prompts/ directory in the repository with separate files for each prompt.
prompts/
├── system/
│ ├── v1.md
│ ├── v2.md
│ └── latest.md
├── user/
│ ├── summarizer_v1.md
│ └── latest.md
└── templates/
└── rag_answer.jinja2
2. Prompt File Format
Use YAML front matter for metadata and Markdown for content.
---
version: 2
created: 2025-06-01
author: ai-wiki
model: deepseek-chat
tags: [system, rag]
---
You are an assistant answering questions based on a knowledge base. Use only the provided context. If the answer is not in the context, say so.
Context:
{{context}}
Question:
{{question}}
3. Loading Prompts into the Application
Write a function to load the file and substitute variables.
import yaml
from pathlib import Path
from jinja2 import Template
def load_prompt(name, version="latest"):
path = Path(f"prompts/{name}/{version}.md")
if not path.exists():
path = Path(f"prompts/{name}/latest.md")
content = path.read_text()
parts = content.split("---", 2)
metadata = yaml.safe_load(parts[1])
template = Template(parts[2])
return metadata, template
def render_prompt(name, **kwargs):
metadata, template = load_prompt(name)
return template.render(**kwargs)
4. Testing Prompts
Create a set of test cases with expected answers.
import json
test_cases = [
{
"input": {"context": "The capital of France is Paris.", "question": "What is the capital of France?"},
"expected": "Paris"
},
{
"input": {"context": "", "question": "What is RAG?"},
"expected": "No data in context"
}
]
def evaluate_prompt(prompt_name, cases):
results = []
for case in cases:
prompt = render_prompt(prompt_name, **case["input"])
response = call_llm(prompt)
results.append({"expected": case["expected"], "actual": response})
return results
5. Canary Deployment
Roll out the new prompt version to a small percentage of traffic.
import random
def get_prompt_version(user_id):
# 10% of users receive the new version
if random.random() < 0.1:
return "v2"
return "v1"
def handle_request(user_id, context, question):
version = get_prompt_version(user_id)
prompt = render_prompt(f"system/{version}", context=context, question=question)
return call_llm(prompt)
6. Quality Metrics
Compare responses from the old and new versions. More details on metrics in the post about evaluation metrics.
from sklearn.metrics import accuracy_score
def compare_versions(old_responses, new_responses):
# Compare with expected responses
old_acc = accuracy_score(expected, old_responses)
new_acc = accuracy_score(expected, new_responses)
print(f"Old accuracy: {old_acc:.2f}, New accuracy: {new_acc:.2f}")
return new_acc >= old_acc
7. Rollback Changes
Switch back to the previous version if metrics deteriorate.
class PromptManager:
def __init__(self):
self.current_version = "v2"
self.rollback_version = "v1"
def rollback(self):
self.current_version = self.rollback_version
print(f"Rolled back to {self.rollback_version}")
def get_prompt(self, name):
return render_prompt(f"{name}/{self.current_version}")
8. Automation via CI/CD
Add a step to the pipeline that runs tests when prompts change.
# .github/workflows/prompt-test.yml
name: Test Prompts
on:
push:
paths:
- 'prompts/**'
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run prompt tests
run: python tests/test_prompts.py
9. Final Diagram
graph LR
A[Git commit] --> B[CI: tests]
B -->|Success| C[Canary deployment]
B -->|Failure| D[Abort]
C --> E[Metric monitoring]
E -->|Good| F[Full deployment]
E -->|Bad| G[Rollback]
Prompt versioning allowed me to safely experiment with changes and quickly rollback during regressions. Also recommended to explore prompt optimization approaches and system prompts.