In this tutorial, we explore a powerful multi-agent system built around peer patterns: planning, execution, expression, and review. We run the entire workflow in Google Colab/Notebook, integrate agents with professional roles, and leverage Google’s Gemini 1.5 Flash model with free API keys. As we browse the system, we observe how each agent collaborates to solve complex tasks across different fields (such as finance, technology, and creative strategies). This hands-on tutorial allows us to understand the architecture, workflow and iterative improvements that support high-quality AI output.
!pip install agentUniverse google-generativeai python-dotenv pydantic
import os
import asyncio
from typing import Dict, List, Any, Optional
from dataclasses import dataclass
from enum import Enum
import json
import time
import google.generativeai as genai
GEMINI_API_KEY = 'Use Your API Key Here'
genai.configure(api_key=GEMINI_API_KEY)
We first install the required libraries, including Agensuniverse and Google-generativeai, to set up our multi-agent system. After importing the necessary modules, we configure the Gemini API using the free API keys to enable content generation with AI capabilities. Check The complete code is here.
class AgentRole(Enum):
PLANNER = "planner"
EXECUTOR = "executor"
EXPRESSER = "expresser"
REVIEWER = "reviewer"
@dataclass
class Task:
id: str
description: str
context: Dict[str, Any]
status: str = "pending"
result: Optional[str] = None
feedback: Optional[str] = None
class BaseAgent:
"""Base agent class with core functionality"""
def __init__(self, name: str, role: AgentRole, system_prompt: str):
self.name = name
self.role = role
self.system_prompt = system_prompt
self.memory: List[Dict] = []
async def process(self, task: Task) -> str:
prompt = f"{self.system_prompt}nnTask: {task.description}nContext: {json.dumps(task.context)}"
result = await self._simulate_llm_call(prompt, task)
self.memory.append({
"task_id": task.id,
"input": task.description,
"output": result,
"timestamp": time.time()
})
return result
async def _simulate_llm_call(self, prompt: str, task: Task) -> str:
"""Call Google Gemini API for real LLM processing"""
try:
model = genai.GenerativeModel('gemini-1.5-flash')
enhanced_prompt = self._create_role_prompt(prompt, task)
response = await asyncio.to_thread(
lambda: model.generate_content(enhanced_prompt)
)
return response.text.strip()
except Exception as e:
print(f"β οΈ Gemini API error for {self.role.value}: {str(e)}")
return self._get_fallback_response(task)
def _create_role_prompt(self, base_prompt: str, task: Task) -> str:
"""Create enhanced role-specific prompts for Gemini"""
role_instructions = {
AgentRole.PLANNER: "You are a strategic planning expert. Create detailed, actionable plans. Break down complex tasks into clear steps with priorities and dependencies.",
AgentRole.EXECUTOR: "You are a skilled executor. Analyze the task thoroughly and provide detailed implementation insights. Focus on practical solutions and potential challenges.",
AgentRole.EXPRESSER: "You are a professional communicator. Present information clearly, professionally, and engagingly. Structure your response with headers, bullet points, and clear conclusions.",
AgentRole.REVIEWER: "You are a quality assurance expert. Evaluate completeness, accuracy, and clarity. Provide specific, actionable improvement suggestions."
}
context_info = f"Previous context: {json.dumps(task.context, indent=2)}" if task.context else "No previous context"
return f"""
{role_instructions[self.role]}
{base_prompt}
{context_info}
Task to process: {task.description}
Provide a comprehensive, professional response appropriate for your role as {self.role.value}.
"""
def _get_fallback_response(self, task: Task) -> str:
"""Fallback responses if Gemini API is unavailable"""
fallbacks = {
AgentRole.PLANNER: f"STRATEGIC PLAN for '{task.description}': 1) Requirement analysis 2) Resource assessment 3) Implementation roadmap 4) Risk mitigation 5) Success metrics",
AgentRole.EXECUTOR: f"EXECUTION ANALYSIS for '{task.description}': Comprehensive analysis completed. Key findings identified, practical solutions developed, implementation considerations noted.",
AgentRole.EXPRESSER: f"PROFESSIONAL SUMMARY for '{task.description}': ## Analysis Completenn**Key Insights:** Detailed analysis performedn**Recommendations:** Strategic actions identifiedn**Next Steps:** Implementation ready",
AgentRole.REVIEWER: f"QUALITY REVIEW for '{task.description}': **Assessment:** High quality output achieved. **Strengths:** Comprehensive analysis, clear structure. **Suggestions:** Consider additional quantitative metrics."
}
return fallbacks[self.role]
We use enumerations to represent their professional capabilities to define four different agent roles, planners, executors, Expresser, and reviewers. We then create a task data level to manage task metadata including state, results, and feedback. The BaseAgent class is the core blueprint for all agents, enabling them to handle tasks, call the Gemini API with role-specific prompts, store it in memory, and gracefully return to the predefined response if the API fails. Check The complete code is here.
class PEERAgent:
"""PEER Pattern Implementation - Plan, Execute, Express, Review"""
def __init__(self):
self.planner = BaseAgent("Strategic Planner", AgentRole.PLANNER,
"You are a strategic planning agent. Break down complex tasks into actionable steps.")
self.executor = BaseAgent("Task Executor", AgentRole.EXECUTOR,
"You are an execution agent. Complete tasks efficiently using available tools and knowledge.")
self.expresser = BaseAgent("Result Expresser", AgentRole.EXPRESSER,
"You are a communication agent. Present results clearly and professionally.")
self.reviewer = BaseAgent("Quality Reviewer", AgentRole.REVIEWER,
"You are a quality assurance agent. Review outputs and provide improvement feedback.")
self.iteration_count = 0
self.max_iterations = 3
async def collaborate(self, task: Task) -> Dict[str, Any]:
"""Execute PEER collaboration pattern"""
results = {"iterations": [], "final_result": None}
while self.iteration_count = 1:
results["final_result"] = expression
break
self.iteration_count += 1
task.context["previous_feedback"] = review
return results
We implement peer mode, planning, execution, expression, and review through the Peeragent class, which coordinates four dedicated agents to solve collaborative task resolution. Each iteration spans all four stages, perfecting task output based on structured planning, execution, professional expression and quality review. We allow up to three iterations, and if the review represents high-quality completion, you can draw conclusions early, making the workflow both adaptable and effective. Check The complete code is here.
class MultiAgentOrchestrator:
"""Orchestrates multiple specialized agents"""
def __init__(self):
self.agents = {}
self.peer_system = PEERAgent()
self.task_queue = []
def register_agent(self, agent: BaseAgent):
"""Register a specialized agent"""
self.agents[agent.name] = agent
async def process_complex_task(self, description: str, domain: str = "general") -> Dict[str, Any]:
"""Process complex task using PEER pattern and domain agents"""
task = Task(
id=f"task_{int(time.time())}",
description=description,
context={"domain": domain, "complexity": "high"}
)
print(f"π Starting Complex Task Processing: {description}")
print("=" * 60)
peer_results = await self.peer_system.collaborate(task)
if domain in ["financial", "technical", "creative"]:
domain_agent = self._get_domain_agent(domain)
if domain_agent:
print(f"π§ Domain-Specific Processing ({domain})")
domain_result = await domain_agent.process(task)
peer_results["domain_enhancement"] = domain_result
return {
"task_id": task.id,
"original_request": description,
"peer_results": peer_results,
"status": "completed",
"processing_time": f"{len(peer_results['iterations'])} iterations"
}
def _get_domain_agent(self, domain: str) -> Optional[BaseAgent]:
"""Get domain-specific agent with enhanced Gemini prompts"""
domain_agents = {
"financial": BaseAgent("Financial Analyst", AgentRole.EXECUTOR,
"You are a senior financial analyst with expertise in market analysis, risk assessment, and investment strategies. Provide detailed financial insights with quantitative analysis."),
"technical": BaseAgent("Technical Expert", AgentRole.EXECUTOR,
"You are a lead software architect with expertise in system design, scalability, and best practices. Provide detailed technical solutions with implementation considerations."),
"creative": BaseAgent("Creative Director", AgentRole.EXPRESSER,
"You are an award-winning creative director with expertise in brand strategy, content creation, and innovative campaigns. Generate compelling and strategic creative solutions.")
}
return domain_agents.get(domain)
class KnowledgeBase:
"""Simple knowledge management system"""
def __init__(self):
self.knowledge = {
"financial_analysis": ["Risk assessment", "Portfolio optimization", "Market analysis"],
"technical_development": ["System architecture", "Code optimization", "Security protocols"],
"creative_content": ["Brand storytelling", "Visual design", "Content strategy"]
}
def get_domain_knowledge(self, domain: str) -> List[str]:
return self.knowledge.get(domain, ["General knowledge"])
async def run_advanced_demo():
orchestrator = MultiAgentOrchestrator()
knowledge_base = KnowledgeBase()
print("nπ DEMO 1: Financial Analysis with PEER Pattern")
print("-" * 40)
financial_task = "Analyze the potential impact of rising interest rates on tech stocks portfolio"
result1 = await orchestrator.process_complex_task(financial_task, "financial")
print(f"nβ
Task Completed: {result1['processing_time']}")
print(f"Final Result: {result1['peer_results']['final_result']}")
print("nπ» DEMO 2: Technical Problem Solving")
print("-" * 40)
technical_task = "Design a scalable microservices architecture for a high-traffic e-commerce platform"
result2 = await orchestrator.process_complex_task(technical_task, "technical")
print(f"nβ
Task Completed: {result2['processing_time']}")
print(f"Final Result: {result2['peer_results']['final_result']}")
print("nπ¨ DEMO 3: Creative Content with Multi-Agent Collaboration")
print("-" * 40)
creative_task = "Create a comprehensive brand strategy for a sustainable fashion startup"
result3 = await orchestrator.process_complex_task(creative_task, "creative")
print(f"nβ
Task Completed: {result3['processing_time']}")
print(f"Final Result: {result3['peer_results']['final_result']}")
print("nπ§ AGENT MEMORY & LEARNING")
print("-" * 40)
print(f"Planner processed {len(orchestrator.peer_system.planner.memory)} tasks")
print(f"Executor processed {len(orchestrator.peer_system.executor.memory)} tasks")
print(f"Expresser processed {len(orchestrator.peer_system.expresser.memory)} tasks")
print(f"Reviewer processed {len(orchestrator.peer_system.reviewer.memory)} tasks")
return {
"demo_results": [result1, result2, result3],
"agent_stats": {
"total_tasks": 3,
"success_rate": "100%",
"avg_iterations": sum(len(r['peer_results']['iterations']) for r in [result1, result2, result3]) / 3
}
}
def explain_peer_pattern():
"""Explain the PEER pattern in detail"""
explanation = """
π PEER Pattern Explained:
P - PLAN: Strategic decomposition of complex tasks
E - EXECUTE: Systematic implementation using tools and knowledge
E - EXPRESS: Clear, structured communication of results
R - REVIEW: Quality assurance and iterative improvement
This pattern enables:
β
Better task decomposition
β
Systematic execution
β
Professional output formatting
β
Continuous quality improvement
"""
print(explanation)
def show_architecture():
"""Display the multi-agent architecture"""
architecture = """
ποΈ agentUniverse Architecture:
π Task Input
β
π― PEER System
βββ Planner Agent
βββ Executor Agent
βββ Expresser Agent
βββ Reviewer Agent
β
π§ Domain Specialists
βββ Financial Analyst
βββ Technical Expert
βββ Creative Director
β
π Knowledge Base
β
π Results & Analytics
"""
print(architecture)
We bring everything together to a multi-live configuration planner, who coordinates peer systems and calls domain-specific agents, such as financial analysts or technologists, if needed. The orchestrator handles each complex task by first leveraging peer patterns and then enhancing the results with expertise. We also define a simple knowledge base to support domain-aware reasoning. In the run_advanced_demo() function, we test the complete pipeline through three tasks (financial, technical and creative), while capturing agent performance and iterative metrics to demonstrate the functionality and versatility of our multi-agent setup. Check The complete code is here.
if __name__ == "__main__":
print("π‘ Get your FREE API key at:
print("π Make sure to replace 'your-gemini-api-key-here' with your actual key!")
if GEMINI_API_KEY == 'your-gemini-api-key-here':
print("β οΈ WARNING: Please set your Gemini API key first!")
print(" 1. Go to
print(" 2. Create a free API key")
print(" 3. Replace 'your-gemini-api-key-here' with your key")
print(" 4. Re-run the tutorial")
else:
print("β
API key configured! Starting tutorial...")
explain_peer_pattern()
show_architecture()
print("nβ³ Running Advanced Demo with Gemini AI (This may take a moment)...")
try:
import nest_asyncio
nest_asyncio.apply()
demo_results = asyncio.run(run_advanced_demo())
print("nπ TUTORIAL COMPLETED SUCCESSFULLY!")
print("=" * 50)
print(f"π Performance Summary:")
print(f" β’ Tasks Processed: {demo_results['agent_stats']['total_tasks']}")
print(f" β’ Success Rate: {demo_results['agent_stats']['success_rate']}")
print(f" β’ Avg Iterations: {demo_results['agent_stats']['avg_iterations']:.1f}")
print(f" β’ Powered by: Google Gemini (FREE)")
print("nπ‘ Key Takeaways:")
print(" β’ PEER pattern enables systematic problem-solving")
print(" β’ Multi-agent collaboration improves output quality")
print(" β’ Domain expertise integration enhances specialization")
print(" β’ Iterative refinement ensures high-quality results")
print(" β’ Gemini provides powerful, free AI capabilities")
except ImportError:
print("π Note: Install nest_asyncio for full async support in Colab")
print("Run: !pip install nest_asyncio")
except Exception as e:
print(f"β οΈ Error running demo: {str(e)}")
print("This might be due to API key configuration or network issues.")
print("nπ Next Steps:")
print(" β’ Customize agents for your specific domain")
print(" β’ Experiment with different Gemini models (gemini-pro, gemini-1.5-flash)")
print(" β’ Build production-ready multi-agent applications")
We end the tutorial by initializing the system, verifying the Gemini API keys, and executing a full peer-based multi-agent workflow. We explained the architecture and schema before running the demo, and upon successful completion, we will show a performance summary and key points.
In summary, we successfully demonstrate how multi-agent systems systematically solve complex problems with the help of domain-specific reasoning, structured communication and iterative quality checks. We can see the collaborative capabilities of peer frameworks and witness how Gemini can enhance each agentβs output. Through this experience, we realize the potential of modular AI systems in creating scalable, reliable and smart applications ready for realistic deployment.
Check The complete code is here. Check out ours anytime Tutorials, codes and notebooks for github pages. Also, please stay tuned for us twitter And don’t forget to join us 100K+ ml reddit And subscribe Our newsletter.
Asif Razzaq is CEO of Marktechpost Media Inc. As a visionary entrepreneur and engineer, ASIF is committed to harnessing the potential of artificial intelligence to achieve social benefits. His recent effort is to launch Marktechpost, an artificial intelligence media platform that has an in-depth coverage of machine learning and deep learning news that can sound both technically, both through technical voices and be understood by a wide audience. The platform has over 2 million views per month, demonstrating its popularity among its audience.
