Claude API Long Context Document Summarization
Summarizing long documents with the Claude API
If you're trying to summarize long documents with the Claude API, the short answer is: Claude's large context window (up to 200K tokens depending on model) lets you paste most documents in directly without chunking, but you still need a strategy for documents that exceed the limit, for controlling output length, and for keeping costs predictable. This guide covers both the simple case and the harder edge cases.
Most "how do I summarize a PDF with Claude" questions actually have a one-call answer: convert the document to text, send it in a single message with a clear summarization instruction, and let the long context window do the work. The complexity shows up when documents are genuinely huge (legal contracts, codebases, multi-chapter reports) or when you need structured, consistent output across many documents in a pipeline.
Why long context changes the summarization approach
Before large context windows, document summarization meant splitting text into chunks, summarizing each chunk, then summarizing the summaries ("map-reduce" summarization). This works, but it loses cross-references, repeats boilerplate, and compounds errors at each step.
With Claude's context window, you can often skip map-reduce entirely:
- A 50-page PDF (roughly 30-40K tokens as plain text) fits comfortably in a single request.
- A 150K-token technical manual still fits under Claude's 200K token limit, leaving room for your prompt and the response.
- Only documents that exceed the model's context window, or that you want to process alongside other large inputs, need chunking.
This matters for accuracy: summarizing in one pass means Claude sees the whole document at once and can reference earlier sections when summarizing later ones, which produces more coherent output than independently summarized chunks stitched together.
Basic single-pass summarization
For documents that fit in context, the pattern is straightforward:
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-5",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Summarize the following document in 5 bullet points, focused on action items and deadlines:\n\n[DOCUMENT TEXT]"
}
]
}'
Key points that affect summary quality:
- Be specific about format. "Summarize this" produces inconsistent output. "Summarize in 3 paragraphs covering scope, risks, and next steps" produces repeatable, parseable results.
- Set max_tokens deliberately. A 200-word summary needs roughly 300-400 tokens of output budget, not the default. Leaving max_tokens too low will truncate summaries mid-sentence.
- Put instructions after the document for very long inputs. Some teams find Claude follows instructions more reliably when they come after the document text rather than before, especially near the upper end of the context window. Test both orderings with your actual documents.
When you do need chunking
Chunking is still necessary when:
- The document exceeds the model's context window entirely.
- You need per-section summaries (e.g., chapter-by-chapter) rather than one overall summary.
- You're processing a batch of documents and want to cap per-request token usage for cost control.
A practical chunking approach:
function chunkBySection(text, maxTokensPerChunk = 20000) {
// split on natural boundaries first: headings, page breaks, paragraphs
const sections = text.split(/\n#{1,3}\s|\n\n\n/);
const chunks = [];
let current = "";
for (const section of sections) {
if ((current + section).length / 4 > maxTokensPerChunk) {
chunks.push(current);
current = section;
} else {
current += section;
}
}
if (current) chunks.push(current);
return chunks;
}
Summarize each chunk with a prompt that explicitly asks for a dense, information-preserving summary (not a short one), then combine those intermediate summaries in a final pass that produces the actual deliverable. The intermediate summaries should be longer and more detailed than your final output — you're compressing in two stages, not one.
Controlling cost on large-scale summarization
Document summarization is one of the few Claude API use cases where input tokens, not output tokens, dominate cost — a 100K-token contract costs far more to summarize than the 300-token summary it produces. A few practical levers:
- Strip boilerplate before sending. Headers, footers, repeated legal disclaimers, and page numbers add tokens without adding information. Clean them out in preprocessing.
- Cache repeated reference material. If you're summarizing many documents against the same style guide or instructions, keep that part of the prompt stable so caching (where supported) reduces repeated-token cost.
- Pick the right model for the job. A smaller, faster model is often sufficient for first-pass extraction or chunk summaries, reserving the larger model for the final synthesis pass.
If you're building a summarization feature into a product rather than running one-off scripts, routing these calls through SubToAPI gives you a single API key, streaming support for long summaries so users see output as it's generated, and per-request usage metadata so you can see exactly which documents are driving token costs. Setup takes the same shape as a direct integration — see the quickstart and messages docs — but adds team-level visibility into usage across everyone calling the API.
Streaming long summaries
For documents that produce lengthy summaries (multi-page reports summarized into multiple paragraphs), streaming improves perceived latency significantly — users see the first paragraph while the rest is still generating, rather than waiting for the entire response.
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"content-type": "application/json"
},
body: JSON.stringify({
model: "claude-opus-4-5",
max_tokens: 2048,
stream: true,
messages: [{ role: "user", content: `Summarize this document section by section:\n\n${documentText}` }]
})
});
Details on handling the stream are in the streaming docs. If you're comparing plans for a team doing this at volume, the pricing page breaks down Solo, Team, and Scale tiers, and a free trial is available at signup.
Questions
Does Claude's context window mean I never need to chunk documents? No. Chunking is unnecessary for documents under the context limit, but still required for documents that exceed it, or when you need section-level rather than whole-document summaries.
How do I keep summary length consistent across many documents? Specify exact structure in the prompt (word count, bullet count, or section headings) rather than relying on Claude's default judgment, and set max_tokens to match.
What's the biggest cost driver in document summarization? Input tokens from the document itself, not the output summary. Stripping boilerplate and caching repeated instructions reduces cost more than optimizing the summary length.