@@ -4,6 +4,7 @@ title: Python Functions as Tools
44
55Any 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
78For 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:
4041agent = Agent(tools = [get_top_hackernews_stories], show_tool_calls = True , markdown = True )
4142agent.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