ContextCompressor that summarizes the active memory near the limit and archives the raw transcript — the agent keeps running without manual pruning.
When to use it
max_loops="auto"or any long-running iterative task.- Agents that produce or consume large tool outputs.
- Multi-session agents whose
MEMORY.mdwould otherwise grow unbounded.
How it fires
Compression runs when all of the following hold:context_compression=Trueon the agent- Token usage of
agent.short_memory≥threshold * context_length - The agent is at a check point: the top of each subtask iteration with
max_loops="auto", or the top of each loop with an integermax_loops
threshold is 0.9 — compression fires at ~90% of the context window. context_length defaults to the model’s input window, so set it lower if you want compression to start sooner.
Default behavior
Compression is enabled by default. Just construct the agent normally:- Summarizes the conversation with an LLM call.
- With
persistent_memory=True, copiesMEMORY.mdtoarchive/history_<timestamp>.md, then wipes it and re-seeds it with the summary as a singleSystemmessage. - Rebuilds
agent.short_memory(system prompt + rules + summary). - In an autonomous run, rebuilds the request as the summary plus the prompt for the subtask in progress.
archive/. Compression does not run during the planning or final-summary phases of an autonomous run.
Tune the compressor
Swap the defaultContextCompressor after construction to change the threshold, summarizer model, or summary length:
threshold for agents with large tool outputs so you compress before any single iteration overflows.
Manual compaction
You can compact memory yourself at any time — useful after a clear milestone (research phase done, plan finalized):Disable compression
When you want the activeMEMORY.md to keep the raw transcript intact:
What ends up on disk
After compaction, withpersistent_memory=True:
agent_name is constructed, Swarms loads the compact summary from MEMORY.md into agent.short_memory. The archive is preserved for forensics but is never loaded.
Tips
- Keep compression on for autonomous loops; the cost of one summary call is small versus a context-overflow failure.
- Lower
threshold(0.6–0.75) for agents that emit long structured outputs. - Use a cheaper
summarizer_model(Haiku) to keep compaction lightweight. - Compact manually at major milestones to lock in important state with a hand-written summary.
See also
- Persistent Memory — How
MEMORY.mdis created and reloaded. - Agent Memory Reference — Full lifecycle, archive layout, and design rationale.
- Autonomous Mode — How compression fits into the
max_loops="auto"loop. - Conversation API —
compact,export, and search helpers.