智能工具库

How to Build Multi-Agent Systems with the OpenAI Agents SDK

How to Build Multi-Agent Systems with the OpenAI Agents SDK
An LLM agent can do a lot by itself. But as an application grows, the agent may need to handle different kinds of work.At that point, it can be useful
1来源:Towards Data Science

本文由「How to Build Multi-Agent Systems with the OpenAI Agents SDK」的公开内容整理,完整内容与后续更新请点击文末链接阅读原文。

An LLM agent can do a lot by itself. But as an application grows, the agent may need to handle different kinds of work.At that point, it can be useful to introduce a multi-agent setup and split the responsibilities.In this post, we'll discuss how to build practical multi-agent systems using the OpenAI Agents SDK. We'll specifically look at two patterns that are the building blocks for the more complex multi-agent architecture: agent-as-a-tool and handoff.We’ll illustrate both patterns with two case studies.···1. It’s All About DelegationA multi-agent system requires splitting responsibilities across multiple agents.Once those responsibilities are split, the agents would need a way to work together. This is where delegation comes in.In practice, there are two widely used patterns for delegation, i.e., agent as a tool, and handoff.1.1 Agent as a ToolSuppose one agent receives a request and needs help from a specialist. The main agent can then call that specialist agent. The specialist agent works on a well-bounded subtask and returns its result to the calling agent once it finishes. The calling agent remains active, and it's its responsibility to produce the final answer by combining the specialist’s response with possibly other information.In this scenario, this specialist agent is effectively a tool from the perspective of the calling agent. And we can easily scale to several specialist agents, where the main agent decides which specialists to consult given the task.This gives us the so-called "agent-as-a-tool" pattern. In the OpenAI Agents SDK, we create this relationship with specialist_agent.as_tool(...). We’ll see the complete setup in the first case study.1.2 HandoffAnother way to delegate is to transfer control to a specialist agent.In this scenario, the initial agent receives the request and decides which specialist to call. What's different here is that the specialist agent would actually take over and become the active agent. It then becomes the specialist agent's responsibility to produce the final response.Here, the initial agent primarily serves as a router, routing the request to the appropriate specialist.In the OpenAI Agents SDK, we can configure the initial agent with a list of possible handoff targets through handoffs=[...].These two mechanisms form the building blocks, and based on them, we can build a variety of multi-agent systems.We’ll return to these larger compositions at the end of the post. But first, let's see some code examples that implement the above two foundational patterns.···2. Putting Delegation into PracticeFor the case study, let’s build two small assistants around electric cars.In the first case study, we consider the following situation: a family needs to decide whether buying an electric car fits their situation. They live in Zurich, Switzerland, and they drive around 15,000 kilometers per year. They have a budget of 45,000 Swiss Francs, but cannot install a private charger. To make a useful recommendation, our multi-agent system needs to look at suitable models, charging options, and ownership costs.In the second case study, we help someone who already drives an electric car and relies on public charging in Zurich. The user wants to understand the available networks, subscriptions, and prices.For the above tasks, we’ll implement two multi-agent assistants with the OpenAI Agents SDK. We start with the purchase adviser.2.1 Building an EV Purchase AdviserFor the purchase question, it naturally breaks into several independent lines of research: we need to find suitable vehicles, assess charging practicality, and understand ownership costs. These findings are useful only when combined into one recommendation.This makes the agent-as-a-tool pattern a natural fit.We can have specialist agents investigating each of those aspects, while we keep a purchase adviser agent in control and task it to synthesize their findings.We first configure the shared client and model:pythonimport osfrom agents impo

本文基于 Towards Data Science 的公开内容,由 AI 辅助整理改写后发布。

原标题:How to Build Multi-Agent Systems with the OpenAI Agents SDK

阅读原文