Agent Loop โ
Understanding the agent processing cycle.
Overview โ
The agent loop is the core processing cycle that handles each message:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Agent Loop โ
โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ โ Receive โ โ โ Process โ โ โ Respond โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ (loop) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโLoop Stages โ
1. Receive โ
- Message arrives from channel
- Session identified or created
- Context loaded
2. Process โ
- System prompt constructed
- Memory retrieved
- Message sent to model
- Tool calls executed (if any)
3. Respond โ
- Response generated
- Formatted for channel
- Sent to user
- Session updated
Detailed Flow โ
Message In
โ
โผ
โโโโโโโโโโโโโโโโโ
โ Load Session โ
โโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโ
โ Retrieve โ
โ Memory โ
โโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโ
โ Build Context โ
โ (system + โ
โ history + โ
โ memory) โ
โโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโ
โ Call Model โ
โโโโโโโโโโโโโโโโโ
โ
โโโโโ Tool Call? โโโโ
โ โผ
โ โโโโโโโโโโโโโโโโโ
โ โ Execute Tool โ
โ โโโโโโโโโโโโโโโโโ
โ โ
โโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโ
โ Send Response โ
โโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโ
โ Update โ
โ Session & โ
โ Memory โ
โโโโโโโโโโโโโโโโโTool Execution โ
When the model requests a tool:
- Tool call identified
- Tool executed
- Result returned to model
- Model continues processing
This can loop multiple times for complex tasks.
Configuration โ
json5
{
agent: {
maxToolCalls: 10, // Max tool calls per message
timeout: 60000, // Processing timeout (ms)
streaming: true // Stream responses
}
}