← Blog

Claude API Long Document Chat Interface: Build Guide

2026-11-22 · 5 min read · SubToAPI Team

Building a chat interface that lets users ask questions about long documents — contracts, research papers, codebases, support transcripts — comes down to three decisions: how you fit the document into Claude's context window, how you manage conversation state across turns, and how you stream responses so the UI feels responsive instead of frozen.

The short answer: for documents under roughly 150,000 tokens, send the full text in the system prompt or as a leading user message and let Claude's large context window do the work — no chunking or retrieval layer needed. For documents larger than that, or for collections of many documents, you chunk the content, retrieve the relevant sections per question, and send only those sections plus the running conversation history. Everything below explains how to implement both patterns in a real chat UI.

Full-context vs. retrieval: picking the right pattern

Claude's context window is large enough that most "long document" use cases — a 40-page PDF, a full legal contract, a product spec — fit entirely in a single request. This is the simplest and most reliable approach because Claude sees the whole document and doesn't miss cross-references between sections.

Use full-context when:

Use chunking + retrieval when:

A hybrid that works well in practice: load the full document once, then rely on prompt caching (if your provider supports it) or simply keep the document text in the first message of the conversation and only append new user turns after that — you don't need to re-paste the document every time.

Structuring the conversation

A long-document chat interface is still a standard multi-turn conversation under the hood. The document goes in once, early in the message array, and subsequent turns are just question/answer pairs appended to the same thread.

[
  { "role": "user", "content": "Here is the document:\n\n<full text>\n\nI'll ask questions about it." },
  { "role": "assistant", "content": "Got it, ready for your questions." },
  { "role": "user", "content": "What are the termination clauses?" }
]

Keep the entire array in your backend's session store (keyed by a chat/session ID) and send the full history on every request — the model has no memory between API calls. For very long chats, trim or summarize older turns once the running token count approaches your budget, but keep the original document text pinned at the start of the array so it's never dropped.

Chunking for oversized or multi-document corpora

When a single document doesn't fit, split it into overlapping chunks (500–1,000 tokens with 10–15% overlap works for most prose and code), embed each chunk, and retrieve the top-k chunks relevant to the user's current question before calling Claude.

async function answerFromChunks(question, chunks, embedModel) {
  const queryVector = await embed(question, embedModel);
  const topChunks = rankBySimilarity(queryVector, chunks).slice(0, 5);
  const context = topChunks.map(c => c.text).join("\n\n---\n\n");

  return callClaude([
    { role: "user", content: `Context:\n${context}\n\nQuestion: ${question}` }
  ]);
}

This keeps every request small and cheap, but the model only sees what retrieval surfaces — if a question requires reasoning across two chunks that weren't retrieved together, it can miss the connection. Tune chunk size and top-k per document type rather than using one global setting.

Streaming responses in the chat UI

Long documents often lead to long, detailed answers. Streaming the response token-by-token is what makes the interface feel like a chat rather than a form submission. On the frontend, read the stream and append tokens to the message bubble as they arrive instead of waiting for the full 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-sonnet-4",
    stream: true,
    messages: [
      { role: "user", content: `${documentText}\n\nQuestion: ${question}` }
    ]
  })
});

const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  buffer += decoder.decode(value);
  renderPartial(buffer); // update the chat bubble incrementally
}

If you're exposing this chat feature as a product API — for internal tools, a customer-facing support bot, or a SaaS add-on — SubToAPI gives you the same /v1/messages and streaming endpoints behind application API keys, with per-key usage metadata so you can see token and cost breakdowns per chat session or per customer. See the streaming docs and quickstart for the exact request shape.

Managing cost and token limits

Long-document chats accumulate tokens fast because the document text is often re-sent on every turn if you're not careful with session storage. A few practical guardrails:

SubToAPI's dashboard reports usage per API key, which is useful if each chat session or customer maps to its own key — you get a built-in cost breakdown without building usage tracking yourself. Check /pricing for how the Solo, Team, and Scale plans handle seat and key limits, and /docs/messages for the full request reference.

Questions

Can Claude read an entire PDF in one request? Yes, if the extracted text fits within the context window (roughly 150K tokens for most practical chat use cases). Extract the text first — Claude's chat API takes text input, not binary PDF files directly — then send it as part of the conversation.

Do I need a vector database for a long-document chat interface? Only if your documents exceed the context window or you're searching across many documents at once. For single documents that fit in context, skip retrieval entirely and send the full text — it's simpler and more accurate.

How do I keep conversation context across multiple questions about the same document? Store the full message array (including the original document text) per chat session in your backend, and resend the entire array with each new question. Claude has no memory between separate API calls — the client is responsible for maintaining history.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →