Skip to content

Latest commit

 

History

History

README.md

Concurrent Agents Pattern

This demo showcases the Concurrent Agents pattern using the Azure AI Agent Framework. Concurrent workflows execute multiple agents in parallel (fan-out) and aggregate their results (fan-in), enabling parallel processing for improved efficiency.

Features

  • Fan-Out Execution: Same prompt dispatched to multiple agents simultaneously
  • Fan-In Aggregation: Results from all agents combined into final output
  • Parallel Processing: Multiple domain experts work concurrently
  • Custom Executors: Extend with specialized agent executor classes
  • Custom Aggregators: Control how results are combined

Samples Included

File Description
concurrent_agents.py Basic concurrent workflow with researcher, marketer, and legal agents
concurrent_custom_agent_executors.py Using custom executor classes that own ChatAgents
concurrent_custom_aggregator.py Custom aggregation logic for combining results
concurrent_participant_factory.py Participant factories for stateful workflows
concurrent_workflow_as_agent.py Wrap concurrent workflow as a reusable agent

Prerequisites

  • Python 3.10 or later
  • Azure OpenAI resource with a deployed model
  • Azure CLI authenticated (az login)

Setup

See the main README for complete setup instructions. Quick start:

# From project root
cp .env.example .env  # Then edit with your Azure OpenAI settings
python -m venv venv
venv\Scripts\activate  # Windows (or source venv/bin/activate on Linux/Mac)
pip install -r requirements.txt
az login

Running the Demos

# Basic concurrent workflow
python concurrent_agents.py

# With custom agent executors
python concurrent_custom_agent_executors.py

# With custom aggregator
python concurrent_custom_aggregator.py

# With participant factories
python concurrent_participant_factory.py

# Workflow as reusable agent
python concurrent_workflow_as_agent.py

How It Works

Concurrent Fan-Out/Fan-In Pattern

                    User Input
                        │
                   [Dispatcher]
                   /    │    \
                  ↓     ↓     ↓
            Researcher Marketer Legal
                  \     │     /
                   [Aggregator]
                        │
                   Final Output

Architecture

  1. ConcurrentBuilder: Constructs the workflow with parallel participants
  2. Default Dispatcher: Fans out the same user prompt to all agents
  3. Parallel Execution: All agents process the input simultaneously
  4. Default Aggregator: Combines results into list[ChatMessage]

Sample Output

===== Final Aggregated Conversation (messages) =====
------------------------------------------------------------
01 [user]:
We are launching a new budget-friendly electric bike for urban commuters.
------------------------------------------------------------
02 [researcher]:
**Insights:**
- Target Demographic: Urban commuters seeking affordable, eco-friendly transport
- Market Trends: E-bike sales growing with urbanization and sustainability focus
...
------------------------------------------------------------
03 [marketer]:
**Value Proposition:**
"Freedom to commute without breaking the bank"
...
------------------------------------------------------------
04 [legal]:
**Compliance Considerations:**
- Consumer safety regulations for electric vehicles
- Battery disposal and recycling requirements
...

Key Concepts

  • Domain Agents: Each agent specializes in a specific perspective (research, marketing, legal)
  • Custom Executors: Create classes that own ChatAgents with @handler decorators
  • AgentExecutorRequest/Response: Standard input/output types for custom executors
  • Workflow Completion: Completes when all participants become idle

Use Cases

  1. Multi-Perspective Analysis: Get insights from different domain experts
  2. Parallel Research: Multiple researchers investigating different aspects
  3. Review Processes: Legal, technical, and business reviews in parallel
  4. Content Generation: Multiple writers creating variations simultaneously

Troubleshooting

Authentication errors:

az login --tenant <your-tenant-id>

Module not found: Ensure root-level virtual environment is activated:

cd ..  # Go to project root
venv\Scripts\activate
cd 02-ConcurrentAgents
python concurrent_agents.py

Azure OpenAI errors:

  • Verify AZURE_OPENAI_ENDPOINT is correct
  • Ensure your deployment name matches AZURE_OPENAI_CHAT_DEPLOYMENT_NAME
  • Check that your Azure subscription has access to Azure OpenAI

Learn More