Tools
Python Functions as Tools
Any python function can be used as a tool by an Agent. We highly recommend creating functions specific to your workflow and adding them to your Agents.
Custom Python Function Example
For example, here’s how to use a get_top_hackernews_stories
function as a tool:
hn_agent.py
Using the @tool Decorator
For more flexibility, you can use the @tool
decorator to quickly create agent-ready functions without creating a full Toolkit class.
This approach is more concise and ideal for single-purpose tools.
The @tool
decorator supports several parameters to customize tool behavior:
advanced_tool.py
@tool Parameters Reference
Parameter | Type | Description |
---|---|---|
name | str | Override for the function name |
description | str | Override for the function description |
show_result | bool | If True, shows the result after function call |
stop_after_tool_call | bool | If True, the agent will stop after the function call |
pre_hook | callable | Hook that runs before the function is executed |
post_hook | callable | Hook that runs after the function is executed |
cache_results | bool | If True, enable caching of function results |
cache_dir | str | Directory to store cache files |
cache_ttl | int | Time-to-live for cached results in seconds (default: 3600) |