FrugentSign in ↗

Token economics

How to control context growth in long-running AI agents

Keep long-running agents within a useful context budget. Learn what to retain, summarize or retrieve again without losing permissions, task state or evidence.

Context grows as an agent accumulates messages, tool results and earlier decisions. Keeping everything is simple until each new step carries a large amount of material that no longer helps. The goal is a useful working context, rather than the shortest possible prompt.

Classify information before removing it

Separate durable instructions, current task state, recent observations and historical evidence. Permissions and safety constraints belong in the durable group. A current account identifier or an uncertain action outcome may be essential even when it looks like a small detail.

Large completed tool outputs are candidates for summarization or later retrieval. A navigation menu from ten steps ago is often less useful than the result of the last action. Treat that as a hypothesis to test on your workload, not a universal deletion rule.

Summarize state, not confidence

A useful checkpoint names the objective, completed steps, remaining work, active account and known uncertainties. Preserve the difference between an attempted action and a confirmed result. A summary that turns “submitted, outcome unknown” into “completed” can trigger a duplicate action later.

Retain references to evidence that can be fetched again. If the underlying page can change, mark which facts need a fresh check instead of treating an old summary as live data.

Measure the transition cost

Claude documents context editing as a way to manage conversation content. Availability and configuration vary, so consult the documentation for the model and API in use. Application-managed summaries are a separate design choice and need their own validation.

Compaction or editing may change cache reuse. Measure the cost of producing a summary, the next request after the transition and subsequent requests. A lower cache-hit rate can coexist with a lower bill when the new context is much smaller.

Evaluate difficult recoveries

Test a resumed task after a login change, a stale page, a failed tool call and an interrupted submission. Check whether the agent remembers what it is allowed to do and what still needs confirmation.

Set a context budget, but pair it with a correctness gate. If a compacted agent loses a critical constraint, saving tokens has made the system worse. Keep the original trace in appropriately protected storage when you need it for evaluation, rather than in every new model request.

A checkpoint you can test

A checkpoint might read: “Goal: download this account’s monthly report. Confirmed: correct account and report month selected. Attempted: download click. Unknown: whether a file was created. Next: inspect downloads before clicking again. Authority: read-only task; do not change account settings.” Use synthetic data to test this pattern. Keep live account identifiers private and preserve evidence references outside the model’s repeated working context.

Sources and further reading

Keep exploring

Compare local plans · Get started

Why your prompt cache misses: a debugging checklist

Diagnose prompt-cache misses caused by changing prefixes, model settings, cache boundaries and timing. Verify reuse from usage rather than repeated wording.

How to reduce AI agent token costs in browser automation

Reduce AI browser-agent costs with stable context, smaller tool results, reusable plans and bounded retries. Includes token math and a success-based cost metric.

How to design safe cache keys for AI applications

Build cache keys around account scope, permissions, versions and freshness. Avoid reusing a plausible AI answer for the wrong user or task.