Context Anatomy

Breaking down the structure of context windows and how agents manage information.

Understanding Agent Context

Agent context includes the system prompt, conversation history, tool definitions, and retrieved information. Managing this context efficiently is crucial for agent performance.

Think of context as the agent's working memory—everything it needs to understand the task and respond appropriately.

📊

Explore Context Layers

Click each layer to see example content

Context Window

7,750 / 8,192 tokens

850
1200
2500
3200
You are Claude, an AI assistant made by Anthropic.
You have access to tools for file operations.
Always be helpful, harmless, and honest.
Never execute dangerous commands.

Priority Order

When context exceeds the window limit, content is typically trimmed from the middle or oldest history first. System prompts and tool definitions have highest priority and are rarely truncated. Recent conversation turns are preserved to maintain coherence.

What Actually Gets Sent to the Model

messages = [
  // 1. System prompt (highest priority)
  { "role": "system", "content": "You are a helpful coding assistant..." },

  // 2. Conversation history
  { "role": "user", "content": "Help me fix this bug" },
  { "role": "assistant", "content": "I'll read the file first" },

  // 3. Tool calls and results
  { "role": "assistant", "tool_calls": [{"name": "read_file"}] },
  { "role": "tool", "content": "def buggy_function():..." },

  // 4. Latest user message
  { "role": "user", "content": "Thanks, what was the issue?" },
]

tools = [
  { "name": "read_file", "description": "Read file contents", ... },
  { "name": "write_file", "description": "Write to a file", ... },
]

Context Components

📋

System Prompt

Defines the agent's role, capabilities, and behavioral guidelines.

🔧

Tool Definitions

Descriptions of available tools and how to use them.

💬

Conversation History

Previous messages, tool calls, and their results.

📚

Retrieved Information

External knowledge fetched during the conversation.

Context Management Strategies

Sliding Window

Keep the most recent N messages. Simple but may lose important early context.

messages = messages[-MAX_MESSAGES:]

Summarization

Periodically compress older messages into summaries. Preserves key information while reducing tokens.

summary = llm("Summarize this conversation: " + old_messages)
context = [system, summary] + recent_messages

Priority-based Truncation

Assign priority scores to messages. System prompts and recent turns get highest priority.

priority: system > tools > recent_user > recent_assistant > old_history

Common Pitfalls

Context Overflow

Exceeding the context window causes truncation. The model loses access to earlier information, potentially forgetting instructions or important context.

Tool Definition Bloat

Too many tools or overly verbose descriptions eat into context space. Keep tool definitions concise and only include tools relevant to the task.

Lost in the Middle

Models pay less attention to information in the middle of long contexts. Place critical information at the start or end.

Stale Retrieved Data

RAG results from earlier in conversation may become outdated as discussion evolves. Refresh retrieved data when the topic shifts.

Key Takeaways

  • 1Context management is key to agent reliability
  • 2Prioritize recent and relevant information
  • 3Tool definitions should be clear and unambiguous
  • 4Summarization helps maintain context over long sessions