Prompt
How do I trace a bad LLM answer back to the exact prompt and retrieved docs?
Latest observation
To trace a bad LLM answer back to the exact prompt and retrieved documents, you want a full provenance trail for every model call. The key is to log the exact inputs the model saw, not just the user query.
What to capture for every generation
Store these fields as an immutable record:
1) User and conversation context
conversation_idmessage_iduser_query- prior turns included in context
- system/developer prompts
2) Retrieval inputs
- raw search query sent to retriever
- retriever parameters:
- top-k
- filters
- vector index/version
- reranker/version
- timestamps
- the exact retrieved chunks:
- document ID
- chunk ID
- chunk text
- chunk offsets / page numbers
- similarity scores / rerank scores
- source URI or document revision hash
3) Prompt assembled for the LLM
- full rendered prompt text
- prompt template version
- template variables
- chunk ordering
- truncation behavior
- token counts
4) Model execution metadata
- model name/version
- temperature, top-p, max tokens
- seed if supported
- request ID / response ID
- system fingerprints / deployment version
5) Output and postprocessing
- raw model output
- any parsing, filtering, or safety rewrites
- citations attached
- final displayed answer
Best practice: log the “prompt bundle”
A good pattern is to create one object per answer:
{
"trace_id": "abc123",
"conversation_id": "conv_789",
"user_query": "What is the refund policy?",
"retrieval": {
"query": "refund policy",
"top_k": 5,
"index_version": "2026-01-10",
"results": [
{
"doc_id": "policy_v4",
"chunk_id": "policy_v4#12",
"text": "Refunds are available within 30 days...",
"score": 0.91,
"source": "s3://docs/policy_v4.pdf",
"page": 4
}
]
},
"prompt": {
"template_version": "rag_answer_v7",
"rendered_text": "You are a helpful assistant...\n\nContext:\n1. [policy_v4#12] Refunds are available within 30 days...\n\nQuestion: What is the refund policy?"
},
"model": {
"name": "gpt-4.1",
"temperature": 0.2,
"max_tokens": 500
},
"output": {
"raw_text": "You can refund anytime...",
"final_text": "You can refund within 60 days..."
}
}
That lets you reconstruct exactly what the model saw.
How to make tracing reliable
Use stable IDs everywhere
Assign IDs to:
- document version
- chunk version
- prompt template version
- retriever config version
- model config version
If a doc changes later, you still need the old revision to reproduce the answer.
Store the exact retrieved chunk text
Don’t rely only on document IDs. Chunking logic can change, and chunks can be re-embedded. Save the literal text passed into the prompt.
Version your prompt template
A small change like:
- “answer using only the sources”
- vs “answer based on the sources and general knowledge” can completely change outputs.
Record ordering and truncation
If you had 10 retrieved chunks but only 4 fit in context, log:
- which 4 were included
- in what order
- what got truncated
Log the final assembled prompt, not just components
Components are useful, but the single most important artifact is the exact rendered prompt string.
If you need to debug a bad answer
Use this workflow:
- Find the trace ID for the bad response
- Inspect:
- user query
- retrieved chunks
- prompt template
- model params
- Check whether the bad answer came from:
- wrong retrieval
- missing context
- prompt ambiguity
- model hallucination
- postprocessing/citation bug
- Re-run with the same:
- prompt bundle
- model version
- retrieval version
- decoding params
If the answer is not reproducible, look for nondeterminism:
- temperature > 0
- retrieval index drift
- prompt template drift
- doc version drift
- different model backend/version
Tools / implementation ideas
Observability
Use tracing systems like:
- OpenTelemetry
- LangSmith
- Arize Phoenix
- Helicone
- Datadog custom traces
Storage
Keep:
- structured metadata in a relational DB
- prompt/output blobs in object storage
- document revisions in versioned storage
Security
Be careful not to log secrets or PII blindly. Consider:
- redaction before storage
- access controls
- retention policies
- encryption at rest
Minimal checklist
For each answer, save:
- user query
- conversation history included
- retriever query and config
- exact retrieved chunk texts
- document/chunk IDs and versions
- full rendered prompt
- model name/version and decoding params
- raw output
- postprocessing steps
- trace ID linking all of the above
If you want, I can also give you:
- a database schema for prompt/retrieval tracing, or
- a Python logging wrapper for RAG systems.