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: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 Async with uvloop
swarms does not ship a dedicated uvloop-specific execution function. For maximum async
throughput on Linux/macOS, install uvloop yourself and install it as the event loop
policy before calling the standard async helpers (run_agents_concurrently_async,
run_agent_async):
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_async(optionally withuvloopinstalled as the event loop policy) - Large scale: Use AOP with queue-based execution
- Mixed tasks: Use
batched_grid_agent_execution
2. Configure Workers Appropriately
3. Implement Graceful Degradation
4. Monitor Resource Usage
5. Use Appropriate Timeouts
Agent itself does not take a timeout constructor parameter. Enforce timeouts at the
execution layer instead — via AOP.add_agent(..., timeout=...) when deploying agents as
services (see Agent Orchestration Protocol),
or by bounding a future’s result when running concurrently: