Overview
Swarms provides powerful utilities for scaling agent execution horizontally through concurrent and parallel processing. This guide covers the different scaling patterns, performance optimization techniques, and best practices for running agents at scale.Scaling Patterns
Swarms offers multiple execution patterns for different scaling scenarios:| Pattern | Use Case | Max Workers | Execution |
|---|---|---|---|
| Concurrent | I/O-bound tasks, API calls | 95% of CPU cores | Thread-based |
| Async | High-throughput async operations | Configurable | Event loop |
| Batch | Large-scale processing | Configurable | Batched |
| Grid | Different tasks per agent | 90% of CPU cores | Thread-based |
| UVLoop | Ultra-high performance async | 95% of CPU cores | Optimized event loop |
Concurrent Execution
Basic Concurrent Execution
Run multiple agents on the same task concurrently usingThreadPoolExecutor:
Concurrent with Dictionary Output
Get results as a dictionary mapping agent names to outputs:Concurrent with Images
Asynchronous Execution
Basic Async Execution
High-Performance with UVLoop
For maximum async performance, use uvloop (Linux/macOS) or winloop (Windows):Batch Processing
Batched Concurrent Execution
Process agents in batches to avoid resource exhaustion:Grid Execution (Different Tasks)
Run different tasks across different agents:Batch with Agent-Task Pairs
Queue-Based Scaling
For production deployments, use AOP’s queue-based execution:Queue Configuration for Scale
Performance Optimization
Worker Configuration
Dynamic Context Window
Optimize token usage with dynamic context windows:Memory Management
Batch Size Tuning
Load Balancing
Round-Robin Distribution
Priority-Based Distribution
Monitoring at Scale
Real-Time Metrics
Throughput Monitoring
Best Practices
1. Choose the Right Pattern
- I/O-bound tasks (API calls): Use
run_agents_concurrentlywith high worker count - CPU-bound tasks: Use
run_agents_concurrentlywith worker count = CPU cores - Async workloads: Use
run_agents_concurrently_uvloop - Large scale: Use AOP with queue-based execution
- Mixed tasks: Use
batched_grid_agent_execution