Study 70 files ยท 12 subfolders
Copy to Workspace 11_RUN_STATE Shared from "Study" on Inkdown
Run State - Comprehensive Deep Dive
Overview
RunState is the serialization mechanism that enables pausing, resuming, and inspecting agent runs. Think of RunState as a "save point" or "checkpoint" in a video game - it captures the complete state of an agent run at any moment, allowing you to pause execution, save that state, and resume it later. This is essential for human-in-the-loop workflows, long-running tasks, and debugging.
Core Concepts
What is RunState?
RunState is a complete snapshot of an agent run that includes:
Current agent - Which agent is currently active
Conversation history - All messages and tool calls so far
Model responses - History of model responses
Tool use tracker - Which tools have been used
Guardrail results - Results of guardrail checks
Session state - Session-related information
Sandbox state - Sandbox execution state (if applicable)
basic-ques
Interruptions - Pending human approvals
Error details - Any errors that occurred
Context - User-provided context object
Trace state - Tracing information
Why RunState Matters
Human-in-the-Loop - Pause for human approval and resume
Long-Running Tasks - Handle tasks that span hours or days
Debugging - Inspect state at any point
Reproducibility - Replay exact execution paths
Error Recovery - Resume from failures
State Inspection - Understand what the agent is doing
RunState Structure
RunState Class
Schema Versioning
CURRENT_SCHEMA_VERSION The SDK uses schema versioning to handle changes in RunState format:
SCHEMA_VERSION_SUMMARIES Each schema version has a summary:
Schema Compatibility The SDK checks schema compatibility:
RunState Serialization
to_json() Serialize RunState to JSON:
Agent metadata (name, instructions, etc.)
All run items
Model responses
Tool use tracker
Guardrail results
Trace state
Sandbox state (if applicable)
Context (if serializer provided)
Run configuration
from_json() Deserialize RunState from JSON:
All serialized fields
Agent is reconstructed from metadata
Context is deserialized (if deserializer provided)
Context Serialization Custom context requires serializers:
RunState and Human-in-the-Loop
Pausing for Approval RunState enables approval workflows:
Interruption Handling RunState captures interruptions:
RunState and Resumption
Resuming from State Resume a run from a saved state:
Resumption Behavior
The current agent is restored
Conversation history is preserved
Tool use tracker is restored
Pending approvals are handled
Session state is maintained
Sandbox state is restored (if applicable)
Resumption with New Input You can provide new input when resuming:
RunState and Sessions
Session-Aware RunState RunState includes session information:
Session Resumption Resume with session context:
RunState and Sandbox
Sandbox State in RunState Sandbox state is included:
Sandbox Resumption Resume with sandbox state:
RunState and Errors
Error Details RunState captures error details:
Error Recovery Recover from errors using RunState:
RunState Inspection
Inspecting Conversation History
Inspecting Tool Usage
Inspecting Guardrail Results
Inspecting Model Responses
RunState and Tracing
Trace State RunState includes trace state:
Resuming with Trace Trace is continued when resuming:
RunState Best Practices
1. Serialize Context Properly Always provide serializers for custom context:
2. Handle Schema Versions Be aware of schema version changes:
3. Clean Up Old States Implement cleanup for old states:
4. Validate State Before Resumption Validate state before resuming:
5. Encrypt Sensitive States Encrypt sensitive state data:
Common RunState Patterns
1. Approval Workflow
2. Long-Running Task
3. Error Recovery
4. Debugging
5. State Sharing Share state across processes:
RunState and Version Migration
Migrating Between Versions When the schema changes, implement migration:
Backward Compatibility Maintain backward compatibility:
RunState Performance
State Size RunState can be large for long conversations:
Optimizing State Size
State Compression Compress state for storage:
Summary RunState enables pausing and resuming agent runs. Key takeaways:
RunState is a complete snapshot of an agent run
Serialization enables state persistence
Schema versioning handles format changes
Context serialization requires custom serializers
Human-in-the-loop via approval workflows
Resumption continues from saved state
Session integration maintains conversation context
Sandbox state preserves execution environment
Error details capture failure information
Inspection enables debugging
Tool use tracker monitors tool usage
Guardrail results capture validation outcomes
Model responses track LLM interactions
Trace state preserves tracing information
Validation before resumption
Encryption for sensitive data
Cleanup prevents storage bloat
Migration handles schema changes
Compression reduces storage size
Performance considerations for large states
RunState is essential for building robust, resumable, and debuggable agent workflows.