Claude API Long Context Document Chat Guide
Claude API Long Context Document Chat Guide
If you're building a product where users upload a contract, a codebase, a research paper, or a 300-page manual and then ask questions about it, the Claude API's long context window is the feature that makes this practical without a vector database. Claude's models support context windows large enough to hold entire documents — often hundreds of pages — directly in the prompt, so the model can answer with full awareness of the text instead of relying on retrieved snippets.
This guide covers the two real approaches to document chat with Claude (full-context vs. chunked retrieval), how to structure prompts so answers stay grounded in the document, and how to avoid the latency and cost traps that come with sending large documents on every request.
Full-context vs. chunked retrieval
There are two ways to build "chat with your document" on top of the Claude API, and picking the wrong one for your use case wastes money and hurts answer quality.
Full-context (put the whole document in the prompt)
- Best when a single document is 1–200 pages and users ask open-ended questions that might touch any part of it
- No retrieval infrastructure needed — no embeddings, no vector store
- Claude can reason across the entire document at once, which matters for questions like "summarize the disagreements between section 3 and section 7"
- Downside: you resend the document on every turn unless you use prompt caching, which adds latency and token cost
Chunked retrieval (RAG)
- Best when you have many documents, or documents in the thousands of pages, and most questions only need a small slice
- Requires embeddings + a vector store (or at minimum a keyword search index)
- Cheaper per-query once set up, but adds infrastructure and a failure mode: if retrieval misses the right chunk, the answer is wrong even though the model is fine
- Worse for cross-document synthesis unless you retrieve generously
For most document-chat products — a single uploaded PDF, a single contract, a single codebase file set under a few hundred pages — full-context is simpler to build and gives better answers. Reach for chunking only when the corpus is too large to fit, or when you're serving a knowledge base rather than a single document.
Structuring the prompt for document chat
When you put a document directly in the context, structure matters more than people expect. A clean pattern:
{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"system": "You are a document assistant. Only answer using the document provided below. If the answer isn't in the document, say so explicitly.",
"messages": [
{
"role": "user",
"content": "<document>\n...full document text...\n</document>\n\nQuestion: What termination notice period does section 9 require?"
}
]
}
A few things that consistently improve grounded answers:
- Wrap the document in XML-style tags (
<document>...</document>). Claude reliably treats this as structured context distinct from the question. - Put the question after the document, not before. Models attend better to instructions placed near the end of a long prompt.
- Explicitly instruct "if it's not in the document, say so." Without this, you'll get confident answers that blend document content with general knowledge.
- Keep the conversation history, not just the document, inside the context. For multi-turn document chat, resend the document with every request unless you're caching it — the model has no memory between API calls.
Handling multi-turn chat without re-paying for the document every time
The real cost problem in document chat isn't the first question — it's the fifth one. If a document is 50,000 tokens and a user asks ten follow-up questions, naive implementations resend those 50,000 tokens ten times.
Two mitigations:
- Prompt caching. If your provider supports caching the document portion of the prompt, subsequent turns only pay for the new question and the cached-read cost, which is substantially cheaper than a full re-send.
- Summarize-then-chat for very long sessions. For sessions that run long, periodically compress earlier Q&A turns into a short summary while keeping the original document intact, so your token count doesn't grow unbounded across a long conversation.
If you're routing this traffic through SubToAPI instead of calling the Claude API directly, you get usage metadata per request out of the box, so you can see exactly how many tokens each document-chat session is consuming and catch runaway conversations before they show up as a surprise bill. Streaming is also supported, which matters for document chat specifically — users asking about a long document expect the answer to start appearing immediately rather than waiting for the full response to generate. See /docs/streaming for the implementation.
const res = 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-sonnet-4-5",
max_tokens: 1024,
stream: true,
messages: [
{ role: "user", content: `<document>${docText}</document>\n\nQuestion: ${userQuestion}` }
]
})
});
Full request/response shape is in /docs/messages, and the fastest way to get a working document-chat endpoint running is /docs/quickstart.
Where context windows still hit a wall
Even with a large context window, there are limits worth planning for:
- Scanned PDFs need OCR first. The context window doesn't help if the document is an image — extract text before sending it.
- Very large corpora (thousands of pages) still need chunking. Full-context doesn't scale to "chat with our entire knowledge base."
- Cost scales with input tokens. A 100-page document sent on every single-question request adds up fast across many users — this is where caching or session-level summarization pays for itself.
- Latency grows with prompt size. Larger prompts take longer to process before the first token streams back, so set user expectations accordingly in the UI.
If you're building document chat as a product feature rather than a one-off script, routing through an API layer like SubToAPI gives you a stable key management and billing setup for this instead of managing provider credentials per environment yourself.
questions
Does Claude's context window fit a whole book-length document? It depends on the model, but most modern Claude context windows comfortably fit hundreds of pages of plain text. For anything beyond that, or for multi-document corpora, use chunked retrieval instead of full-context.
Do I need a vector database for document chat with Claude? No, not for single documents that fit in the context window. Vector databases earn their complexity when you're searching across many documents or a corpus too large to include directly in the prompt.
How do I stop re-sending the whole document on every chat turn? Use prompt caching if available, or periodically summarize older conversation turns while keeping the source document intact, so token usage per request doesn't keep growing across a long session.