- Turn on
citations.enabledon every document block so Claude returns structured locations instead of freeform "according to the docs" prose. - Prefer plain text or PDF for sentence chunking, or custom content blocks when you need RAG-chunk granularity.
- Do not combine citations with
output_config.formatJSON schema. The API returns 400 because citation spans cannot satisfy a single schema object.
You shipped a RAG answer that sounded sourced and still could not show the sentence. Prompting "cite your sources" produces decorative footnotes. The Messages API can attach citation metadata to text blocks when you enable citations on the documents you already send.
Why it matters
Support and internal Q&A break when operators cannot jump from a claim to a page, character range, or chunk. Prompt-only citation instructions waste output tokens on quoted text and still invent soft references. Anthropic's citations feature chunks your documents, grounds claims, and returns location objects the client can render. cited_text does not count toward output tokens. Enable citations on all documents in the request or on none, keep title and context non-citable, and skip structured outputs on the same call. The one move: set citations.enabled to true on each document block and parse the citation arrays on the text blocks you render.
How to enable document citations
Use the Messages API with a document content block, citations.enabled true, and a real model id from current docs examples (claude-opus-5 below). Put secrets in the environment. Prefer plain text for prose, PDF when you need page numbers, and custom content when each RAG chunk must stay one citable unit.
- 1. Attach one or more
documentblocks before the user question. Setcitations.enabledto true on every document in the request. - 2. Choose a source type:
text(sentence chunking, char indices), PDF (base64orurl, page numbers), orcontent(your blocks, no further chunking). - 3. Optionally set
titleandcontextfor display and non-citable hints. Onlysourcetext is citable. - 4. On HTTP 200, walk
contenttext blocks and render anycitationsarrays (char_location,page_location, orcontent_block_location). Do not enableoutput_config.formaton this request.
#!/usr/bin/env bash
set -euo pipefail
: "${ANTHROPIC_API_KEY:?Set ANTHROPIC_API_KEY in your environment}"
# Document grounding: citations on the document block, not prompt-only "please cite".
curl -sS https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: ${ANTHROPIC_API_KEY}" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-opus-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "Refunds ship within five business days after approval. Express shipping is unavailable on clearance items. Support hours are 09:00-17:00 PT Monday through Friday."
},
"title": "Returns policy excerpt",
"context": "Internal policy snippet for customer support grounding. Not a full legal contract.",
"citations": {"enabled": true}
},
{
"type": "text",
"text": "How fast do refunds ship after approval, and can clearance orders use express shipping?"
}
]
}
]
}'
# Render each text block's citations[].cited_text plus char/page/block indices in your UI.
Gotchas
- Citations must be enabled on all documents in the request or on none. Mixed settings are not supported.
- Citations plus structured outputs (
output_config.format/ deprecatedoutput_format) return 400. Tip 3's JSON schema path and this tip are alternate reply shapes, not a combo. - Image citations from PDFs are not supported yet. Scanned PDFs without extractable text are not usefully citable.
cited_textdoes not count toward output tokens, and when you pass citation blocks back in later turns it also skips input token counting for that field.- Prompt caching still works with
cache_controlon the document block. For finer RAG control, put each retrieved chunk in its own plain text document or use a custom content document with one block per chunk.
Recommended AI prompt
Copy this paragraph into ChatGPT, Claude, Gemini, Grok, or whatever you use.
Review my Claude Messages RAG call and rewrite it so answers are document-grounded with API citations. Put each source in a document block with citations.enabled true on every document, pick plain text versus PDF versus custom content for the citation granularity I need, keep title and context non-citable, omit output_config.format, and show how to render citations on text blocks in the UI. Return a short checklist plus the one request-body change.
Sources: Citations · Introducing Citations