Skip to content
Flows
Implement Context Compaction
Memory & Context90-120 minutes
0/4 steps0%

Step 1 of 4

Add token accounting

You cannot manage what the loop does not measure.

Prompt capsule

Instrument the agent loop with context accounting. Requirements: before every model call, compute the token count of the assembled context (system prompt + history + reminders) using the model's tokenizer or a reliable estimator; log it per turn with a breakdown (system, history, tool results, reminders); expose current usage as a fraction of the model's window; add a warning log at 60% and a trigger event at the compaction threshold (default 75%, configurable). Verify by running a file-heavy session and watching the per-turn accounting climb in the logs.

Paste into Claude · Complete implementation prompt with explicit requirements

Expected after this step

Per-turn context accounting with threshold events.

Should not happen

  • Compacting everything including the task itself, leaving an agent with amnesia
  • Triggering at 100% so compaction itself fails for lack of room
  • Freeform prose summaries that drop file paths, decisions, and constraints
  • Summarizing tool results the agent still needs at full fidelity

Verify before continuing

Do not move on until every check is true. The complete button stays locked until then.

Do not continue if…

  • !Compacting everything including the task itself, leaving an agent with amnesia
  • !Triggering at 100% so compaction itself fails for lack of room
  • !Freeform prose summaries that drop file paths, decisions, and constraints
  • !Summarizing tool results the agent still needs at full fidelity

If the AI messes this up

Use this when the AI fakes progress or breaks the feature. It forces a real fix.

Counts are wildly off from API-reported usage. Switch to the provider's tokenizer for your model (or calibrate your estimator against API usage fields across 10 real calls) before trusting the trigger.

Your notes for this step