Context Management
The runtime builds a system prompt from agent source and supplies conversation history to the model. Reference loading, tool-result clearing, and compaction help keep requests within the model's context window.
Instructions and current app context
AGENT.md supplies operating instructions. Its template values are rendered when the session is built. Put facts that change between turns in a sibling CONTEXT.md; that template is rendered from the current request's context before each model call.
A non-empty basePrompt in amodal.json overrides the normal compiled prompt. Use it only when you intend to supply that prompt yourself. See Agents and Configuration.
Indexed knowledge loading
Documents in amodal/knowledge/ contribute a name and description to the prompt. Their bodies are read through load_knowledge when needed.
Searchable corpora use knowledge_search and knowledge_read. These tools let an agent locate and read sections without loading the whole corpus. See Knowledge Base.
A scoped agent or subagent needs the relevant tools in its declared resources. Write specific document descriptions so it can select the right reference.
Tool output masking
Clearing runs when estimated context is past compactThreshold and there are more than clearThreshold tool results. The defaults are a 90% context threshold and 15 results.
The most recent 5 results remain intact. Older results become short summaries when a summarizer is configured, or cleared-result markers otherwise. Selection is by recency, not response size.
Compaction
When estimated context exceeds the threshold, the runtime can summarize older conversation turns with the session's model. By default, it keeps the latest 6 user turns intact and limits the summary to 4,000 tokens.
The summary asks for the current task, key data, actions taken, errors, and next steps. It replaces older messages in the model's working context. Exact wording and tool-result details can be lost, so save important structured facts to a store.
Compaction emits compaction_start and compaction_end events. If summarization fails, the runtime continues with the existing history. After 3 consecutive failures, the compaction circuit breaker skips further attempts for that loop context.
Loop detection
Loop detection is disabled by default (maxToolRepeats: 0). When enabled, it scans the latest 16 messages for repeated calls to the same tool with similar parameters, ignoring pagination keys such as offset, limit, and cursor.
Detection requires at least 3 matching calls, even if the configured limit is 1 or 2. Reaching the limit ends the turn with reason: "loop_detected". A later user message can start another turn.