Skip to main content

Overview

“Context” is all the information Zeus sends to the model on every Agent invocation. It is bounded by the model’s context window (token limit). At a high level, the Context is composed of:
  • System Prompt (built by Zeus): layered prompts, tool descriptions, skill metadata, runtime info, user profile & memories
  • Conversation History: user + assistant messages from the current session
  • Tool Calls / Results: tool invocation parameters and return values
  • Attachments: user-uploaded files and sandbox data
Context is not the same as Memory. Memory can be persisted to disk and loaded later, while Context is the real-time content within the current model window. See Memory for the full memory architecture.

Architecture

Zeus assembles the Context through BaseService._build_system_prompt(), with each component maintained independently and combined in a fixed order.

System Prompt

Detailed breakdown of each module (CORE, SOUL, TOOLS, WORKFLOW, etc.) and how they are assembled

Memory

Four-layer memory model, profile generation, and how memories are injected into the prompt

What Counts Toward the Context Window

Everything sent to the model counts toward the context window:

System Prompt Assembly

The System Prompt is the largest and most complex component of the Context. It is rebuilt by BaseService._build_system_prompt() on every Agent invocation. The assembly follows a fixed 15-step pipeline — loading layered prompt modules, injecting dynamic content (time, skills, connectors, MCP prompts, resources), and appending personalization data (profile and memories).

System Prompt — Full Module Breakdown

How each module (CORE, SOUL, TOOLS, WORKFLOW, MEMORY, MODE) is structured, how tool descriptions are injected, and how mode prompts control Agent behavior

Tool Description Injection

TOOLS.md contains a {tools_description} placeholder that is dynamically filled at build time. The system iterates over all enabled tools, extracts their names, descriptions, and parameter schemas, and generates a formatted tool description list. Tools are loaded by category, each with different enablement conditions:

Profile & Memory Injection

At the end of the System Prompt assembly, Zeus fetches the user’s profile and semantically relevant memories from the Memory system, then appends them as structured text. This allows the Agent to be aware of user preferences, project context, and historical information without explicit retrieval.

Memory — Retrieval & Ranking Details

How memories are stored, retrieved, ranked, and the complete Memory Gate pipeline

Conversation History

How It’s Built

Conversation history is constructed by _build_chat_history(), converting raw messages into LangChain format:

Truncation Strategy

Messages beyond the 30-message limit are removed by truncation. Automatic summarization is handled by SummarizationMiddleware (see below).

Token Management

Model Profiles

Zeus ships with 200+ model token-limit configurations. Common examples: When a model is not in the predefined list, a default of 64,000 tokens is used.

SummarizationMiddleware

When context approaches the model’s token limit, SummarizationMiddleware (provided by the DeepAgents framework) automatically triggers conversation history summarization:

Prompt Caching

For Anthropic models, AnthropicPromptCachingMiddleware enables prompt caching to reduce token billing for repeated System Prompt content. The caching mechanism is natively supported by the Anthropic API, caching the static portions of the System Prompt (CORE, SOUL, etc.) and significantly reducing token consumption across multi-turn conversations.

Complete Data Flow

The following diagram shows the full lifecycle of Context in a single Agent invocation — from assembly to consumption:

Context Changes by Phase


Mode-Based Context

Different modes produce different Context compositions: Ask and Plan modes reduce context consumption by limiting the number of available tools, which in turn reduces Tool Schema size.

Optimization Strategies

1. Progressive Disclosure

Problem: Skills and Connector prompts can be very long; injecting all of them wastes context space. Solution: Only inject metadata summaries; the Agent loads full content on-demand via tools.

2. Agentic RAG (On-Demand Retrieval)

Problem: Knowledge base content can be massive; pre-injection is impractical. Solution: The knowledge base is exposed as a tool. The Agent decides when and what to retrieve.

3. Automatic Summarization (SummarizationMiddleware)

Problem: Long conversations cause history to consume large amounts of context space. Solution: When token usage hits the threshold, older messages are automatically compressed into summaries.

4. History Truncation

Problem: Session messages grow without bound. Solution: Only the most recent 30 messages are kept, combined with LangGraph Checkpoint for full history persistence.

5. Prompt Caching

Problem: The System Prompt is mostly unchanged across turns but is billed every time. Solution: Anthropic models use AnthropicPromptCachingMiddleware to cache static portions.

6. Memory Formatting Limits

Problem: Too many retrieved memories can consume excessive space. Solution: Limit formatted output to max 2000 characters, Top K = 5.