Welcome back! In the last two lessons, you learned how to make your OpenAI agents more capable by integrating both hosted tools (like the WebSearchTool
) and your own custom function tools. These skills allow your agents to access real-time information and perform custom logic, such as calculating a travel budget.
Today, you will take the next step in building modular and maintainable AI systems: turning agents themselves into reusable tools. This approach allows you to create specialized agents for specific tasks and then orchestrate them from a central, main agent. By the end of this lesson, you will know how to convert an agent into a callable tool, add it to another agent’s tool list, and run a workflow in which the main agent delegates tasks to its specialized agent-tools. This modular design is a powerful way to build complex, flexible AI applications.
Let’s quickly review what you have learned so far. In the previous lesson, you saw how to create custom function tools by writing Python functions with type annotations and docstrings, then registering them with your agent using the @function_tool
decorator. This allowed your agent to perform custom logic, such as estimating a travel budget, in addition to using hosted tools like web search.
Now, imagine you want to break down your agent’s responsibilities even further. Instead of having one agent do everything, you can create smaller, specialized agents — each focused on a specific task, such as research or calculations. By turning these agents into callable tools, you can have a central agent delegate tasks to them as needed. This not only keeps your code organized but also makes it easier to maintain and extend your system in the future.
While using agents as tools and handoffs both involve interactions between agents, they serve different purposes:
-
Agents as Tools: The central agent retains control and invokes other agents as tools to perform specific tasks. This approach is suitable when the primary agent needs to utilize the capabilities of specialized agents without transferring control.
-
Handoffs: The primary agent delegates control to another agent, effectively transferring the conversation or task to the specialized agent. This method is useful when a task or query is best handled entirely by another agent.
In summary, using agents as tools allows for the integration of specialized agents into a workflow while maintaining control within the central agent, whereas handoffs involve transferring control to another agent to handle specific tasks or queries.
To turn an agent into a tool, you use the .as_tool()
method. This method wraps your agent so it can be called just like any other tool, such as a function tool or a hosted tool. When the main agent decides to use this tool, it will invoke the specialized agent, get its result, and then continue its own workflow.
Let’s look at a simple example. Suppose you have a travel researcher agent that specializes in searching for the latest travel news. You can define this agent as follows:
To make this agent available as a tool, you call its .as_tool()
method. You can also give it a custom name and description, which helps the main agent understand when to use it:
Now, researcher_tool
can be added to another agent’s tool list, just like any other tool.
Once you have converted your specialized agent into a tool, you can add it to a main agent’s list of tools. This allows the main agent to delegate specific tasks to the specialized agent whenever it decides it is appropriate.
Now, you can create a main agent that includes the researcher agent as a tool, alongside other tools:
With this setup, the main agent can decide when to call on the researcher agent for up-to-date travel news and when to use the budget estimator for calculations. This modular approach keeps each agent focused on its own task, while the main agent orchestrates the overall workflow.
Let's see how the main agent leverages both a specialized researcher agent (as a tool) and a budget estimator in a real scenario. Here’s how you can run the orchestrated agent and display its output:
In this example, the main agent analyzes the user's question and determines that it needs to both research current snow conditions and estimate a travel budget. Thanks to the modular setup, it can delegate the research to the specialized agent-tool and the calculation to the budget estimator. The response might look like:
This output demonstrates how the main agent orchestrates multiple tools—calling the researcher agent for up-to-date information and the budget estimator for calculations—then combines their results into a single, helpful answer.
To better understand how the main agent delegates tasks and how each tool is used, you can inspect the full interaction sequence:
This reveals the step-by-step process the agent followed:
This breakdown illustrates the agent’s orchestration process:
- The main agent receives the user's question.
- It recognizes the need for up-to-date information and delegates to the
research_information
agent-tool. - It also identifies a calculation task and invokes the
estimate_budget
tool with the appropriate parameters. - Each tool returns its result.
- The main agent combines these results into a single, coherent response for the user.
Notice how the main agent called the specialized researcher agent exactly like a function tool—passing arguments, receiving outputs, and tracking the call in the same way as with any other tool. This demonstrates the power of modular delegation: by converting an agent into a callable tool, you enable seamless, function-like invocation and integration within more complex agent workflows.
In this lesson, you learned how to turn agents into reusable tools using the .as_tool()
method and how to orchestrate them from a main agent. This modular approach allows you to build more organized, maintainable, and powerful AI systems, where each agent focuses on a specific task and the main agent coordinates the workflow.
You also saw how to run the orchestrated agent, inspect its tool-call flow, and understand how the main agent delegates tasks to its specialized agent-tools. This is a key skill for building complex applications that require multiple steps or areas of expertise.
In the next set of exercises, you will get hands-on practice creating your own agent-tools and integrating them into a main agent. Keep experimenting and exploring — this modular approach opens up many possibilities for building advanced AI solutions!
