Claude API Summarization Tool Tutorial
Building a summarization tool with the Claude API means sending text to a model endpoint with a prompt that instructs it to condense content, then handling the response in your app. This tutorial walks through the whole process: setting up the request, designing a prompt that produces consistent summaries, handling documents longer than the context window, and structuring the output so it's usable in production.
Summarization is one of the most common LLM use cases — meeting notes, support tickets, legal documents, articles, PDFs — and Claude models handle it well because of their large context windows and strong instruction-following. The hard part isn't calling the API once; it's making the tool reliable across different document lengths, formats, and summary styles.
What You Need Before Starting
- An API key for Claude (directly from Anthropic, or through a gateway like SubToAPI if you want a simpler HTTPS interface with usage tracking built in)
- A way to chunk long documents (for anything beyond a single context window)
- A clear definition of what "good summary" means for your use case: length, tone, bullet points vs prose, extractive vs abstractive
Skipping that last point is the most common mistake. "Summarize this" without constraints gives inconsistent results — some runs produce three sentences, others produce half a page.
Step 1: Basic Summarization Request
Start with a single-document request. Here's a minimal example using curl against an API that follows the standard messages format:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 300,
"messages": [
{
"role": "user",
"content": "Summarize the following support ticket in 3 bullet points, focusing on the customer issue and requested resolution:\n\n[TICKET TEXT HERE]"
}
]
}'
The key design choice here is constraining the output format directly in the prompt: "3 bullet points," "focus on X." Vague instructions produce vague summaries.
Step 2: Design a Reusable Prompt Template
For a production tool, you want a template function rather than hand-written prompts per request:
function buildSummaryPrompt(text, { style = "bullets", maxPoints = 5, focus = null }) {
const formatInstruction = style === "bullets"
? `Provide exactly ${maxPoints} bullet points.`
: `Write a single paragraph of no more than 100 words.`;
const focusInstruction = focus
? `Focus specifically on: ${focus}.`
: "";
return `Summarize the following text. ${formatInstruction} ${focusInstruction}
Do not include information not present in the source text.
TEXT:
${text}`;
}
That "do not include information not present in the source text" line matters — it reduces the model inventing details, which is the main risk in summarization tasks. If your use case requires factual grounding (legal, medical, financial text), add an explicit instruction to flag uncertainty rather than guess.
Step 3: Handle Documents Longer Than the Context Window
For most single articles or support tickets, you won't hit context limits. But for transcripts, long reports, or batches of documents, you need a chunking strategy:
- Split the document into chunks (by paragraph or section, not arbitrary character counts — breaking mid-sentence hurts summary quality).
- Summarize each chunk independently with the same prompt template.
- Combine the chunk summaries into a single input and run a second summarization pass over them.
async function summarizeLongDocument(chunks, summarizeFn) {
const chunkSummaries = await Promise.all(
chunks.map(chunk => summarizeFn(buildSummaryPrompt(chunk, { style: "bullets", maxPoints: 3 })))
);
const combined = chunkSummaries.join("\n\n");
return summarizeFn(buildSummaryPrompt(combined, { style: "paragraph" }));
}
This two-pass "map-reduce" approach is the standard way to summarize content that exceeds a single context window, and it works well for transcripts, multi-chapter documents, and log files.
Step 4: Make the Output Structured, Not Just Readable
If the summary feeds into another system (a dashboard, a CRM note, a Slack message), ask for JSON instead of free text:
{
"summary": "...",
"key_points": ["...", "..."],
"sentiment": "neutral"
}
Instruct the model explicitly to return only valid JSON and nothing else, and validate the response before using it downstream. This is also where Claude's tool use / structured output capabilities are worth exploring if your tool needs guaranteed schema compliance — see /docs/tools for details on defining input schemas the model must follow.
Step 5: Stream for Long Summaries in UI Contexts
If you're summarizing long documents and displaying the result live in a UI, streaming the response token-by-token gives users immediate feedback instead of a long wait. This is a separate implementation concern from the summarization logic itself — see /docs/streaming for how to wire up server-sent events against a Claude-backed endpoint.
Where SubToAPI Fits In
If you're prototyping a summarization tool and don't want to manage Anthropic console setup, key rotation, or usage tracking yourself, SubToAPI turns your existing Claude access into a standard HTTPS API with application keys (sub_live_...), streaming support, and per-key usage metadata — useful if your summarization tool has multiple internal services or team members calling the same model. Plans start at €9/month for solo use, with team seats at €19 and scale seats at €49. Check /pricing for details, or jump straight into the /docs/quickstart to get a working request in minutes.
Common Pitfalls
- No length constraint → inconsistent summary sizes across runs.
- No grounding instruction → the model occasionally adds plausible-sounding but invented details.
- Chunking mid-sentence → loses context and produces disjointed chunk summaries.
- No validation on structured output → downstream code breaks when the model returns almost-JSON.
Questions
Can Claude summarize an entire PDF in one request? Only if the extracted text fits within the model's context window. For long PDFs, extract text first, then use the chunking approach described above.
How do I keep summaries consistent in length? Specify an exact constraint — "3 bullet points" or "under 100 words" — rather than relative terms like "briefly." Exact constraints produce far more consistent output across calls.
Should I use streaming for a summarization tool? Only if you're displaying results live in a UI. For backend batch processing or API responses consumed by other systems, a standard non-streaming request is simpler and sufficient.