Guides  /  State and memory

How do you stop context from growing without bound?

Answer

An agent that runs for a long time accumulates history, and every token of that history is re-sent on each model call. Cost per turn rises with the length of the conversation instead of the difficulty of the question.

Compaction decides what the agent carries forward. Recent turns stay verbatim and older material is summarised, with the detail behind the summary still retrievable from the journal.

The mechanism

Token cost scales with the length of the history.

A model call is priced on the tokens it receives. An agent that appends every turn to its context sends the whole conversation again on each call, so the tenth turn costs several times the first even when the question is simpler.

The naive fixes both lose something. Truncating the oldest turns discards facts the agent needs later. Raising the context window pays for the growth instead of stopping it, and the growth continues.

What compaction preserves

The choice is what stays verbatim.

Recent turnsHeld in full, because the immediate exchange is what the next response depends on.
Durable factsExtracted and kept as state rather than as conversation, so a decision made in turn two survives to turn fifty.
SummariesOlder stretches compressed to their conclusions, with the tokens they replace no longer re-sent.
The full recordEverything remains in the journal. Compaction changes what the model is sent, and it does not delete history.
What this changes

Long-running agents stay affordable.

Short interactions hide this entirely. A three-turn exchange never accumulates enough history for the growth to show, which is why the problem appears after a prototype becomes a product.

An agent that holds a customer relationship over weeks needs its cost per turn to stay flat. Compaction plus durable state keeps the recall while the token count stops tracking the length of the history.

Questions

Related questions.

What happens to a fact stated fifty turns ago?

Facts extracted into durable state stay available without being re-sent. The agent reads them from state, and the model receives them only when they matter to the current turn.

Does summarising lose accuracy?

A summary carries less than the original, which is why recent turns stay verbatim and the full record stays in the journal. What the model receives is a working set, and the evidence record is complete.

Can the agent go back and read the detail?

The journal holds every event, so the detail behind a summary is retrievable when the agent or a person needs it.

How much does this actually save?

The saving scales with conversation length. Swiggy reduced token consumption by 22% alongside its latency work, and the figure depends on how much history a given agent carries.

On Akka. Memory compaction and durable state are capabilities of the Akka SDK runtime, and token reporting is part of Akka Optimize.