Sub topic 11
Tracing Outputs Back to Prompt Versions
This chapter assumes you can render templates from files, pin a version in your application, and read basic Python logging code. Here you add the last link in the chain. When a model gives a bad answer in production, you need to find out which prompt produced it.
Why versions need to appear in logs
Pinning decides which prompt version your application uses. It doesn't record which version a given call used. Suppose users report poor summaries last Tuesday. Your pins have changed twice since then, and the model provider may have updated the model too. Without a record on each call, you can only guess at the cause.
Each model call depends on several inputs:
- The prompt name and version.
- The model name.
- Settings such as temperature.
- The variable values that were rendered into the template.
A useful log entry captures enough of these to reproduce the call or to rule out a cause.
Logging each call
The helper below writes one structured JSON line per call. It records the prompt version and the model settings. It records the output length and not the output text.
import json, logging
logger = logging.getLogger("llm")
def log_call(prompt_name, prompt_version, model, temperature, output):
logger.info(json.dumps({
"prompt": prompt_name,
"prompt_version": prompt_version,
"model": model,
"temperature": temperature,
"output_chars": len(output),
}))Call it right after each model response, using the same version string your loader used. If the version comes from the PINNED mapping, read it from there. Don't type it a second time. A second copy can drift out of sync with the real pin, and then the logs are wrong.
JSON lines work well because log tools can filter on fields. You can search for prompt_version = "1.2.0" and compare error rates between versions.
What the data flow looks like
The pin feeds both the loader and the logger. That shared source is what keeps the log honest.
Using the logs to trace a problem
With version fields in the logs, a bad-output report becomes a short investigation:
- Find the call in the logs by time, request ID, or user.
- Read its
prompt_version,model, andtemperature. - Open the matching file, such as
prompts/summarize/1.2.0.yaml, and read its change note. - Re-run the regression cases against that version.
- If the new version is at fault, change the pin back to the previous one. That is the rollback from the pinning chapter.
Step 3 is where the change notes pay off. A note such as "Ask for a one-sentence answer when the text is short" tells you what behavior to look for.
Common errors
- Logging only the prompt name. Two versions share a name, so the name alone can't separate them.
- Using "latest" as the recorded version. An alias like
latestmoves over time. Log the resolved version number, not the alias. - Omitting model settings. A change in model or temperature can look like a prompt regression. Log them so you can tell the two apart.
- Logging sensitive text without thinking. Raw inputs and outputs can hold personal data. The example logs only the output length. If you need full text for debugging, store it separately with tighter access and a short retention period.
- Changing the log format without notice. Dashboards and queries depend on field names. Treat the log schema like any other interface.
Limits of this approach
A version number identifies the template, not the full prompt. The rendered text also depends on the input values. If you need to reproduce a call exactly, you need the inputs as well, or a hash of the rendered prompt to confirm you rebuilt the same text. Keep that in mind when you decide how much to store. More detail makes debugging easier but raises storage cost and privacy risk.
Exercise
A user reports a bad summary from yesterday. Your log line shows prompt "summarize", model "model-a", temperature 0.7, but no prompt_version. Name two problems this causes and one fix.
Show answer
Without the version you can't tell which template produced the output, so you can't open its file or change note, and you can't tell whether a recent pin change caused the problem. The fix is to add the resolved version, read from the same PINNED mapping the loader uses, to every log entry.
Exercise
Why should you log "1.2.0" instead of "latest"?
Show answer
"latest" is an alias that points to different versions over time. A log that says "latest" can't show which prompt ran at the time of the call. "1.2.0" always refers to the same file.