Skip to main content

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 using ThreadPoolExecutor:

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_concurrently with high worker count
  • CPU-bound tasks: Use run_agents_concurrently with 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

2. Configure Workers Appropriately

3. Implement Graceful Degradation

4. Monitor Resource Usage

5. Use Appropriate Timeouts