Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Context Management

The SDK provides several mechanisms to keep agent context clean and within limits, even during complex multi-step investigations.

Smart Compaction

When context approaches the limit, the SDK performs structured state snapshots that preserve key findings while discarding intermediate reasoning and raw data.

What's preserved:

  • Key findings and conclusions
  • Active hypotheses
  • Entities and relationships discovered
  • User preferences from the session

What's discarded:

  • Intermediate reasoning chains
  • Raw tool outputs already summarized
  • Superseded hypotheses

Tool Output Masking

A backward-scan FIFO mechanism prunes bulky tool outputs while protecting recent context. Older tool responses are truncated or removed when newer, more relevant data arrives.

The mask operates on a priority system:

  1. Recent tool outputs are protected
  2. Older outputs with summaries already incorporated are candidates for removal
  3. Large raw JSON responses are first to be pruned

Eager Knowledge Loading

All knowledge documents in amodal/knowledge/ are loaded into the system prompt at session start. No on-demand fetch, no "which doc should I load" decision turn — the agent sees approved policy and terminology from turn one.

System prompt: [knowledge docs inline]
  amodal/knowledge/support-policy.md
  amodal/knowledge/product-glossary.md
  amodal/knowledge/escalation-paths.md
  amodal/knowledge/response-style.md

Sub-agents share the parent's context compiler, so they also start with full knowledge loaded.

This works best when knowledge files are concise. If your knowledge base starts to crowd the context window, split behavior by session type or move structured facts into stores.

Loop Detection

The SDK detects when an agent enters unproductive loops:

  • Pattern matching — repeated identical tool calls or reasoning patterns
  • LLM-based detection — the model evaluates whether it's making progress

When a loop is detected, the agent is nudged to try a different approach or escalate to the user.