Skip to content

Commit 7920b9a

Browse files
authored
Add docs for the @tool decorator (#190)
1 parent ef1dae0 commit 7920b9a

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

tools/caching.mdx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ title: Tool Result Caching
55
Tool result caching is designed to avoid unnecessary recomputation by storing the results of function calls on disk.
66
This is useful during development and testing to speed up the development process, avoid rate limiting, and reduce costs.
77

8+
This is supported for all Agno Toolkits
9+
810
## Example
911

1012
Pass `cache_results=True` to the Toolkit constructor to enable caching for that Toolkit.
@@ -30,16 +32,3 @@ asyncio.run(
3032
)
3133
)
3234
```
33-
34-
35-
## Supported Toolkits
36-
37-
- `DuckDuckGoTools`
38-
- `ExaTools`
39-
- `FirecrawlTools`
40-
- `GoogleSearchtools`
41-
- `HackernewsTools`
42-
- `NewspaperTools`
43-
- `Newspaper4kTools`
44-
- `Websitetools`
45-
- `YFinanceTools`

tools/custom-toolkits.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,3 @@ class ShellTools(Toolkit):
5050

5151
agent = Agent(tools=[ShellTools()], show_tool_calls=True, markdown=True)
5252
agent.print_response("List all the files in my home directory.")
53-
```

tools/functions.mdx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title: Python Functions as Tools
44

55
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.
66

7+
## Custom Python Function Example
78
For example, here's how to use a `get_top_hackernews_stories` function as a tool:
89

910
```python hn_agent.py
@@ -40,3 +41,75 @@ def get_top_hackernews_stories(num_stories: int = 10) -> str:
4041
agent = Agent(tools=[get_top_hackernews_stories], show_tool_calls=True, markdown=True)
4142
agent.print_response("Summarize the top 5 stories on hackernews?", stream=True)
4243
```
44+
45+
## Using the @tool Decorator
46+
47+
For more flexibility, you can use the `@tool` decorator to quickly create agent-ready functions without creating a full Toolkit class.
48+
This approach is more concise and ideal for single-purpose tools.
49+
50+
The `@tool` decorator supports several parameters to customize tool behavior:
51+
52+
```python advanced_tool.py
53+
import httpx
54+
from agno.agent import Agent
55+
from agno.tools import tool
56+
57+
def log_before_call(fc):
58+
"""Pre-hook function that runs before the tool execution"""
59+
print(f"About to call function with arguments: {fc.arguments}")
60+
61+
def log_after_call(fc):
62+
"""Post-hook function that runs after the tool execution"""
63+
print(f"Function call completed with result: {fc.result}")
64+
65+
@tool(
66+
name="fetch_hackernews_stories", # Custom name for the tool (otherwise the function name is used)
67+
description="Get top stories from Hacker News", # Custom description (otherwise the function docstring is used)
68+
show_result=True, # Show result after function call
69+
stop_after_tool_call=True, # Return the result immediately after the tool call and stop the agent
70+
pre_hook=log_before_call, # Hook to run before execution
71+
post_hook=log_after_call, # Hook to run after execution
72+
cache_results=True, # Enable caching of results
73+
cache_dir="/tmp/agno_cache", # Custom cache directory
74+
cache_ttl=3600 # Cache TTL in seconds (1 hour)
75+
)
76+
def get_top_hackernews_stories(num_stories: int = 5) -> str:
77+
"""
78+
Fetch the top stories from Hacker News.
79+
80+
Args:
81+
num_stories: Number of stories to fetch (default: 5)
82+
83+
Returns:
84+
str: The top stories in text format
85+
"""
86+
# Fetch top story IDs
87+
response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
88+
story_ids = response.json()
89+
90+
# Get story details
91+
stories = []
92+
for story_id in story_ids[:num_stories]:
93+
story_response = httpx.get(f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json")
94+
story = story_response.json()
95+
stories.append(f"{story.get('title')} - {story.get('url', 'No URL')}")
96+
97+
return "\n".join(stories)
98+
99+
agent = Agent(tools=[get_top_hackernews_stories])
100+
agent.print_response("Show me the top news from Hacker News")
101+
```
102+
103+
### @tool Parameters Reference
104+
105+
| Parameter | Type | Description |
106+
|-----------|------|-------------|
107+
| `name` | `str` | Override for the function name |
108+
| `description` | `str` | Override for the function description |
109+
| `show_result` | `bool` | If True, shows the result after function call |
110+
| `stop_after_tool_call` | `bool` | If True, the agent will stop after the function call |
111+
| `pre_hook` | `callable` | Hook that runs before the function is executed |
112+
| `post_hook` | `callable` | Hook that runs after the function is executed |
113+
| `cache_results` | `bool` | If True, enable caching of function results |
114+
| `cache_dir` | `str` | Directory to store cache files |
115+
| `cache_ttl` | `int` | Time-to-live for cached results in seconds (default: 3600) |

0 commit comments

Comments
 (0)