Agent Loop
The agent loop is the core execution cycle that drives all agent behavior.
Loop diagram
Loop stages
| Stage | Description | Hook points |
|---|---|---|
| 1. Input | Receive user instruction | — |
| 2. Context | Load skills, tools, context files | beforeAgentStart, transformContext |
| 3. Prompt | Build system prompt with tools | beforeProviderRequest, beforeProviderPayload |
| 4. Request | Call LLM with streaming | — |
| 5. Parse | Extract tool calls from response | afterProviderResponse |
| 6. Execute | Run tool calls | beforeToolCall, afterToolCall |
| 7. Repeat | Feed results back to LLM | prepareNextTurn, shouldStopAfterTurn |
| 8. Output | Render final response | — |
Error handling
The backend classifies errors into categories:
| Category | Retryable | Action |
|---|---|---|
context_full | No | Compact session |
rate_limit | Yes | Backoff retry |
transient | Yes | Backoff retry |
client | No | Report to user |
poisoned_history | No | Compaction |
unknown | No | Report to user |
Iteration limits
json
{
"maxIterations": 10,
"maxTokens": 8192
}The loop terminates when:
- The agent produces a final response (no tool calls)
maxIterationsis reachedmaxTokensis exceeded- The user cancels