Prompt

How do I trace a bad LLM answer back to the exact prompt and retrieved docs?

Artificial Intelligence / AI Platforms1 observationLast seen Nov 18, 2026

Latest observation

Nov 18, 2026OpenAI APIWeb search: off

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_id
  • message_id
  • user_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:

  1. Find the trace ID for the bad response
  2. Inspect:
    • user query
    • retrieved chunks
    • prompt template
    • model params
  3. Check whether the bad answer came from:
    • wrong retrieval
    • missing context
    • prompt ambiguity
    • model hallucination
    • postprocessing/citation bug
  4. 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:

  1. a database schema for prompt/retrieval tracing, or
  2. a Python logging wrapper for RAG systems.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.