0
0
Agentic-aiHow-ToBeginner ยท 4 min read

How to Create Multi Agent System: Simple Guide and Example

To create a multi agent system, define multiple independent agents that interact or work together to solve tasks. Each agent has its own behavior and communication method, often implemented as separate classes or processes. Coordination can be done via message passing or shared environment.
๐Ÿ“

Syntax

A multi agent system typically involves these parts:

  • Agent class: Defines the behavior and state of each agent.
  • Environment: Shared space or context where agents act.
  • Communication: Methods for agents to send messages or signals.
  • Scheduler/Controller: Manages agent actions and timing.

Each agent runs independently but can interact through communication or environment changes.

python
class Agent:
    def __init__(self, name):
        self.name = name
    def act(self):
        pass

class Environment:
    def __init__(self):
        self.agents = []
    def add_agent(self, agent):
        self.agents.append(agent)
    def step(self):
        for agent in self.agents:
            agent.act()
๐Ÿ’ป

Example

This example shows two agents that greet each other by sending messages through a simple environment.

python
class Agent:
    def __init__(self, name, env):
        self.name = name
        self.env = env
        self.messages = []
    def send_message(self, msg, receiver):
        self.env.deliver_message(msg, receiver)
    def receive_message(self, msg):
        self.messages.append(msg)
    def act(self):
        if self.messages:
            print(f"{self.name} received: {self.messages.pop(0)}")
        else:
            # Send greeting if no messages
            for agent in self.env.agents:
                if agent != self:
                    self.send_message(f"Hello from {self.name}", agent)

class Environment:
    def __init__(self):
        self.agents = []
    def add_agent(self, agent):
        self.agents.append(agent)
    def deliver_message(self, msg, receiver):
        receiver.receive_message(msg)
    def step(self):
        for agent in self.agents:
            agent.act()

# Setup environment and agents
env = Environment()
agent1 = Agent("Agent1", env)
agent2 = Agent("Agent2", env)
env.add_agent(agent1)
env.add_agent(agent2)

# Run steps
for _ in range(3):
    env.step()
Output
Agent1 received: Hello from Agent2 Agent2 received: Hello from Agent1 Agent1 received: Hello from Agent2
โš ๏ธ

Common Pitfalls

Common mistakes when creating multi agent systems include:

  • Lack of clear communication: Agents must have defined ways to send and receive messages.
  • Shared state conflicts: Avoid agents changing shared data without coordination.
  • Infinite loops: Agents acting without stopping or waiting can freeze the system.
  • Ignoring agent independence: Agents should operate independently to allow parallelism.

Always design communication and environment carefully to prevent these issues.

python
class Agent:
    def __init__(self, name):
        self.name = name
    def act(self):
        # Wrong: modifying shared data without control
        global shared_data
        shared_data += 1

# Correct approach uses locks or message passing to avoid conflicts.
๐Ÿ“Š

Quick Reference

Tips for building multi agent systems:

  • Define clear agent roles and behaviors.
  • Use message passing for communication.
  • Keep agents loosely coupled for flexibility.
  • Manage shared resources carefully.
  • Test agent interactions step-by-step.
โœ…

Key Takeaways

Create independent agents with defined behaviors and communication methods.
Use an environment or controller to manage agent interactions and timing.
Avoid shared state conflicts by using message passing or synchronization.
Test multi agent interactions carefully to prevent infinite loops or deadlocks.
Keep agents loosely coupled to allow easy extension and parallelism.