Sub topic 3
Prompts, Model IDs, and Settings as Versioned Artifacts
This chapter assumes you know Python, have called an LLM API at least once, and understand why classic ML projects version datasets and trained models. You do not need any prior tooling for prompt management.
Why versioning looks different here
In classic MLOps, the thing you ship is a trained model file. You version the training data, the code, and the resulting weights. When behavior changes, you can usually trace it to one of those three.
In an LLM application you often do not train anything. The weights belong to a provider. What you control is the text you send and the settings you send it with. Those inputs decide the behavior of your product, so they take the role that the model file plays in classic MLOps.
Three things change behavior without any change to your application code:
- The prompt template, including system instructions and few-shot examples.
- The model identifier, such as a name with a date suffix.
- Generation settings, such as temperature, maximum output tokens, and stop sequences.
If you edit any of these and cannot tell afterward which edit caused a quality drop, you have an unversioned artifact. The goal of this chapter is to remove that situation.
What belongs in a version
A useful rule is that a version contains everything that, if changed, could change the output for the same user input. That list is longer than the prompt text.
| Component | Example | Why it matters |
|---|---|---|
| Template text | System message, instructions, output format rules | Small wording changes can shift tone, length, and accuracy |
| Few-shot examples | Three example tickets and summaries | They steer style and can leak into outputs |
| Model identifier | A pinned, dated model name | Providers update models behind aliases |
| Temperature and sampling | `temperature`, `top_p` | Controls output variability |
| Output limits | `max_tokens`, stop sequences | Can truncate answers or break JSON |
| Output schema | JSON schema or tool definitions | Downstream parsers depend on it |
| Retrieval settings | Chunk size, top-k, embedding model | Change the context the model sees |
Retrieval settings are covered in detail in a later chapter. For now, treat them as part of the same bundle: they are configuration that changes output, so they follow the same rules.
Pin model identifiers
Many providers offer two kinds of model names. An alias such as latest or a name without a date points to whichever snapshot the provider currently recommends. A pinned name points to one snapshot.
Aliases are convenient and risky. The provider can move the alias to a new snapshot, and your application changes behavior on a day you did not choose. Pin the dated name in production, and treat a move to a new snapshot as a release that goes through evaluation (see the chapters on evaluation and release gates).
Pinning does not last forever. Providers retire old snapshots on published schedules. Record the retirement date in your notes so the migration is planned work, not an outage.
Bundle the artifacts into one immutable object
The simplest way to keep these pieces together is a single immutable object that holds all of them and derives an identifier from its contents. The example below uses a frozen dataclass and a content hash.
import hashlib
from dataclasses import dataclass
@dataclass(frozen=True)
class PromptVersion:
name: str
template: str
model: str
temperature: float
@property
def version_id(self) -> str:
raw = f"{self.name}|{self.model}|{self.temperature}|{self.template}"
return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:12]
def render(self, **values: str) -> str:
return self.template.format(**values)
SUMMARIZE_TICKET = PromptVersion(
name="summarize_ticket",
template="Summarize this support ticket in two sentences:\n\n{ticket_text}",
model="example-model-2026-01",
temperature=0.2,
)
print(SUMMARIZE_TICKET.version_id)
print(SUMMARIZE_TICKET.render(ticket_text="My invoice shows a duplicate charge."))Three design choices are worth noting.
frozen=True. A version cannot be modified after creation. To change anything, you create a new object, which gets a new ID. This prevents code from changing a prompt at runtime and leaving logs that point to the wrong text.- Content-derived ID. The ID is a hash of the contents, so two objects with identical contents share an ID and any difference produces a different one. Nobody has to remember to bump a number.
- The model and temperature are inside the hash. Changing only the temperature from 0.2 to 0.7 is a new version. That matches reality, because it changes behavior.
A hash tells you that something changed. It does not tell you what or why. Pair the hash with a human-readable label and a change note in your version control system.
Where to store versions
There are three common storage options. Each has trade-offs.
| Option | Strengths | Weaknesses |
|---|---|---|
| Prompts in the code repository | Reviewed in pull requests, tied to a commit, easy to roll back with the code | Changing a prompt needs a deploy; non-engineers cannot edit |
| Config files or a database loaded at startup | Prompts can change without a code deploy | You must build your own review, history, and rollback |
| A hosted prompt registry | Built-in history, labels, and editing UI | Another dependency; the registry becomes production-critical |
For most teams, start with the repository. Prompts get code review and history for free, and a rollback is a revert. Move to a registry when non-engineers need to edit prompts often, or when you need to change a prompt faster than your deploy cycle allows.
Whichever you choose, keep these rules:
- Production reads a specific version, never "the latest edit."
- Every change is reviewable and has an author and a reason.
- Rolling back means pointing at a previous version, not retyping an old prompt from memory.
Keep templates separate from untrusted input
Templates use placeholders for user data. Python's str.format is simple, but it has two edge cases you should know.
First, any literal curly brace in the template, such as a JSON example, must be doubled ({{ and }}), or format raises a KeyError or ValueError. Second, format fills placeholders but does nothing to the inserted text. If the ticket text contains { characters, that is fine because format does not re-scan inserted values. Still, the inserted text is untrusted content that goes straight into the prompt. The safety chapter covers what that means for prompt injection.
A check that catches the first problem early is to render every template with dummy values in a unit test. A template that fails to render should fail the build, not a customer request.
Record the version on every request
A version ID is only useful if it ends up where you debug. Every logged model call should include the version ID, so that when a user reports a bad answer, you can find the exact prompt, model, and settings that produced it. The tracing chapter builds this out. The essential point here is that the version object and the log line are designed together.
The same ID appears in the evaluation report, the release record, and every request log. That shared key lets you ask questions such as "did the pass rate drop for version a1b2c3?" and "which requests used the version we rolled back?"
Common errors
- Editing a prompt in place in production. There is no record of the old text and no way to compare results. Create a new version.
- Hashing only the template. A model or temperature change then goes unnoticed. Include every behavior-affecting field.
- Using an alias for the model. Behavior can change without any commit on your side.
- Storing the version in one place and logging it in another. If the log records a hand-typed label, labels drift. Derive the logged value from the object that was actually used.
- Forgetting the output schema. If a parser expects a field that a new prompt version no longer produces, the failure shows up downstream, far from the cause. Version the schema with the prompt.
- Treating whitespace edits as harmless. A trailing newline changes the hash, and in some cases the output. That is the correct behavior for a content hash; accept the noise rather than normalizing it away.
Limits of this approach
A version ID identifies the inputs you control. It cannot guarantee identical outputs, because sampling is random and providers can change serving infrastructure under a pinned name. Even at temperature 0, outputs may differ between runs. The next chapter explains why, and later chapters show how to measure the effect with repeated evaluation runs instead of assuming determinism.
Exercise
Your team's prompt template and temperature are unchanged, but a teammate switches the model from a pinned dated name to the provider's latest alias. The version_id property shown above hashes name, model, temperature, and template. Does the version ID change, and what risk remains even after you pin the model again?
Show answer
Yes, the ID changes, because the model string is part of the hash input, so the switch registers as a new version and should go through evaluation. The remaining risk is that the pinned snapshot may be retired by the provider on a published date, and that outputs can still vary between runs because of sampling. So you need a tracked retirement date and repeated evaluation runs, not only a version ID.
Exercise
A prompt version stores its template and model in the repository, but the max_tokens setting lives in an environment variable. A release changes max_tokens from 1000 to 200, and JSON responses start getting cut off. Why was this hard to trace, and how would you fix it?
Show answer
The setting that changes output was outside the versioned object, so the version ID and logs did not change when behavior did. Fix it by adding max_tokens (and other output limits and the schema) as fields of the frozen version object, so they are included in the hash, evaluated, and logged with each request.