When Agents Manage Their Own Context

For a long time, the standard approach to context limits in autonomous agents was blunt: pick a threshold, fire compaction when the agent crosses it, move on. The engineer sets the number; the agent has no say. That worked well enough when agents ran short tasks with a human watching. It starts to break down when the same agents run for hours, unsupervised, across a swarm of ten, fifty, or a hundred concurrent instances.

The failure mode has a name — context rot — and it is more insidious than a hard crash. As a context window fills, model performance degrades gradually, not all at once. An agent that was reasoning clearly at 40 percent capacity starts making worse decisions at 85 percent, long before it hits the wall. And on some provider pricing structures, crossing a specific token threshold (roughly 270,000 tokens for certain GPT models) nearly doubles the cost per request. Fixed auto-compaction set too late catches the crash but not the drift.

Letting the Agent Decide

A more capable pattern gives the agent a dedicated tool to compact its own context at any point it judges appropriate — not just when the harness fires. The agent treats compaction as a first-class action, the same way it treats reading a file or running a test.

One small addition makes this substantially more useful: a “note to self” field. When the agent calls the compact tool, it can write a short message — preserved intent, open questions, where it was in the plan — that gets injected back as a user prompt after summarization. The agent crosses the compaction boundary without losing its thread. Without something like this, compaction is a reset; with it, compaction is more like a pause.

Three Levels, Not One

Rather than a single threshold, a three-level system maps better to how agents should actually behave as context fills:

– A soft notice early on — an advisory the agent can read and continue without acting on.
– A warning deeper in — a sterner signal that the agent should begin wrapping up current work.
– A force compaction at the limit — the harness blocks all other tool calls until the agent complies.

This graduated structure matters because different tasks have different natural stopping points. An agent in the middle of writing a test suite is in a different position than one about to start a new subtask. Giving it early notice lets it finish a coherent unit of work before compacting, rather than being cut off mid-thought.

Custom prompts at each level reinforce the behavior — the agent gets different instructions depending on which threshold it has crossed, which is more useful than a single generic “compact soon” message.

Prompt Engineering That Holds Up

The same devlog surfaces a few prompt-engineering patterns that proved durable across all three model runs:

A “how you’re graded” section in every agent prompt. Rather than plain instructions, this section frames the task the way a rubric would — with explicit criteria the agent is evaluated against. Models follow rubric-style framing more reliably than instruction-style framing, likely because rubrics map more closely to the feedback structures used in training.

A “definition of done” section that tells the agent precisely when to stop. Without this, long-running agents tend to keep going — adding polish, running extra checks, exploring edge cases that were never requested. Explicit stopping conditions prevent this.

Instant failure conditions encoded directly in the prompt: specific actions the agent must never take (reading files outside its working directory, touching another agent’s app in a multi-agent run). Encoding these as hard failures, not suggestions, makes boundary violations far less likely.

What the Comparison Shows

Running three models against the same build prompt — plan, build, verify — produced a telling contrast. One model (running without self-compaction) failed completely at 98 percent context, unable to recover mid-task. The other two completed the task, but at very different resource profiles: one took roughly 50 minutes and consumed around 500,000 tokens; the other completed in roughly 21 minutes using about 136,000 tokens.

Neither result is a verdict on which model is “better” — the faster, cheaper run may not generalize to every task type. But the failure of the third model is a clear illustration of what happens when heavy reasoning traces accumulate without a recovery mechanism. Context management is not a tuning detail; for fully autonomous, long-running work it is as load-bearing as model choice.

Where This Points

The shift from “human in the loop” to “fully autonomous, hours-long” agentic work is not just a matter of better models. It requires agents that manage their own operational state — and context is the most basic kind of state there is. Self-compaction, with intent preservation and graduated thresholds, is one piece of that infrastructure. Expect the patterns here to become table stakes as agent runs get longer and swarms get larger.

Text summarized and optimized using Anthropic’s models and reviewed by a human.