Agents
Agents are large language models (LLMs) that use tools in a loop to accomplish tasks. These components work together:- LLMs process input and decide the next action
- Tools extend capabilities beyond text generation (reading files, calling APIs, writing to databases)
- Loop orchestrates execution through:
- Context management - Maintaining conversation history and deciding what the model sees (input) at each step
- Stopping conditions - Determining when the loop (task) is complete
ToolLoopAgent Class
The ToolLoopAgent class handles these three components. Here’s an agent that uses multiple tools in a loop to accomplish a task:- Calls the
weathertool to get the temperature in Fahrenheit - Calls
convertFahrenheitToCelsiusto convert it - Generates a final text response with the result
Why Use the ToolLoopAgent?
The ToolLoopAgent is the recommended approach for building agents with the AI SDK because it:- Reduces boilerplate - Manages loops and message arrays
- Improves reusability - Define once, use throughout your application
- Simplifies maintenance - Single place to update agent configuration
generateText, streamText) when you need explicit control over each step for complex structured workflows.
Structured Workflows
Agents are flexible and powerful, but non-deterministic. When you need reliable, repeatable outcomes with explicit control flow, use core functions with structured workflow patterns combining:- Conditional statements for explicit branching
- Standard functions for reusable logic
- Error handling for robustness
- Explicit control flow for predictability
Next Steps
- Building Agents - Guide to creating agents with the ToolLoopAgent
- Workflow Patterns - Structured patterns using core functions for complex workflows
- Loop Control - Execution control with stopWhen and prepareStep