Prompt Templates, Versioning and Iteration

Treat prompts as code: templates with variables, version control, change reviews and a repeatable loop for improving them.

Intermediate · about 3 hours · difficulty 2/5

Learn first: Structured Prompts and Long Context, Git and Project Hygiene

Once an app has more than two prompts, ad hoc strings scattered through the code become a liability. Nobody knows which version is live or why a behaviour changed.

Practices

  • Templates: store prompts as files or constants with named variables (Jinja, f-strings, or a template library). Keep business logic out of the template.
  • Version control: prompts live in Git with the code, or in a prompt registry that records versions. Every production response should be traceable to a prompt version.
  • Review changes like code: a prompt diff can change behaviour for every user. Require a test run before merging.
  • Iteration loop: collect failing examples, form a hypothesis, change one thing, rerun the test set, keep or revert.
  • Model-specific tuning: a prompt tuned for one model family may need adjustments for another. Record which model a prompt was validated with.

Tools

Prompt playgrounds in provider consoles are good for exploring. Observability platforms like Langfuse and LangSmith offer prompt registries linked to traces. Tools like promptfoo let you run a prompt against a test matrix from the command line. The evaluation section builds on this.

Pitfalls

Endless tweaking without measurement. If you cannot say whether version 7 beats version 6 on a test set, you are guessing.

Good enough to move on

Your project has prompts in versioned template files, and you can rerun a fixed test set after any prompt change.

Key concepts

  • Prompt templates with variables
  • Versioning prompts in Git
  • Prompt registries
  • Test-driven prompt iteration
  • promptfoo
  • Per-model prompt variants

Mini project

Refactor an existing app so all prompts live in versioned template files, log the prompt version with each response, and add a script that runs a 20-case test set against any prompt version.

Check yourself

A user complaint arrives about an answer from two weeks ago. What would you need to have logged to reproduce it exactly?

Free resources