Provenance

Provenance is the system that records which source documents grounded a RAG pipeline's answer and renders those sources as citable, human-readable output.

Mental model

When a user submits a query, the RAG pipeline retrieves document chunks and generates a response. Provenance captures a snapshot of that retrieval — what was queried, which documents were returned, and how confidently each one ranked — then attaches that snapshot to the response so readers can trace every claim back to its source.

The flow looks like this:

  1. build_citation_record converts raw RetrievalHit objects into a CitationRecord, capturing the query text, retriever name, retrieval timestamp, and up to excerpt_chars characters of each hit's content.
  2. The CitationRecord holds one CitedSource per retrieved document, each carrying the template path, category, relevance score, and optional excerpt.
  3. Optionally, the Anthropic Citations API produces ClaimCitation objects that link specific spans of the response text to specific documents by index.
  4. format_citations_markdown renders a full CitationRecord as a markdown section. format_claim_citations_markdown renders the response text with inline footnote-style callouts for each ClaimCitation.

Core data structures

CitationRecord — the top-level provenance snapshot for one pipeline run. It records:

CitedSource — one document returned by the retriever. It records:

ClaimCitation — a finer-grained citation produced by the Anthropic Citations API, linking a span of the response text (response_span) to a specific document (document_index, document_title, cited_text, cited_block_index). Use these when you need to attribute individual sentences or phrases rather than the response as a whole.

When provenance matters