CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
Ground Your Claude RAG Answers in Sources: A Practical Guide to the Citations API

Ground Your Claude RAG Answers in Sources: A Practical Guide to the Citations API

Chris Harper

3 min read

Jul 4, 2026 · 12:06 UTC

AI
Tutorial
RAG
Best Practices

TL;DR: The Anthropic Citations API lets Claude attribute each sentence of its answer to the exact passage it came from — add "citations": {"enabled": true} to each document you pass and get structured citation objects back, no format prompting required.

What you'll be able to do after this:

  • Pass retrieved documents to Claude and get back answers where every claim is linked to an exact source passage — character ranges, page numbers, or custom chunk IDs depending on document type
  • Replace brittle prompt-engineered attribution ("cite your sources in [n] format") with a first-class API feature that Claude evaluates more accurately
  • Render inline footnotes, "cited from doc X, lines Y–Z" callouts, or source panels directly from the structured citation objects Claude returns

Why citations matter in RAG

Every RAG app has the same trust problem: Claude generates a plausible-sounding answer, but which parts came from your documents and which are interpolated? Prompt-engineering your way to citations is inconsistent — Claude sometimes over-cites, sometimes skips attribution entirely.

The Citations API moves attribution into the model call itself. Claude returns a structured citations array alongside each response text block, pointing at exact character ranges (or pages, or custom chunks) in your source documents. In Anthropic's own evaluations, the Citations API cites the most relevant quotes significantly more reliably than purely prompt-based approaches.

Enabling citations — one field change

Add "citations": {"enabled": true} to each document object. Citations must be enabled on all documents in a request or none.

import anthropic

client = anthropic.Anthropic()

# Chunks retrieved from your vector DB
chunks = [
    {
        "type": "document",
        "source": {
            "type": "text",
            "media_type": "text/plain",
            "data": "The context window for Claude Sonnet 5 is 1 million tokens."
        },
        "title": "claude-models.md",
        "citations": {"enabled": True}   # <-- the only change
    },
    {
        "type": "document",
        "source": {
            "type": "text",
            "media_type": "text/plain",
            "data": "Claude Code uses Sonnet 5 as its default model as of June 2026."
        },
        "title": "claude-code-changelog.md",
        "citations": {"enabled": True}
    }
]

response = client.messages.create(
    model="claude-sonnet-5-20260629",
    max_tokens=512,
    messages=[{
        "role": "user",
        "content": chunks + [{"type": "text", "text": "What model does Claude Code use by default?"}]
    }]
)

for block in response.content:
    if block.type == "text":
        print(block.text)
        for c in getattr(block, "citations", []):
            print(f"  [{c.document_title}] chars {c.start_char_index}–{c.end_char_index}")

Output: Claude's answer text, plus one citation object per attributed sentence pointing at the exact character span in claude-code-changelog.md.

Three granularity levels

Document typeCitation typeWhat you get
Plain textchar_locationstart_char_index, end_char_index
PDFpage_locationstart_page_number, end_page_number
Custom chunkscontent_block_locationstart_block_index, end_block_index

For plain text, Claude cites at the sentence level by default — or chains consecutive sentences together when the claim spans a paragraph. For PDFs, it cites by page. If you chunk documents yourself (by section header, by paragraph), pass them as custom content blocks and get back citations that point to your own chunk IDs.

RAG search results as first-class inputs

The API includes a search_results document type that wires directly into tool-use RAG patterns. Your tool returns search results as structured content blocks; citations flow through automatically — no prompt glue needed between your retrieval and generation steps.

Sources: Citations — Claude Platform Docs · Introducing Citations on the Anthropic API — Anthropic · Simon Willison's deep dive