Add Tool After Initialization
Attach a new tool to an existing agent at runtime with add_tool().
"""
Add Tool After Initialization
=============================
Demonstrates add tool after initialization.
"""
import random
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools import tool
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
@tool(stop_after_tool_call=True)
def get_weather(city: str) -> str:
"""Get the weather for a city."""
# In a real implementation, this would call a weather API
weather_conditions = ["sunny", "cloudy", "rainy", "snowy", "windy"]
random_weather = random.choice(weather_conditions)
return f"The weather in {city} is {random_weather}."
agent = Agent(
model=OpenAIChat(id="gpt-5.2"),
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent.print_response("What can you do?", stream=True)
agent.add_tool(get_weather)
agent.print_response("What is the weather in San Francisco?", stream=True)add_tool adds the function for subsequent runs on this agent. The weather function returns a randomly chosen condition for demonstration; it does not fetch weather data. Its stop flag returns the tool result without a further model turn.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openaiExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the code above as add_tool_after_initialization.py, then run:
python add_tool_after_initialization.pyFull source: cookbook/91_tools/other/add_tool_after_initialization.py