How to Create a Multi-Agent System with OpenAI

Learn how to create a multi-agent system with OpenAI.

Tutorials5 min read min read
How to Create a Multi-Agent System with OpenAI

If you want to build a basic multi-agent system without relying on heavy frameworks like LangGraph or CrewAI, you can create it from scratch using just the official OpenAI Python SDK.

This approach is perfect for learning how agents can communicate, coordinate tasks, and specialize, using prompt engineering and Python logic alone. For a more advanced implementation using frameworks, check out our guide on building multi-agent systems with LangGraph, CrewAI and LlamaIndex.


🤖 What is a Multi-Agent System?

A multi-agent system (MAS) consists of multiple AI agents working collaboratively or independently to solve problems. Each agent has its own:

  • Role or specialization
  • Goal or task
  • Communication interface (how it exchanges information)

In this tutorial, we build a minimal MAS where agents send messages to each other through a simple Python orchestrator, all powered by OpenAI GPT models. For more advanced agent implementations, see our guides on AI Agent Web Scraping and building intelligent agents with ScrapeGraph.


🛠️ Prerequisites

Make sure you have the OpenAI Python library installed:

bash
pip install openai

Set your API key as an environment variable:

bash
export OPENAI_API_KEY="your-openai-api-key"

1. Define Your Agents as Python Classes

Each agent will have a role, and a method to generate responses based on input messages.

python
import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

class Agent:
    def __init__(self, name, role_description):
        self.name = name
        self.role = role_description
        self.history = []

    def send_message(self, message):
        # Store incoming message
        self.history.append({"role": "user", "content": message})
        # Generate agent's reply using OpenAI
        response = openai.ChatCompletion.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": f"You are an AI agent named {self.name}. Your role: {self.role}."}
            ] + self.history
        )
        reply = response.choices[0].message.content.strip()
        # Store agent reply in history
        self.history.append({"role": "assistant", "content": reply})
        return reply

2. Instantiate Your Agents

Create a few agents with different specializations.

python
researcher = Agent(
    name="Researcher",
    role_description="Finds and summarizes the latest technology trends."
)

analyst = Agent(
    name="Analyst",
    role_description="Analyzes research data and provides actionable insights."
)

3. Build the Orchestrator to Make Agents Talk

We simulate a conversation where the Researcher gathers info, then sends it to the Analyst for interpretation.

python
def multi_agent_workflow():
    print("Starting multi-agent workflow...\n")

    # Step 1: Researcher collects info
    researcher_message = "Please summarize recent breakthroughs in AI."
    print(f"[User -> Researcher]: {researcher_message}")
    research_output = researcher.send_message(researcher_message)
    print(f"[Researcher]: {research_output}\n")

    # Step 2: Analyst receives summary and analyzes it
    print(f"[Researcher -> Analyst]: {research_output}")
    analysis = analyst.send_message(f"Analyze this research summary and provide insights:\n{research_output}")
    print(f"[Analyst]: {analysis}\n")

multi_agent_workflow()

4. Output Example

text
[User -> Researcher]: Please summarize recent breakthroughs in AI.
[Researcher]: Recent breakthroughs in AI include advances in large language models, reinforcement learning, and multimodal AI that can process images and text simultaneously.

Ready to Scale Your Data Collection?

Join thousands of businesses using ScrapeGrapAI to automate their web scraping needs. Start your journey today with our powerful API.

[Researcher -> Analyst]: Recent breakthroughs in AI include advances in large language models, reinforcement learning, and multimodal AI that can process images and text simultaneously. [Analyst]: These breakthroughs enable new applications in natural language understanding, autonomous systems, and creative content generation, opening exciting possibilities across industries.

text

---

## 🧠 How It Works

- Each agent holds its own conversation history (`self.history`), allowing context persistence.
- The orchestrator manages message passing between agents by calling `send_message`.
- System messages define agent roles and behavior context.
- You can expand this with more agents, more complex message routing, or tool integration.

---

## 🚀 Next Steps

- Add memory persistence to store agent conversations in a database.
- Build message queues for asynchronous communication.
- Introduce tool-use capabilities (e.g., calling APIs or scraping data). Learn how to [integrate ScrapeGraphAI as a tool](/blog/scrapegraphai-crewai-integration) in your agents.
- Wrap this logic in a web server for interactive apps.

---

## ❓ Frequently Asked Questions (FAQ)

### What is a multi-agent system?

A multi-agent system (MAS) is a setup where multiple AI agents work together or independently to achieve complex goals by dividing tasks and collaborating.

---

### Why use multiple agents instead of one?

Multiple agents can specialize, share workload, and produce more reliable, modular, and scalable AI workflows.

---

### Can I scale this simple example?

Yes, by adding more agents, improving communication channels (e.g., message queues), and persisting state externally.

---

### Which OpenAI models work best here?

You can use GPT-4 or GPT-4o-mini for production; GPT-3.5-turbo is a cheaper alternative but with some limitations.

---

### How do agents remember context?

Each agent maintains its own conversation history and sends that with each request to provide context.

---

### Can agents call external APIs?

Yes, but you need to add code to your agents to perform API calls as tools, then feed results back into their prompt or history.

---

## 🔚 Conclusion

Even without frameworks, you can build a functional multi-agent system using the OpenAI Python SDK. This approach gives you full control and helps you understand core multi-agent principles. For more advanced implementations, explore our guides on [building agents with frameworks](/blog/how-to-create-agent-with-frameworks) and [multi-agent systems with LangGraph](/blog/multi-agent).

Happy coding! 🚀

---

## Related Resources

Want to learn more about building AI agents? Explore these guides:

- [Web Scraping 101](/blog/101-scraping) - Master the basics of web scraping
- [Building Agents with Frameworks](/blog/how-to-create-agent-with-frameworks) - Learn about using LangGraph and CrewAI
- [Multi-Agent Systems](/blog/multi-agent) - Deep dive into building complex agent systems
- [AI Agent Web Scraping](/blog/ai-agent-webscraping) - Discover how to integrate scraping with agents
- [Integrating ScrapeGraphAI](/blog/integrating-scrapegraph-into-intelligent-agents) - Learn how to add scraping capabilities to your agents
- [LlamaIndex Integration](/blog/llamaindex-integration) - Combine agents with document processing
- [ScrapeGraphAI CrewAI Integration](/blog/scrapegraphai-crewai-integration) - See how to use ScrapeGraphAI with CrewAI
- [Building Intelligent Agents](/blog/integrating-scrapegraph-into-intelligent-agents) - Advanced agent development techniques
- [Web Scraping Legality](/blog/legality) - Understand the legal aspects of agent-based scraping

These resources will help you understand different approaches to building AI agents and integrating them with web scraping capabilities.

---

Try ScrapeGrapAI

Start with our free tier