Tool Integration
Tool integration enables agents to call external functions and APIs during conversations, extending their capabilities beyond text generation.
Pattern Overview
This pattern involves:
- Tool Calls: Agents request function execution via
ToolCall
events - Tool Results: Functions return results via
ToolResult
events - Async Execution: Tools can execute synchronously or asynchronously
- Error Handling: Failed tool calls are handled gracefully with error information
Key Components
Events
ToolCall
: Request to execute a function with argumentsToolResult
: Result of tool execution (success or error)- Correlation: Tool calls and results linked by
tool_call_id
Nodes
- Tool-Aware Agents: Generate
ToolCall
events during processing - Context Integration: Process
ToolResult
events to continue conversation - Error Recovery: Handle tool failures gracefully
Routes
- Tool Execution: Route
ToolCall
events to tool handlers - Result Processing: Route
ToolResult
events back to agents - Parallel Processing: Multiple tools can execute concurrently
Basic Example
Advanced Tool Integration
Multiple Tool Support
Tool Result Processing
System Tools Pattern
Best Practices
- Error Handling: Always handle tool failures gracefully
- Async Tools: Use async functions for I/O operations (API calls, DB queries)
- Tool Validation: Validate tool arguments before execution
- Result Context: Include tool results in conversation context for follow-up
- Tool Discovery: Implement tool registration and discovery patterns
- Correlation IDs: Use tool_call_id to match calls with results
- Timeout Handling: Set reasonable timeouts for tool execution
Common Use Cases
- API Integration: Call external APIs for data (weather, stocks, news)
- Database Operations: Query databases for user information
- Calculations: Perform complex calculations or data analysis
- System Integration: Interact with internal business systems
- Multi-step Workflows: Chain multiple tool calls for complex tasks
- Real-time Data: Fetch live information during conversations
Error Scenarios
Tool Not Found
Tool Execution Error
Invalid Arguments
This pattern enables agents to become more capable by integrating with external systems and data sources while maintaining conversational flow.