Building Intelligent Swarm Agents with Python: A Dive into Multi-Agent Systems
Swarm agents and multi-agent systems let specialized agents collaborate to solve complex workflows. This article walks through a Python implementation and explores why MAS matter.

Multi-agent systems: the new era of intelligence.
The digital age is increasingly dominated by intelligent systems that collaborate to solve complex problems. Multi-agent systems (MAS), inspired by the behavior of natural swarms, are at the forefront of this transformation.
This article walks through a Python-based implementation of swarm agents for performing arithmetic operations and explains the motivation and potential of MAS in the new era of distributed intelligence.
What Are Swarm Agents and Multi-Agent Systems?
Swarm agents are entities within a system that collaborate to achieve shared objectives. Mimicking biological swarms like bees or ants, these agents operate independently yet communicate to ensure collective success.
Multi-agent systems (MAS) take this concept further by enabling diverse agents to solve specialized tasks.
The Motivation Behind Multi-Agent Systems
The rise of distributed systems, cloud computing, and edge devices demands solutions that:
- Handle complex workflows by delegating tasks to specialized agents.
- Scale dynamically as new challenges arise.
- Mimic human problem-solving through collective intelligence.
Code Walkthrough
In this walkthrough, we’ll build a minimal swarm-style agent setup that can route a user’s request to the right “expert” agent, execute a tool/function call, and return a clean result. The goal is to show the moving parts (routing, specialization, and tool execution) in a way you can extend to real workflows.
1) Setting Up the Environment
from dotenv import load_dotenv
from openai import OpenAI
from swarm import Swarm, Agent
import json
import os
load_dotenv()
model = os.getenv('LLM_MODEL', 'qwen2.5:latest')
ollama_client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="ollama",
)2) Defining Agent Roles
The router agent acts as an orchestrator:
def get_arithmetic_router_agent_instructions():
return """You are an orchestrator of different arithmetic experts and it is your job to
determine which of the agent is best suited to handle the user's request, and
transfer the conversation to that agent."""Specialized agents focus on a single operation:
def get_arithmetic_agent_instructions():
return """You are an arithmetic expert who takes in a request from a user for
information they want to perform an arithmetic operation, and then invoke the function to
run the operation with arguments and get the results back to then report to the
user the information they wanted to know."""
def add_a_and_b(a: int, b: int) -> int:
return a + b3) Creating Specialized Agents
Each operation agent is defined with:
- A name and purpose
- Specific functions it can invoke
- Transfer mechanisms to return control to the router agent
Example:
add_operation_agent = Agent(
name="Add Operation Agent",
instructions=get_arithmetic_agent_instructions()
+ "\n\nHelp the user with performing add operation for two numbers.",
functions=[add_a_and_b],
model=model,
)4) Orchestrating Communication
The router agent decides which specialist handles a request:
def transfer_add_operation_agent():
return add_operation_agent
arithmetic_router_agent.functions = [
transfer_add_operation_agent,
transfer_subtract_operation_agent,
transfer_multiply_operation_agent,
transfer_divide_operation_agent,
]This modular setup makes the system extensible: adding a new capability is often “add a new agent + add a transfer function”.
5) Running the System
def run_demo_loop(starting_agent, context_variables=None, stream=False, debug=False):
client = Swarm(client=ollama_client)
print("Starting Ollama Swarm CLI")
messages = []
agent = starting_agent
while True:
user_input = input("User: ")
messages.append({"role": "user", "content": user_input})
response = client.run(
agent=agent,
messages=messages,
context_variables=context_variables or {},
stream=stream,
debug=debug,
)
pretty_print_messages(response.messages)
messages.extend(response.messages)
agent = response.agentExecution
Example interaction:
python swarm-main.py
User: Hello, can you multiply 3 and 12?
Router Agent: transfer_multiply_operation_agent()
Multiply Operation Agent: The result of multiplying 3 and 12 is 36.
User: what is 2+2*2?
Multiply Operation Agent: The expression 2 + 2 * 2 equals 6, following the order of operations (multiplication before addition).The New Era of Distributed Intelligence
This implementation is a microcosm of what swarm and multi-agent systems can enable.
Potential applications:
- Customer Support: Modular agents for handling different query types
- Edge Computing: Distributed task execution on IoT devices
- Autonomous Vehicles: Coordinated navigation and decision-making
Key properties:
- Resilient: failures in individual agents don’t cripple the whole system
- Scalable: new agents can be added without disrupting existing operations
- Intelligent: emergent behavior can mimic human collaboration patterns
Conclusion
A Python-based swarm agent system like this shows how MAS can solve tasks with modularity and scalability — and serve as a foundation for more complex agentic workflows.
Are you ready to build your own swarm?
Engineering Team
The engineering team at Originsoft Consultancy brings together decades of combined experience in software architecture, AI/ML, and cloud-native development. We are passionate about sharing knowledge and helping developers build better software.
Related Articles
Building Production-Ready AI Agents: A Complete Architecture Guide
In 2026, the gap between AI demos and real tools is defined by agency. This guide explains how to architect, orchestrate, and operate AI agents that can be trusted in production.
Unlocking the Power of CrewAI: A Comprehensive Guide to Building AI-Driven Workflows
A practical guide to building multi-agent workflows with CrewAI—how agents, tasks, crews, and tools fit together, plus six real scenarios like job search automation, lead generation, and trend analysis.
The Death of Prompt Engineering: Why Flow Engineering Is the New AI Frontier
Prompt engineering is no longer enough. Learn why flow engineering and agentic workflows now define how reliable, scalable AI systems are built.
