Skip to main content

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
Transitions to:
  • 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
Transitions to:
  • 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
Transitions to:
  • ExecutingTool (permission granted)
  • Done (permission denied)
  • Processing (error in permission handling)
Payload:
  • Tool name
  • Action description
  • Tool input

ExecutingTool

Meaning: Currently running a tool When:
  • Permission granted (or auto-allowed)
  • Tool execution in progress
Transitions to:
  • Processing (tool complete, continuing)
  • Done (tool complete, task finished)
  • Error (tool execution failed)
Payload:
  • tool_name: Which tool is running
  • tool_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
Transitions to:
  • Processing (subagent completed)
  • Error (subagent failed)
Payload:
  • session_id: ID of the child agent

WaitingForUserInput

Meaning: Waiting for user to answer questions When:
  • Agent used AskUserQuestion tool
  • Questions sent to frontend
  • Awaiting responses
Transitions to:
  • Processing (answers received)
  • Done (interrupted)
Payload:
  • request_id: ID to match request/response

Done

Meaning: Agent has completed all work When:
  • Task finished successfully
  • User interrupted
  • Permission denied
  • Shutdown requested
Transitions to:
  • 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
Transitions to:
  • (Terminal state)
  • Idle (if new input clears error)
Payload:
  • message: Error description

State Transitions

Complete State Machine

Typical Flow Examples

Simple text response:
Tool execution:
Multiple tools:
With subagent:
With questions:

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

The AgentHandle provides convenience methods:

Error States

Entering Error State

Agents enter error state when:
  1. LLM API fails:
    • Network error
    • API key invalid
    • Rate limit exceeded
    • Timeout
  2. Tool execution fails:
    • Tool threw exception
    • Invalid tool input
    • Tool not found
  3. 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
During ExecutingTool:
  • Current tool completes
  • Remaining tools get “Interrupted” error
  • Agent transitions to Done
During WaitingForPermission:
  • Tool gets “Interrupted” error
  • No execution
  • Agent transitions to Done
See Interrupt Handling for details.

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