ADR-001: Agent Loop Design
Status
Accepted
Context
The agent loop is the core of claude-code-Go. It determines how the AI:
- Receives user input
- Decides which tools to use
- Executes tools
- Processes results
- Generates responses
Decision
We implemented a Stop-Reason Driven State Machine with the following states:
UserInput → Think → ToolSelection → Execute → Observe → Respond
↑ ↓
└────────── Loopback ←─────────┘State Transitions
| Current State | Trigger | Next State |
|---|---|---|
| UserInput | User sends message | Think |
| Think | Model generates tool_use | ToolSelection |
| Think | Model generates text | Respond |
| ToolSelection | Tool selected | Execute |
| Execute | Tool completes | Observe |
| Observe | Tool result sent to model | Think |
| Respond | Response shown | UserInput |
Stop Reasons
The model can return these stop reasons:
end_turn: Conversation complete, wait for usertool_use: Model wants to use a toolmax_tokens: Context limit reached, need compactionstop_sequence: Special stop token encountered
Consequences
Positive:
- Predictable behavior
- Easy to debug
- Clear state tracking
- Supports both single-turn and multi-turn
Negative:
- Complex state management
- Need to handle all edge cases
- Potential for infinite loops (mitigated with max_turns)
Alternatives Considered
- Event-Driven Architecture: More flexible but harder to reason about
- Coroutine-Based: Could use Go channels, but added complexity
- Recursive Loop: Simpler but harder to track state
Related Decisions
- ADR-003: Context Management
- ADR-005: Session Persistence