States enable agents to maintain context, remember past interactions, and build upon previous work across multiple requests and sessions.
Overview
The States primitive represents the agent’s persistent memory and context management system. While sessions provide the container for conversations, states determine what information persists and how agents maintain context across interactions. This enables sophisticated workflows where agents can:- Remember Context: Maintain conversation history and relevant information across requests
- Build Incrementally: Continue work from previous steps without starting over
- Track Progress: Keep track of multi-step workflows and their current state
- Share Knowledge: Make information available across different parts of a workflow
- Maintain Variables: Store and retrieve data throughout the agent’s execution
Automatic Persistence
Message history persists automatically at no cost for all conversations
Environment State
Files, installed packages, and system state persist within session sandboxes
Session-Scoped
Each session maintains its own isolated state and context
Incremental Building
Agents build upon previous work without repeating completed steps
How State Works in Agentbase
Agentbase implements a two-tier state management model:1. Conversational State (Message History)
Automatic & Free: Every message in a session is automatically persisted:- What’s Stored: All user messages, agent responses, tool calls, and their results
- Cost: Zero - message history is always free
- Retention: Persists for the lifetime of the session
- Access: Automatically included in agent context for subsequent requests
2. Computational State (Environment)
On-Demand Creation: The sandbox environment state when agents perform operations:- Files Created: All files written persist in the session sandbox
- Packages Installed: Dependencies remain installed across requests
- System State: Environment variables, running processes, directory structure
- Browser State: Cookies, local storage, authentication sessions
Session Continuity: Both conversational and computational state are tied to session IDs. Reuse the same session ID to maintain all context.
Code Examples
Basic State Persistence
Multi-Step Workflow with State
Conversational Context
Stateful Development Workflow
State Lifecycle
State Creation
State is created automatically on the first request in a session:State Persistence
State persists throughout the session:State Pause and Resume
After 5 minutes of inactivity, the computational environment pauses:State Cleanup
Sessions eventually expire after extended inactivity:Use Cases
1. Iterative Development
Build software incrementally:2. Data Analysis Pipeline
Process data through multiple stages:3. Long-Running Support Sessions
Maintain context throughout customer interactions:4. Research and Compilation
Gather information progressively:5. Testing and Debugging
Iteratively debug and test code:6. Document Generation
Build documents incrementally:Best Practices
Session Management
Store Session IDs for Long-Running Workflows
Store Session IDs for Long-Running Workflows
Use Separate Sessions for Independent Tasks
Use Separate Sessions for Independent Tasks
Clean Up Temporary State
Clean Up Temporary State
State Organization
State Recovery
Handle Session Expiration
Handle Session Expiration
Create State Checkpoints
Create State Checkpoints
Performance Optimization
Reuse Sessions
Minimize cold starts by reusing sessions for related work
Batch Operations
Group related tasks in single session to leverage state
Clean State
Remove unused files to stay within disk limits
Preload Common
Install common dependencies once per session
Integration with Other Primitives
With Sessions
Sessions provide the container for state:With Sandbox
State exists within sandbox boundaries:With Computer Environment
System state persists across requests:With File System
Files are the primary persistent state:Performance Considerations
State Size
Monitor state size to avoid hitting limits:- Message History: Unlimited messages, but very long histories may impact performance
- File Storage: 10GB per session limit
- Memory Usage: 2-4GB depending on mode
Context Window Management
Very long message histories may need summarization:Cold Start vs. Warm Start
- Cold Start (new session): ~2-5 seconds overhead
- Warm Start (existing session): Instant, state already loaded
- Resumed (paused session): ~1-2 seconds to resume
Troubleshooting
State Not Persisting
State Not Persisting
Problem: Changes don’t persist between requestsSolution: Verify you’re using the same session ID
Session Expired
Session Expired
Problem: Session no longer availableSolution: Implement expiration handling and state recovery
Disk Space Exceeded
Disk Space Exceeded
Problem: Hit 10GB storage limitSolution: Clean up large files regularly
Conflicting State
Conflicting State
Problem: Previous state interfering with new tasksSolution: Start new session for unrelated work
Advanced Patterns
State Snapshots
Create snapshots of important state:State Migration
Move state between sessions:Conditional State Reset
Reset parts of state while keeping others:Related Primitives
Sessions
Container for state and conversations
Sandbox
Isolated environment hosting state
File System
Primary mechanism for persistent state
Computer
System state and environment persistence
Additional Resources
Persistence Guide
Understanding Agentbase’s persistence model
API Reference
Session and state parameters
Best Practices
Production patterns and tips