In this categoryTroubleshooting · 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to reduce Claude hallucinations by grounding answers in your documents
Cut fabricated facts by feeding source documents into the prompt and turning on citations so every claim is traceable.
A hallucination is a confident answer that is not supported by anything real. The most reliable fix is grounding: give the model the source material in the request and tell it to answer only from that material. The Anthropic API has a built-in citations feature that ties each sentence of the answer back to a span of your document, so you can verify claims instead of trusting them.
- The Anthropic SDK installed and an API key set
- One or more source documents (text or PDF)
- A question you want answered strictly from those documents
Step 1: Put the source in a document block
Instead of pasting the source as plain text, wrap it in a document content block. This lets the API track character positions for citations later.
from anthropic import Anthropic
client = Anthropic()
source = open("policy.txt").read()
resp = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
messages=[{
"role": "user",
"content": [
{
"type": "document",
"source": {"type": "text", "media_type": "text/plain", "data": source},
"title": "Refund policy",
"citations": {"enabled": True},
},
{"type": "text", "text": "What is the refund window? Answer only from the document."},
],
}],
)Step 2: Read the citations off the response
With citations enabled, the answer is split into text blocks. Cited blocks carry a citations array, each entry pointing at the exact span of the source the claim came from. Walk the blocks to show or log where each statement is grounded.
for block in resp.content:
if block.type != "text":
continue
print(block.text)
for c in (block.citations or []):
print(" source:", repr(c.cited_text))Step 3: Keep the model honest about gaps
Test with a question the document cannot answer. A well grounded setup should refuse to invent. If it still makes something up, tighten the instruction and confirm the source actually contains the section you expected.
Result: with the source in a document block and citations on, the refund answer pointed straight at the 30 day clause, and the gift card question returned an honest I-do-not-know instead of a fabricated policy. You can now audit every claim.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.