generate_text() and stream_text().
It is not about the Agent class. For persistent stateful agents, read Stateful Agents.
What Is A Tool Loop?
When you pass tools to a model call, the model can ask to call a tool. ai-query executes the tool, sends the result back to the model, and continues until the model produces a final answer or a stop condition fires. The loop is:- send messages and tool definitions to the model
- receive text and/or tool calls
- execute requested tools
- append tool results
- call the model again
- stop when there are no tool calls or a stop condition is met
Minimal Example
Stop Conditions
Stop conditions prevent runaway loops.Stop after N steps
Stop when a tool is called
Combine conditions
True.
Step Results
The result contains every step.Step Callbacks
Callbacks let you observe or steer the loop at step boundaries.Observe start
Observe finish
Inject at a boundary
Streaming Tool Loops
Tool loops work with streaming too.When To Use This Layer
Use core tool loops when:- you do not need persistent agent state
- you are writing a script or background job
- you want direct control over callbacks and stop conditions
- you are building a custom abstraction above core generation
Agent instead when:
- you need message history
- you need durable state
- you need server routes
- you need live turn events or steering