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
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.
Summarization
Periodically compress older messages into summaries. Preserves key information while reducing tokens.
context = [system, summary] + recent_messages
Priority-based Truncation
Assign priority scores to messages. System prompts and recent turns get highest priority.
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