Overview
The agent loop is the foundation for all Sprout agents. It sends messages to Claude, executes tool calls, feeds results back, and loops until the model stops calling tools or hits the maximum iteration limit. Location:sprout-backend/src/agents/agent-loop.ts
Loop flow
The agent loop follows this pattern:Send message to Claude
The loop begins by sending the initial message (system prompt + user context) to Claude.
Emit reasoning
The
onThinking callback captures Claude’s reasoning text and emits it as agent_reasoning SSE events.Execute tools
Each tool call is executed with the provided parameters. Results are captured and emitted via
onToolCall and onToolResult callbacks.Key features
Reasoning visibility
The agent loop captures Claude’s reasoning text between tool calls:Reasoning visibility is a key feature that provides transparency into agent decision-making. Users can see why an agent chose to add a subconcept or insert a prerequisite concept.
Retry with backoff
Rate limits (HTTP 429) are automatically retried with exponential backoff:- 1st retry: 2 seconds
- 2nd retry: 4 seconds
- 3rd retry: 8 seconds
Configurable iterations
Agents have different iteration limits based on their complexity:| Agent | Max Iterations | Reason |
|---|---|---|
| Topic Agent | 15 | Needs to extract context and create multiple concepts |
| Subconcept Agent | 15 | Creates diagnostic questions + subconcept DAG |
| Refinement Agent | 15 | Complex ORAV loop with multiple verification steps |
| Tutor Agent | 5 | Shorter interactions with students (chunk-based) |
| Grading Agent | 10 | Semantic evaluation of student answers |
Agent loop parameters
System prompt that defines the agent’s role and behavior.
Initial user message or context for the agent to process.
Array of Claude tool definitions that the agent can call.
Maximum number of tool-calling iterations. Defaults to 15.
Claude model to use. Defaults to
claude-opus-4-20250514.Optional callbacks for reasoning and tool execution visibility.
Agent loop return value
The agent loop returns:The final text response from Claude after all tool calls.
Array of all tool calls made during the loop, with inputs and results.
Complete conversation history including system prompt, tool calls, and results.
Tool execution
Tools are executed synchronously in the order Claude requests them:Tool execution is synchronous within each iteration. If Claude requests 3 tools, they execute sequentially before the next Claude API call.
Small mode
Agents support a “small mode” for cost-efficient testing:- Topic Agent: 1-2 concepts instead of 6-10
- Subconcept Agent: 2-3 questions and 2-3 subconcepts instead of 5-10 / 8-12
Error handling
The agent loop handles several error scenarios:Rate limit errors (429)
Rate limit errors (429)
Automatically retried with exponential backoff (2s, 4s, 8s). After 3 retries, the error is propagated.
Tool execution errors
Tool execution errors
If a tool throws an error, it’s converted to a
tool_result with an error message. Claude receives the error and can decide how to proceed.Max iterations reached
Max iterations reached
If the agent hits
maxIterations, it stops and returns the final text and all tool calls up to that point.Invalid tool calls
Invalid tool calls
If Claude requests a tool that doesn’t exist, an error
tool_result is sent back and the loop continues.Performance considerations
Token usage
Each iteration adds to the conversation history:- System prompt (once): ~1,000 tokens
- Each tool call: ~100-500 tokens (input)
- Each tool result: ~200-2,000 tokens (result)
- Claude reasoning: ~200-1,000 tokens per iteration
- Topic Agent (15 iterations): 20,000-40,000 tokens
- Tutor Agent (5 iterations): 5,000-15,000 tokens
Latency
Each iteration requires:- Claude API call: 2-5 seconds
- Tool execution: 0.1-2 seconds per tool
- Total per iteration: 2-10 seconds
- Topic Agent (15 iterations): 30-150 seconds
- Tutor Agent (5 iterations): 10-50 seconds
Next steps
Topic Agent
Learn how the Topic Agent uses the loop to generate concepts
Refinement Agent
See how the Refinement Agent uses the ORAV loop