Dynamic Tools
Build the tool list at runtime from a function that reads session state off the RunContext.
"""
Dynamic Tools
=============================
Dynamic Tools.
"""
from datetime import datetime, timezone
from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.run import RunContext
def get_runtime_tools(run_context: RunContext):
"""Return tools dynamically based on session state."""
def get_time() -> str:
return datetime.now(timezone.utc).isoformat()
def get_project() -> str:
project = (run_context.session_state or {}).get("project", "unknown")
return f"Current project: {project}"
return [get_time, get_project]
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
name="Dynamic Tools Agent",
model=OpenAIResponses(id="gpt-5.2"),
tools=get_runtime_tools,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent.print_response(
"Use available tools to report current context.",
session_state={"project": "cookbook-restructure"},
stream=True,
)Resolve the factory for each run
Add cache_callables=False to this Agent constructor before running. The returned get_project function closes over its RunContext. Default callable caching reuses that closure for the same user or session, so a later run can report the first run's project. Disabling caching makes this state-dependent factory run again with the current context.
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 dynamic_tools.py, then run:
python dynamic_tools.pyFull source: cookbook/02_agents/15_dependencies/dynamic_tools.py