Learn how to give your agents new capabilities with tools
Tools are what make agents powerful. They allow your agent to interact with the world - searching the web, processing files, analyzing images, and much more. In this guide, you’ll learn how to add tools to your agents.💻 Code Examples
You can create your own tools by following the OpenAI function format:
Copy
def get_weather(location: str, unit: str = "celsius") -> str: """Get the current weather for a location.""" # Your implementation here return f"The weather in {location} is sunny and 22°{unit[0].upper()}"# Tool definitionweather_tool = { "definition": { "name": "get_weather", "description": "Get the current weather for a location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and country, e.g. San Francisco, USA" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit" } }, "required": ["location"] } }, "implementation": get_weather}# Use your custom toolagent = Agent( name="weather-assistant", model_name="gpt-4", purpose="To provide weather information", tools=[weather_tool])
from lye import WEB_TOOLS, BROWSER_TOOLS, FILES_TOOLSscraper_agent = Agent( name="web-scraper", model_name="gpt-4", purpose="To extract and save information from websites", tools=[*WEB_TOOLS, *BROWSER_TOOLS, *FILES_TOOLS])
A TimeoutError is raised with the message: Tool 'tool_name' timed out after X seconds
The error is returned to the LLM as the tool result
The LLM can then decide how to proceed (retry, try a different approach, or inform the user)
Timeouts work for both async and synchronous tools. For sync tools, the operation runs in a thread pool with the timeout applied to the entire operation.
For synchronous tools, the underlying thread cannot be forcibly stopped after a timeout—it will continue running in the background. For truly cancellable long-running operations, implement your tools as async functions.
You can also specify timeouts in the lye tool format:
Copy
my_tool = { "definition": { "type": "function", "function": { "name": "my_slow_tool", "description": "A tool that might take a while", "parameters": {...} } }, "implementation": my_slow_function, "timeout": 60.0 # 60 second timeout}
# Agent that can research, analyze, and reportagent = Agent( name="analyst", model_name="gpt-4", purpose="To analyze data from multiple sources", tools=[ *WEB_TOOLS, # Gather data *IMAGE_TOOLS, # Analyze visuals *FILES_TOOLS # Save results ])