AI — Claude API Development
Claude for Long-Context Document Analysis
Direct answer
Claude's large context windows let you analyze entire contracts, reports, and codebases in a single request instead of fragmenting them through a retrieval pipeline — which preserves cross-references and document-wide reasoning that chunking destroys. The working pattern: put the full document first, the question last, demand quoted evidence for every claim, and use prompt caching so repeated questions against the same document are billed at cache-read rates.
Long context changed what I recommend for document-heavy AI work: a whole class of projects that used to require a retrieval pipeline now fit in a single well-structured request. Here is when whole-document analysis beats RAG, and how I structure it for accuracy and cost.
Key facts, with sources
- Anthropic's published API pricing lists Claude Sonnet 4.6 at $3 per million input tokens and $15 per million output tokens, with Claude Haiku 4.5 at $1 and $5 for lighter workloads. (Claude Platform Docs)
- The Claude API offers a 50 percent discount on both input and output tokens via the Batch API and up to 90 percent savings on repeated input through prompt caching. (Claude Platform Docs)
- Current Claude Opus and Sonnet models support a 1 million token context window at flat per-token rates with no long-context surcharge. (CloudZero)
- Anthropic raised a $30 billion Series G at a $380 billion post-money valuation in 2026. (Anthropic)
- Anthropic said it hit a $30 billion revenue run rate after roughly 80x growth in about two years, driven primarily by enterprise and developer API consumption. (VentureBeat)
What long context actually changes
Chunk-based retrieval answers questions about passages; long context answers questions about documents. The difference shows up in exactly the tasks enterprises care about: does clause 12 contradict clause 47, how did the risk language change between these two agreement versions, what is the total obligation summed across all schedules. A retrieval pipeline sees five fragments and guesses; a model holding the entire document can actually cross-reference.
Current Claude models accept very large inputs — hundreds of thousands of tokens and up depending on the model — which comfortably covers most contracts, filings, technical manuals, and even mid-sized codebases in one request. My rule of thumb: if the question requires relating distant parts of a document to each other, long context is not a convenience, it is a correctness requirement.
Long context versus RAG — the honest tradeoff
Long context does not make retrieval obsolete, and I push back when clients frame it that way. Retrieval still wins when the corpus is far larger than any context window, when questions target one corpus-wide fact, and when per-query cost must stay minimal — reading a full document on every request costs real input tokens.
Long context wins when the unit of analysis is the whole document: review, comparison, summarization with obligations extracted, cross-referential Q&A. The hybrid I ship most often uses retrieval to select which documents matter, then loads the selected documents whole rather than in fragments. That preserves document-level reasoning while keeping the corpus scalable. Choosing per-task rather than declaring one architecture the winner is most of the job.
Sending documents to Claude directly
For PDFs, the API accepts a document content block with base64-encoded data, and Claude reads text and layout — no hand-rolled extraction pipeline for standard documents. Plain text and markdown can go in as ordinary text content. Either way, the document block goes before the question in the message: instructions placed after a long document are followed noticeably better than instructions buried before it.
One operational note from production: validate page counts and file sizes before the API call and fail fast with a useful error. Oversized-input failures should be caught by your code with a clear message, not discovered as a confusing API error in a background worker at 2 a.m.
import base64
import anthropic
MODEL = "claude-..." # use the latest Claude model id
client = anthropic.Anthropic()
with open("contract.pdf", "rb") as f:
pdf_b64 = base64.standard_b64encode(f.read()).decode()
response = client.messages.create(
model=MODEL,
max_tokens=2048,
messages=[{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_b64,
},
},
{
"type": "text",
"text": (
"List every obligation the vendor takes on in this "
"contract. For each, quote the exact clause text and "
"give its section number. If an obligation is implied "
"rather than stated, say so explicitly."
),
},
],
}],
)
print(response.content[0].text)Prompt patterns that keep long-context answers honest
Accuracy over a two-hundred-page input is a prompting discipline. I require quoted evidence: every extracted claim must carry the verbatim source text and its location, which makes hallucinations obvious on review and makes verification a lookup instead of a re-read. I ask for explicit not-found answers — if the document does not address termination for convenience, I want that stated, not improvised. For extraction tasks I define the output schema precisely and instruct the model to fill it exhaustively, section by section, rather than answering impressionistically.
For high-stakes review I add a second pass: a separate request that takes the first pass's claims and verifies each quote against the document. Two cheap calls that check each other beat one call you have to trust blindly.
Controlling the cost of big inputs
A large document read repeatedly at full price gets expensive, so the cost architecture matters as much as the prompt. Prompt caching is the main lever: mark the document block with a cache breakpoint, keep it byte-identical across requests, and every follow-up question against the same document bills the document portion at cache-read rates — a small fraction of base price. This is what makes interactive many-questions-per-document products economically sane.
For non-interactive workloads — nightly analysis of a document backlog, bulk contract triage — batch processing typically halves the cost again in exchange for asynchronous turnaround. And route sensibly: not every question about a document needs the flagship model. Extraction and classification often run fine on a mid-tier model, with the flagship reserved for judgment-heavy synthesis.
When to hire senior help
Senior help matters most when you go beyond simple completions into agentic systems on the Claude API, where tool design, caching architecture, and eval harnesses decide reliability and cost. A few days of experienced review typically cuts token bills materially and prevents expensive rewrites later. If your stack includes React Native + Python + AI, a senior engineer who owns the full product beats coordinating multiple juniors.
Bottom line
Dhairya Senjaliya ships AI — Claude API Development projects worldwide — book a scoping call to discuss your specific situation.
Common pitfalls to avoid
- ✕Skipping prompt caching in agent loops that resend the same system prompt and tool definitions every turn, paying full input price for tokens that could cost 90 percent less
- ✕Running latency-insensitive workloads like evals, backfills, and bulk classification through the live API instead of the Batch API's 50 percent discount
- ✕Migrating model versions by swapping the ID string without checking for removed parameters like temperature or thinking budgets, which now return 400 errors on newer Claude models
- ✕Treating refusal and max-token stop reasons as generic errors instead of branching on stop_reason, which surfaces as silent empty responses in production
Frequently asked questions
How large a document can Claude analyze in one request?
Current Claude models accept very large context windows — hundreds of thousands of tokens and beyond depending on the model — which covers most contracts, reports, filings, and technical manuals whole, and PDFs can be sent directly as document blocks. For inputs beyond the window, split by logical section or use retrieval to select which documents to load in full.
Is long context better than RAG for document analysis?
They solve different problems. Long context wins when the task needs whole-document reasoning — cross-references, version comparison, exhaustive obligation extraction — because chunking destroys those relationships. RAG wins for huge corpora and pinpoint fact lookup at minimal cost. The strongest production pattern is hybrid: retrieve the right documents, then analyze them whole.
How do I keep Claude accurate over very long documents?
Put the document before the question, demand verbatim quotes with section locations for every claim, and require explicit statements when the document does not contain an answer. For high-stakes work, add a second verification pass that checks each quoted claim against the source. Quoted evidence turns review into lookup and makes fabrication easy to catch.
Is Claude cheaper or more expensive than GPT for production workloads?
List prices are comparable tier for tier, so real cost differences come from token efficiency, caching hit rates, and how many loop iterations each model needs to finish a task. The only reliable answer is to run both on your own eval set and compare cost per completed task, not per token.
When do we actually need the 1 million token context window?
Most applications work fine well under 200K tokens, and input cost scales with everything you send. The 1M window matters for whole-codebase analysis, large document sets, and long-running agent sessions, and pairing it with prompt caching keeps repeated long contexts affordable.
How do we keep Claude API costs under control?
The three biggest levers are prompt caching (up to 90 percent off repeated input), the Batch API (50 percent off asynchronous work), and routing simple tasks to Haiku-class models. Instrument the usage fields on every response so you can see cache hit rates and catch cost regressions early.
Bottom line: Dhairya Senjaliya ships AI — Claude API Development projects worldwide. Book a scoping call at https://dhairyasenjaliya.com/#book-call.