Contexts
Managing state, memory, and behavior for agent interactions.
What is a Context?
A context is like a separate workspace for your agent. Think of it like having different tabs open in your browser - each tab has its own state and remembers different things.
Real Examples
Here are contexts that make agents stateful:
Chat Context
Game Context
Project Context
The Problem: Agents Need to Remember Different Things
Without contexts, your agent mixes everything together:
The Solution: Contexts Separate Everything
With contexts, each conversation/session/game stays separate:
How Contexts Work in Your Agent
1. You Define Different Context Types
2. Inputs Route to Specific Context Instances
3. Agent Maintains Separate Memory
Creating Your First Context
Here's a simple todo list context:
Use it in your agent:
Context Memory: What Gets Remembered
Context memory persists between conversations:
Multiple Contexts in One Agent
Your agent can switch between different contexts:
Advanced: Context-Specific Actions
You can attach actions that only work in certain contexts:
Now these actions only appear when the todo context is active!
Context Lifecycle
Contexts have hooks for different stages:
Best Practices
1. Design Clear Boundaries
2. Keep Memory Structures Simple
3. Write Helpful Render Functions
4. Use Descriptive Schema
Key Takeaways
- Contexts separate state - Each conversation/session/game gets its own memory
- Instance-based - Same context type, different instances for different users/sessions
- Memory persists - State is saved between conversations automatically
- LLM sees context - Render function shows current state to the AI
- Context-specific actions - Attach actions that only work in certain contexts
- Clear boundaries - Design contexts around specific tasks or domains
Contexts are what make your agent stateful and able to maintain separate conversations and tasks without mixing things up. They're the foundation for building agents that can remember and manage complex, ongoing interactions.