← Blog

Building a Document Summarization Web App with Claude API

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

What a Document Summarization Web App Needs from the Claude API

If you're building a web app that lets users upload a PDF, Word doc, or long text file and get back a clean summary, Claude is a strong choice because of its large context window and strong instruction-following for structured output (bullet points, executive summaries, key takeaways). The core technical challenge isn't "can Claude summarize text" — it can — it's everything around that call: extracting text from documents, handling files that exceed the context window, streaming partial results to the UI so users aren't staring at a spinner, and keeping the whole thing fast and cheap at scale.

This article walks through a practical architecture for a Claude-powered document summarization web app: text extraction, chunking strategy, the actual API calls, streaming the response, and what changes when you move from a weekend prototype to something you charge users for.

Architecture Overview

A typical document summarization app has four moving parts:

  1. Upload & extraction — convert PDF/DOCX/TXT into plain text server-side
  2. Chunking — split long documents into pieces that fit comfortably in context
  3. Summarization calls — one or more requests to Claude, possibly map-reduce style
  4. Delivery — stream the summary back to the browser as it's generated

For extraction, libraries like pdf-parse (Node) or pypdf (Python) handle most PDFs. DOCX files extract cleanly with mammoth. The output you want is plain UTF-8 text with page breaks or section markers preserved — those markers help Claude keep structure in longer summaries.

Step 1: Chunking Long Documents

Even with a large context window, you shouldn't just dump a 200-page report into a single prompt if you want consistent quality. A simple and effective approach is splitting by section or by a fixed token budget (e.g., 6,000–8,000 tokens per chunk), with a small overlap between chunks so context isn't lost at boundaries.

function chunkText(text, maxChars = 24000, overlap = 500) {
  const chunks = [];
  let start = 0;
  while (start < text.length) {
    const end = Math.min(start + maxChars, text.length);
    chunks.push(text.slice(start, end));
    start = end - overlap;
  }
  return chunks;
}

This is character-based rather than token-based for simplicity — in production, use a tokenizer to size chunks more precisely, since Claude's pricing and context limits are token-based, not character-based.

Step 2: Calling Claude for Summarization

For a single chunk, the request is straightforward. Here's a generic call to the Claude Messages API:

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-20250514",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Summarize the following text in 5 bullet points, focusing on decisions and action items:\n\n<document text here>"}
    ]
  }'

The prompt matters more than most people expect. Being explicit about format ("5 bullet points," "150 words," "include a one-line TL;DR") produces much more consistent output than a bare "summarize this." If your users need different summary styles (executive summary vs. detailed notes vs. key risks), expose that as a parameter in your prompt template rather than hardcoding one instruction.

Step 3: Map-Reduce for Long Documents

When a document spans multiple chunks, summarize each chunk independently, then run a second pass that summarizes the summaries:

async function summarizeDocument(chunks, summarizeFn) {
  const chunkSummaries = await Promise.all(
    chunks.map(chunk => summarizeFn(chunk, 'Summarize this section in 3-4 sentences.'))
  );

  const combined = chunkSummaries.join('\n\n');
  return summarizeFn(combined, 'Combine these section summaries into one coherent summary with key points.');
}

This "map-reduce" pattern keeps quality high on long documents and parallelizes the first pass, which matters for response time. The second pass is cheap since it operates on summaries, not raw text.

Step 4: Streaming to the Frontend

Users abandon summarization tools that feel slow, even if the total time is reasonable. Streaming the response token-by-token makes a 10-second summary feel instant because text starts appearing in under a second.

const response = await fetch('https://api.anthropic.com/v1/messages', {
  method: 'POST',
  headers: {
    'x-api-key': process.env.ANTHROPIC_API_KEY,
    'anthropic-version': '2023-06-01',
    'content-type': 'application/json'
  },
  body: JSON.stringify({
    model: 'claude-opus-4-20250514',
    max_tokens: 1024,
    stream: true,
    messages: [{ role: 'user', content: prompt }]
  })
});

for await (const chunk of response.body) {
  // forward SSE chunk to the connected client via WebSocket or SSE
}

On the frontend, render each incoming delta into the summary panel as it arrives, rather than waiting for the full response.

Production Concerns

Once real users rely on this, a few things become non-negotiable: per-user API keys so you can track usage and enforce limits, retry/backoff logic for rate limits and transient errors, and visibility into token consumption per request so you can price your own plans sensibly (summarizing a 50-page contract costs very differently than a one-page memo).

This is where SubToAPI (https://subtoapi.app) is useful if you'd rather not build key management, usage metering, and streaming infrastructure from scratch. It turns your Claude access into an HTTPS API with application-level keys (sub_live_...), built-in streaming support, and usage metadata per request — so your summarization endpoint reports exactly how many tokens each document consumed, which you can feed straight into your own billing logic. Setup takes a few minutes: grab a key at /signup, follow /docs/quickstart, and swap your existing Claude calls for the same request shape described in /docs/messages. Streaming works the same way as above, documented at /docs/streaming, and if you later want the summarizer to call tools (e.g., fetch a document from a URL before summarizing), that's covered in /docs/tools. Plans start at Solo €9/month, with Team and Scale tiers for multi-seat setups — see /pricing.

questions

Can Claude summarize a 100-page PDF in one request? Not reliably as a single call, even with a large context window. The map-reduce pattern — summarize each section, then summarize the summaries — gives more consistent quality and lets you parallelize the first pass for speed.

Why does my summary lose detail compared to the source document? Vague prompts ("summarize this") produce vague output. Specify length, format, and focus (e.g., "list decisions and action items in 5 bullets") to get summaries that retain the details that matter for your use case.

Should I stream the summary or wait for the full response? Stream it. Users perceive streamed output as much faster even when total generation time is identical, and it lets you show progress on multi-chunk documents instead of a blank loading state.

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 →