Skip to content

Agent Loop

The agent loop is the core execution cycle that drives all agent behavior.

Loop diagram

Loop stages

StageDescriptionHook points
1. InputReceive user instruction
2. ContextLoad skills, tools, context filesbeforeAgentStart, transformContext
3. PromptBuild system prompt with toolsbeforeProviderRequest, beforeProviderPayload
4. RequestCall LLM with streaming
5. ParseExtract tool calls from responseafterProviderResponse
6. ExecuteRun tool callsbeforeToolCall, afterToolCall
7. RepeatFeed results back to LLMprepareNextTurn, shouldStopAfterTurn
8. OutputRender final response

Error handling

The backend classifies errors into categories:

CategoryRetryableAction
context_fullNoCompact session
rate_limitYesBackoff retry
transientYesBackoff retry
clientNoReport to user
poisoned_historyNoCompaction
unknownNoReport to user

Iteration limits

json
{
  "maxIterations": 10,
  "maxTokens": 8192
}

The loop terminates when:

  • The agent produces a final response (no tool calls)
  • maxIterations is reached
  • maxTokens is exceeded
  • The user cancels

MIT License