Module 5 · Industrialization

Versioning and reviewing prompts

5 min read

In many applications, prompts live in strings scattered across the code, modified on the fly, with no history or review. Then one day quality drops and nobody knows which change caused it. A prompt is code: it is versioned, reviewed, tested, and every version in production must be identifiable. This lesson sets up Quill's prompt registry.

One prompt, one file

Each prompt lives in prompts/<name>/v<N>.md, with a metadata header:

---
name: extraction_bon_de_commande
version: 3
owner: equipe-achats
model: claude-haiku-5-5
variables: [document]
changelog: Un exemple ajouté (montant à séparateur de milliers, TVA à ne pas confondre avec le HT).
---
<exemples>
…
</exemples>

<bon_de_commande>
{{ document }}
</bon_de_commande>

Réponds uniquement par un objet JSON avec exactement ces clés : …

(The changelog reads: one example added — an amount with a thousands separator, VAT not to be confused with the amount before tax.)

MetadataRole
name, versionUnambiguously identify the text used
ownerKnow who decides on changes and answers for regressions
modelThe model the prompt was written and evaluated for: changing models means changing prompts
variablesThe prompt's contract: what the code must provide
changelogWhy this version exists, and what it changed

Variables are written {{ name }}, Promptfoo's syntax (lesson 5.2): single braces remain free for JSON examples, which contain many.

The registry: load, validate, render

def load_file(path: Path) -> Prompt:
    match = FRONT_MATTER.match(path.read_text(encoding="utf-8"))
    if not match:
        raise ValueError(f"{path} : en-tête --- manquant")
    meta = _parse_meta(match.group(1))
    missing = [k for k in REQUIRED if k not in meta]
    if missing:
        raise ValueError(f"{path} : métadonnées manquantes {missing}")
    template = match.group(2).strip() + "\n"
    used = set(VARIABLE.findall(template))
    if used != set(meta["variables"]):
        raise ValueError(f"{path} : variables déclarées {sorted(meta['variables'])} ≠ utilisées {sorted(used)}")
    return Prompt(meta["name"], int(meta["version"]), meta["owner"], meta["model"], tuple(meta["variables"]),
                  meta["changelog"], template, path)

Validation at load time catches two frequent errors: a variable used but never provided (the text {{ document }} would be sent as is to the model) and a variable declared but forgotten in the text (the document would never arrive). Rendering in turn rejects missing or extra variables:

def render(self, **values: str) -> str:
    missing = set(self.variables) - set(values)
    extra = set(values) - set(self.variables)
    if missing or extra:
        raise ValueError(f"{self.name} v{self.version} : manquantes {sorted(missing)}, en trop {sorted(extra)}")
    return VARIABLE.sub(lambda m: values[m.group(1)], self.template)

The fingerprint: linking an answer to its prompt

@property
def fingerprint(self) -> str:
    return hashlib.sha256(self.template.encode()).hexdigest()[:12]

Log, with each answer in production, the name, version and fingerprint of the prompt. The version says what was supposed to run; the fingerprint proves what actually ran — including when someone modified a file without changing its number. When a complaint comes in, you find the exact text.

Reviewing a change

A prompt change is reviewed like a code change, with a diff:

def diff(name: str, old: int, new: int, root: Path = PROMPTS_DIR) -> str:
    a, b = load(name, old, root), load(name, new, root)
    return "".join(difflib.unified_diff(a.template.splitlines(True), b.template.splitlines(True),
                                        f"{name}/v{old}", f"{name}/v{new}"))

The questions of a good review:

  1. Is the intent clear in the changelog? What problem does the new version fix?
  2. Is the change measured? A new version without evaluation results (lesson 5.2) is just an opinion.
  3. Does it introduce contradictions with existing instructions? Prompts grow in layers, and two rules end up contradicting each other.
  4. Does it change the prompt's prefix? Any change at the head invalidates the cache (lesson 3.4): do it knowingly.
  5. Are the examples still representative? An example added for a special case can distort all the others.

New version or edit?

ChangeNew version?
Typo with no effect on meaningNo — but the fingerprint changes: re-evaluate anyway
Instruction added, rephrased or removedYes
Example added or modifiedYes
Change of target modelYes, with a full re-evaluation
Change of output formatYes, and check the code that consumes the output

Keep the old versions: they allow an immediate rollback, and the comparisons ("is v3 better than v2?") that the next lesson automates.

Key takeaways

  • A prompt is code: a versioned file, with an owner, a target model, declared variables and a history.
  • Validate at load time (declared variables = used variables) and at render time (neither missing nor extra).
  • Log the name, version and fingerprint of every prompt used in production.
  • Review every change with a diff: intent, measurement, contradictions, prefix, examples.
  • Keep old versions to roll back and compare.

Practice

  1. Move a prompt from your code into a registry file, with its metadata, and load it with registry.load.
  2. Display registry.diff("extraction_bon_de_commande", 2, 3) and write the review of this change.
  3. Add to the registry a check that rejects a version whose model is not in a list of allowed models.

Resources