Skip to content
bits
Context is everything · Lesson 4 of 7

When the chat gets long

Tutor:

The API has no memory, so a chat works by re-sending the whole history every turn. Each new message rides on top of everything before it:

import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const messages: Anthropic.MessageParam[] = [];
export async function chat(text: string) {
messages.push({ role: "user", content: text });
const res = await client.messages.create({
model: "claude-haiku-5-5",
max_tokens: 1024,
messages, // the whole history, every turn
});
messages.push({ role: "assistant", content: res.content });
console.log(`input_tokens=${res.usage.input_tokens}`);
return res;
}
TypeScriptUTF-8● lines 12–12: highlighted
Tutor:

Run a few turns and the input count only goes one way. This is example output, not a live run:

OUTPUT
npx tsx chat.ts
turn 1 input_tokens=38
turn 2 input_tokens=512
turn 3 input_tokens=1,047
turn 4 input_tokens=1,566
example output · input grows every turn
Tutor:

Do the sum for a longer chat. If each turn (question plus reply) adds about 500 tokens, turn 20 sends about 10,000 tokens of input. Add up all 20 turns and you've paid for roughly 105,000 input tokens, because the early turns were re-sent nineteen times. Long chats also run into the last lesson's problem: an old tangent stays in the window, competing for attention.

Tutor:

Three ways to keep it in check.

Trim. Drop the oldest turns. Simple, but anything said early (the user's name, a key decision) silently disappears.

Summarise. Replace older turns with a short summary and keep the recent turns word for word. Anthropic's API can do this for you: compaction (in beta, October 2026) has Claude write the summary on the server, either when you ask or when the input passes a token threshold you set. Claude Code does the same when its context fills: it keeps decisions, open bugs and details, and drops repeated tool output.

Clear what's spent. In agents, old tool results are often the bulk of the history. Anthropic's context editing can clear them by rule. You'll meet tools properly in Give your model tools.

The art is in what the summary keeps. Too aggressive, and a detail that turns out to matter later is gone. So a good summary prompt says what to keep and what to drop.

quick check+10 neurons
Tutor:

A trip-planning chat is 60 turns long. Which detail must the summary keep?

    Type your answer or tap an option · Enter checks

    Tutor:

    Your turn. Write the summary prompt for this chat. Say what to keep, what to drop, and how long the summary can be.

    Your turn

    Run your prompt and the replies land here.

      • Asks for a summary, not yet
      • Says what to keep, not yet
      • Says what to drop, not yet
      • Sets a length limit, not yet
      • Includes the conversation, not yet
      • Ran at 1.0, not yet
      run at
      ~0 tokens

      Turn on JavaScript to run prompts.