Schritt 8 von 13 - Etwa 30 Min.
Lab 07 - Long conversations
Time: about 30 minutes. You will: keep a conversation going for weeks without hitting the context window, using the API's server-side compaction and context editing.
Why
A study coach that you talk to every day builds up a long steward:main session. Every request
sends the whole history, so the context window fills up
(1M tokens on the current models): requests get slower and pricier, and in the end the model
stops with model_context_window_exceeded. Something has to shrink the context - and it
has to do it without breaking the append-only history from lab 02.
Concepts
Three tools for three problems
| Problem | Tool | Where it runs |
|---|---|---|
| Old tool results (file contents, program output) take up space | Context editing: clear old tool results | Server |
| The conversation itself is too long | Compaction: summarize earlier turns | Server |
| Facts must outlive the conversation | Memory (lab 06) | Your code |
Why server-side, not your own summarizer
The obvious approach is to summarize old messages yourself and replace them. With current models that is the wrong move: the API checks that earlier turns are sent back unchanged (lab 02), and replacing the middle of a history while keeping the turns after it breaks that check - the model loses its earlier thinking in every later turn. Server-side compaction and context editing do not count as edits: you keep sending the history exactly as it was, and the server decides what the model actually sees.
Compaction
With context_management.edits containing compact_20260112 (beta compact-2026-01-12), when a
request reaches the trigger (default 150,000 input tokens, at least 50,000), the API
compacts the conversation: it summarizes the earlier turns and
returns a compaction block at the start of the assistant's content:
{ "type": "compaction", "content": "Learner: CCA-F on 2027-01-15; weakest domain ..." }Your only job is rule 1 from lab 01: append the response content exactly as returned. The
block is stored in the session log like any other content and sent back on the next request; the
server uses it in place of everything before it. You can pass your own instructions for the
summary - for a study coach: keep the exam, the date, weak domains, and every score. They replace
the API's default summary prompt completely, so say everything the summary must do.
The summarizing step is billed, but its tokens are not in the response's top-level usage: the
API reports them as a compaction entry in usage.iterations. Steward adds that entry to the
turn's usage, so /usage and the daily limit (lab 12) include it.
Context editing
Context editing with clear_tool_uses_20250919 (beta
context-management-2025-06-27) removes old tool results from what the model sees once the
request passes a trigger (60,000 tokens here), keeping the most recent few. Steward excludes the
memory tool, whose results are small and important. Clearing is cheaper than compaction and keeps
the conversation itself intact, so it runs first, at a lower trigger. Each clearing changes the
prompt prefix, so the next request writes the cache again (lab 03).
Steward's settings
"context": {
"compaction": { "enabled": true, "triggerTokens": 150000, "instructions": "" },
"clearToolResults": { "enabled": true, "triggerTokens": 60000, "keepRecent": 4 }
}/usage shows how full the context is (contextTokens of the last request) next to the
compaction trigger.
When to start over instead
Compaction keeps one conversation alive. Sometimes a fresh one is better: a new topic, or a
conversation that went in circles. /new starts a new session; memory (lab 06) carries the facts
across.
Build
src/config/config.ts- thecontextsection, with a minimum trigger of 50,000 tokens.src/core/anthropic-model.ts-buildRequestParams()adds both edits and their betas when enabled.src/core/turn-loop.ts- nothing new to store: compaction blocks are appended like all content. Thecompactionstop reason (sent only when a request asks the server to pause after compacting, withpause_after_compaction; Steward does not, but handles it) continues the loop, andmodel_context_window_exceededends the turn with any open tool call closed.addUsage()adds the compaction step's tokens fromusage.iterations.
Run
Compaction needs a long conversation to trigger. To see it without chatting for days, lower the trigger in your local config to the minimum and feed Steward long notes:
"context": { "compaction": { "triggerTokens": 50000 } }you> Read every file in my notes folder in full and quiz me on each one.Keep going until /usage shows the context near 50,000 tokens, then check the log:
pnpm steward show | grep compactionThe assistant message that carries the [compaction] block is the point where earlier turns
were summarized. Put the trigger back afterwards.
Break it
Filling a real context window takes up to a million tokens: it is slow, it costs real money, and
the daily spending limit (lab 12) will usually stop you first. So break it offline: pnpm lab 07
runs "ends the turn cleanly when the context window is full" against the fake model. Read that
test, then remove the model_context_window_exceeded case from the stop-reason switch in
src/core/turn-loop.ts and run pnpm lab 07 again: the test fails, because the turn now ends
with stop other instead of context_window, so the terminal no longer tells you to start over
with /new. The open tool call is still closed - by the default branch, the safety net that
keeps the history valid for any stop reason the loop does not know. Put the case back.
If you want to see it live anyway, use a small model and a budget you accept: set model.main
to claude-haiku-5-5, turn compaction and clearing off ("enabled": false for both), and keep
asking Steward to read large files. The turn ends with "the conversation is too long; start a new
one with /new". Turn both back on afterwards. Haiku 5.5 charges more for prompts over 100,000
tokens ($0.50 input and $2.50 output per million instead of $0.10 and $0.50); the example price
table has that tier (longContext), so /usage and the daily limit count those requests at the
higher price.
Check
pnpm lab 07The checkpoint proves: requests carry both edits and their betas when enabled, custom
instructions pass through, nothing is sent when disabled, a too-small trigger is rejected, a
compaction block is stored exactly and sent back (also after a restart), the loop continues after
a compaction pause, the compaction step's tokens are counted, and a full context ends the turn
with a valid history.
Exercises
- Write compaction
instructionsfor the study coach and compare two summaries of the same long session: with and without them. - Show a warning in the terminal when
contextTokenspasses 80% of the compaction trigger. - Compaction on demand (beta
compact-2026-09-04) lets you compact at a moment you choose, such as the end of a study day. Read its docs and sketch a/compactcommand.
Claude API docs
Steward uses the API's own compaction and context editing instead of building a summarizer; compaction on demand is the other server-side option, and your own summarizer the third.
- Context windows - how the window fills up and what happens at the limit
- Compaction overview - compaction at a token threshold (this lab) and compaction on demand
- Context editing - clearing old tool results, and what it does to the prompt cache
- Stop reasons and fallback -
compactionandmodel_context_window_exceeded - Token counting - measure a conversation before you send it
- Effective context engineering for AI agents - why a smaller context gives better answers
Exam connection
CCA-F - Context Management & Reliability: context window limits, compaction versus context editing versus memory, and keeping long-running agents reliable. Practice this domain on CertLabPro, or on your phone with the CertLabPro app.
Previous: Lab 06 - Next: Lab 08 - Gateway and channels