AgentState Enum
Agents transition through various states during execution:State Descriptions
Idle
Meaning: Agent is waiting for input When:- After spawning
- After completing a task
- Between turns
Processing(when input received)Done(when shutdown)
Processing
Meaning: Agent is calling the LLM When:- After receiving user input
- During multi-turn conversations
- While waiting for LLM response
ExecutingTool(LLM wants to use a tool)WaitingForPermission(tool needs permission)WaitingForSubAgent(spawning subagent)WaitingForUserInput(asking user questions)Idle(text-only response complete)Done(task complete)Error(LLM call failed)
WaitingForPermission
Meaning: Tool execution requires user approval When:- LLM wants to use a tool
- No matching permission rule exists
- Interactive mode enabled
ExecutingTool(permission granted)Done(permission denied)Processing(error in permission handling)
- Tool name
- Action description
- Tool input
ExecutingTool
Meaning: Currently running a tool When:- Permission granted (or auto-allowed)
- Tool execution in progress
Processing(tool complete, continuing)Done(tool complete, task finished)Error(tool execution failed)
tool_name: Which tool is runningtool_use_id: Unique ID for this execution
WaitingForSubAgent
Meaning: Waiting for a spawned subagent to complete When:- Tool spawned a subagent
- Subagent is processing
- Parent is blocked
Processing(subagent completed)Error(subagent failed)
session_id: ID of the child agent
WaitingForUserInput
Meaning: Waiting for user to answer questions When:- Agent used
AskUserQuestiontool - Questions sent to frontend
- Awaiting responses
Processing(answers received)Done(interrupted)
request_id: ID to match request/response
Done
Meaning: Agent has completed all work When:- Task finished successfully
- User interrupted
- Permission denied
- Shutdown requested
Idle(if new input sent)- (Terminal state for this task)
Error
Meaning: Agent encountered an unrecoverable error When:- LLM API error
- Tool execution failure
- Internal error
- Invalid state transition
- (Terminal state)
Idle(if new input clears error)
message: Error description
State Transitions
Complete State Machine
Typical Flow Examples
Simple text response:Monitoring State
Polling State
Event-Driven Monitoring
State-Based UI Updates
React Example (TypeScript)
Tauri Backend
State Persistence
States are not persisted to disk. They are runtime-only:Convenience Methods
TheAgentHandle provides convenience methods:
Error States
Entering Error State
Agents enter error state when:-
LLM API fails:
- Network error
- API key invalid
- Rate limit exceeded
- Timeout
-
Tool execution fails:
- Tool threw exception
- Invalid tool input
- Tool not found
-
Internal errors:
- Session save failed
- Invalid state transition
- Channel closed unexpectedly
Recovering from Errors
State and Interrupts
Interrupt in Different States
During Processing:- Partial text preserved
- Incomplete thinking discarded
- All tool calls removed
- Current tool completes
- Remaining tools get “Interrupted” error
- Agent transitions to Done
- Tool gets “Interrupted” error
- No execution
- Agent transitions to Done
State Debugging
Enable State Logging
State History
Track state history for debugging:Best Practices
1. Always Handle Error State
2. Show State to Users
3. Don’t Poll Too Frequently
4. Use Event-Driven When Possible
5. Handle All States
Next Steps
Streaming & History
Understand the dual-channel pattern
Interrupt Handling
Learn about graceful cancellation
AgentState API
Complete state API reference
Error Handling
Advanced error handling patterns