Agents as Tools
Description: Create a multi-agent system where agents use other agents as tools for translation
Difficulty: advanced
Category: tools
Python Code Example
import asyncio
from agents import Agent, ItemHelpers, MessageOutputItem, Runner, trace
"""
This example shows the agents-as-tools pattern. The frontline agent receives a user message and
then picks which agents to call, as tools. In this case, it picks from a set of translation
agents.
"""
spanish_agent = Agent(
name="spanish_agent",
instructions="You translate the user's message to Spanish",
handoff_description="An english to spanish translator",
)
french_agent = Agent(
name="french_agent",
instructions="You translate the user's message to French",
handoff_description="An english to french translator",
)
italian_agent = Agent(
name="italian_agent",
instructions="You translate the user's message to Italian",
handoff_description="An english to italian translator",
)
orchestrator_agent = Agent(
name="orchestrator_agent",
instructions=(
"You are a translation agent. You use the tools given to you to translate."
"If asked for multiple translations, you call the relevant tools in order."
"You never translate on your own, you always use the provided tools."
),
tools=[
spanish_agent.as_tool(
tool_name="translate_to_spanish",
tool_description="Translate the user's message to Spanish",
),
french_agent.as_tool(
tool_name="translate_to_french",
tool_description="Translate the user's message to French",
),
italian_agent.as_tool(
tool_name="translate_to_italian",
tool_description="Translate the user's message to Italian",
),
],
)
synthesizer_agent = Agent(
name="synthesizer_agent",
instructions="You inspect translations, correct them if needed, and produce a final concatenated response.",
)
async def main():
msg = "Hello world, translate this to Spanish, French, and Italian"
print(f"User message: {msg}\n")
# Run the entire orchestration in a single trace
with trace("Orchestrator evaluator"):
orchestrator_result = await Runner.run(orchestrator_agent, msg)
for item in orchestrator_result.new_items:
if isinstance(item, MessageOutputItem):
text = ItemHelpers.text_message_output(item)
if text:
print(f" - Translation step: {text}")
synthesizer_result = await Runner.run(
synthesizer_agent, orchestrator_result.to_input_list()
)
print(f"\n\nFinal response:\n{synthesizer_result.final_output}")
if __name__ == "__main__":
asyncio.run(main())
Expected Output: Translations in Spanish, French, and Italian
About This Example
This is an interactive Python tutorial for the OpenAI Agents SDK. The code runs directly in your browser using PyOdide (Python in WebAssembly).
Key Features:
- No installation required - runs entirely in your browser
- Interactive code editor with syntax highlighting
- Real-time execution using PyOdide
- Complete OpenAI Agents SDK functionality
- Connect with GitHub Models for AI capabilities
Related Examples:
- Usage Tracking with Tools - Track API usage metrics including token counts and request statistics when using tools
- Forcing Tool Use - Learn how to force agents to use tools with different behaviors: default, first tool result, or custom logic
← Back to all examples
JavaScript is required to run the interactive code editor. Please enable JavaScript in your browser for the full experience.