External Tool Execution Async
Execute tools outside the agent in an async environment. You control tool execution externally while the agent handles the rest of the run.
Create a Python file
import asyncio
import shlex
import subprocess
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
from agno.tools import tool
from agno.utils import pprint
# We have to create a tool with the correct name, arguments and docstring for the agent to know what to call.
@tool(external_execution=True)
def execute_shell_command(command: str) -> str:
"""Execute a shell command.
Args:
command (str): The shell command to execute
Returns:
str: The output of the shell command
"""
if shlex.split(command) in (["ls"], ["ls", "-a"], ["ls", "-l"], ["ls", "-la"]):
return subprocess.check_output(shlex.split(command), timeout=10).decode("utf-8")
else:
raise Exception(f"Unsupported command: {command}")
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
tools=[execute_shell_command],
markdown=True,
db=SqliteDb(session_table="test_session", db_file="tmp/example.db"),
)
async def main():
run_response = await agent.arun('What files do I have in my current directory?')
if run_response.is_paused:
for requirement in run_response.active_requirements:
if requirement.needs_external_execution:
if requirement.tool_execution.tool_name == execute_shell_command.name:
print(f'Executing {requirement.tool_execution.tool_name} with args {requirement.tool_execution.tool_args} externally')
result = execute_shell_command.entrypoint(**requirement.tool_execution.tool_args)
requirement.set_external_execution_result(result)
run_response = await agent.acontinue_run(run_id=run_response.run_id, requirements=run_response.requirements)
pprint.pprint_run_response(run_response)
asyncio.run(main())
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openai sqlalchemyExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run Agent
python external_tool_execution_async.py