EP 03 / 234:56Live now
Give Your Agent a Tool
Turn any Python function into an agent capability with the @tool decorator.
Agents that only talk are demos. Agents that use tools are systems.
In AG2, any Python function becomes a tool by adding the @tool decorator. The agent reads the function's docstring + type hints to learn when and how to call it.
What we build: a weather agent that calls a real get_weather(city) function when asked. Same pattern works for database queries, file reads, API calls, anything Python can do.
Watch for: the docstring is the prompt the LLM sees describing the tool. Bad docstring → bad tool use. Spend time on it.
Links
Code
from autogen import ConversableAgent, tool
@tool
def get_weather(city: str) -> str:
"""Return the current weather for the given city.
Args:
city: city name, e.g. 'San Francisco'
"""
# In real life, call a weather API here.
return f"It's 18°C and clear in {city}."
travel = ConversableAgent(
name="travel",
llm_config={"model": "gpt-4o"},
tools=[get_weather],
)
travel.run("What should I pack for Lisbon tomorrow?")