Sub topic 2
Keeping Prompt Templates in Files, Not Code
Assume you can call a language model from Python, read text files, use Git, and understand basic YAML. This chapter focuses on managing prompts as application assets rather than scattered string literals.
Store templates outside application code
A hard-coded prompt mixes instructions with program logic. Small edits then require code changes, reviews, and deployments. Store each prompt in a file that contains its text and declared variables.
A simple layout is:
prompts/
summarize/
1.0.0.yaml
1.1.0.yamlExample file:
name: summarize
version: 1.1.0
changelog: Require a concise answer and preserve dates.
variables: [style, text]
template: |
Summarize the following text in $style style.
Preserve all dates and names.
$textThe application can load a selected file and render its variables. The prompt text can now be reviewed, tested, and changed independently from the code that calls the model.
Version prompts deliberately
Treat a prompt as a versioned artifact. A practical scheme uses major, minor, and patch numbers:
- Major: changes the expected output contract or meaning.
- Minor: adds or changes behavior without breaking callers.
- Patch: fixes wording, spelling, or a small ambiguity.
The version number alone is not enough. Record what changed and why. Keep each released version immutable. Create a new file instead of editing a version already used in production.
Pin versions and roll back
Applications should use an explicit version, not “whatever is newest.” A pin makes a model call reproducible and lets you roll back quickly.
from pathlib import Path
import yaml
PINNED = {"summarize": "1.1.0"}
def load_prompt(name):
version = PINNED[name]
path = Path("prompts") / name / f"{version}.yaml"
return yaml.safe_load(path.read_text())
prompt = load_prompt("summarize")
print(prompt["version"])An alias such as production can point to a pinned version in configuration. Change the alias only after review and regression tests. To roll back, point it to the previous known-good version. Keep aliases separate from immutable prompt files so rollback changes one small configuration value.
> callout note > Never overwrite a version that production may need for diagnosis or rollback. Add a new version and record the reason for the change. > ```
Exercise
Why should an application pin a prompt version instead of loading the newest file?
Show answer
Pinning makes behavior reproducible and allows a tested version to be restored when a change causes bad outputs.