Long-running agents accumulate transcripts that eventually exceed the model’s context window. Swarms ships aDocumentation Index
Fetch the complete documentation index at: https://docs.swarms.world/llms.txt
Use this file to discover all available pages before exploring further.
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 at the top of a loop iteration when all of the following hold:context_compression=Trueon the agent- Token usage of the active prompt ≥
threshold * context_length - The agent is at the start of an iteration (not mid tool-call)
threshold is 0.9 — compression fires at ~90% of the context window.
Default behavior
Compression is enabled by default. Just construct the agent normally:- Summarizes the current transcript with an LLM call.
- Copies
MEMORY.mdtoarchive/history_<timestamp>.md. - Wipes
MEMORY.mdand re-seeds it with the summary as a singleSystemmessage. - Rebuilds
conversation_history(system prompt + rules + summary).
archive/.
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:MEMORY.md — the archive is preserved for forensics but does not enter the active context.
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.
- Conversation API —
compact,export, and search helpers.