> ## Documentation Index
> Fetch the complete documentation index at: https://docs.swarms.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Multi-Agent Best Practices

> Production-grade best practices for using the Swarms API effectively. Learn how to choose the right swarm architecture, optimize costs, and implement robust error handling.

This comprehensive guide outlines production-grade best practices for using the Swarms API effectively. Learn how to choose the right swarm architecture, optimize costs, and implement robust error handling.

## Quick Reference Cards

### Swarm Types

**Available Swarm Architectures**

| Swarm Type            | Best For                 | Use Cases                                                                                                              |
| --------------------- | ------------------------ | ---------------------------------------------------------------------------------------------------------------------- |
| `AgentRearrange`      | Dynamic workflows        | • Complex task decomposition<br />• Adaptive processing<br />• Multi-stage analysis<br />• Dynamic resource allocation |
| `MixtureOfAgents`     | Diverse expertise        | • Cross-domain problems<br />• Comprehensive analysis<br />• Multi-perspective tasks<br />• Research synthesis         |
| `BatchedGridWorkflow` | Data processing          | • Financial analysis<br />• Data transformation<br />• Batch calculations<br />• Report generation                     |
| `SequentialWorkflow`  | Linear processes         | • Document processing<br />• Step-by-step analysis<br />• Quality control<br />• Content pipeline                      |
| `ConcurrentWorkflow`  | Parallel tasks           | • Batch processing<br />• Independent analyses<br />• High-throughput needs<br />• Multi-market analysis               |
| `GroupChat`           | Collaborative solving    | • Brainstorming<br />• Decision making<br />• Problem solving<br />• Strategy development                              |
| `MultiAgentRouter`    | Task distribution        | • Load balancing<br />• Specialized processing<br />• Resource optimization<br />• Service routing                     |
| `HierarchicalSwarm`   | Complex organization     | • Project management<br />• Research analysis<br />• Enterprise workflows<br />• Team automation                       |
| `MajorityVoting`      | Consensus needs          | • Quality assurance<br />• Decision validation<br />• Risk assessment<br />• Content moderation                        |
| `DebateWithJudge`     | Adversarial review       | • Contested decisions<br />• Red-teaming<br />• Argument stress-testing                                                |
| `HeavySwarm`          | Deep multi-pass analysis | • High-stakes research<br />• Exhaustive due diligence                                                                 |
| `RoundRobin`          | Even task rotation       | • Fair load distribution<br />• Sequential specialist review                                                           |

See [Available Architectures](/docs/documentation/multi-agent/available-architectures) for the full, current list of swarm types.

### Application Patterns

**Specialized Application Configurations**

| Application           | Recommended Swarm    | Benefits                                                                                       |
| --------------------- | -------------------- | ---------------------------------------------------------------------------------------------- |
| **Team Automation**   | `HierarchicalSwarm`  | • Automated team coordination<br />• Clear responsibility chain<br />• Scalable team structure |
| **Research Pipeline** | `SequentialWorkflow` | • Structured research process<br />• Quality control at each stage<br />• Comprehensive output |
| **Trading System**    | `ConcurrentWorkflow` | • Multi-market coverage<br />• Real-time analysis<br />• Risk distribution                     |
| **Content Factory**   | `MixtureOfAgents`    | • Automated content creation<br />• Consistent quality<br />• High throughput                  |

### Cost Optimization

**Advanced Cost Management Strategies**

| Strategy           | Implementation                 | Impact                                      |
| ------------------ | ------------------------------ | ------------------------------------------- |
| Batch Processing   | Group related tasks            | 20-30% cost reduction                       |
| Off-peak Usage     | Schedule for 8 PM - 6 AM PT    | 50% cost reduction (Swarm Completions only) |
| Token Optimization | Precise prompts, focused tasks | 10-20% cost reduction                       |
| Caching            | Store reusable results         | 30-40% cost reduction                       |
| Agent Optimization | Use minimum required agents    | 15-25% cost reduction                       |
| Smart Routing      | Route to specialized agents    | 10-15% cost reduction                       |
| Prompt Engineering | Optimize input tokens          | 15-20% cost reduction                       |

### Service Tiers

**Choosing the Right Service Tier**

| Tier                         | Best For                                                                     | Benefits                                                               | Considerations                                                     |
| ---------------------------- | ---------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------ |
| Standard                     | • Real-time processing<br />• Time-sensitive tasks<br />• Critical workflows | • Immediate execution<br />• Higher priority<br />• Predictable timing | • Higher cost<br />• 5-min timeout                                 |
| Off-peak (Swarm Completions) | • Batch processing<br />• Non-urgent tasks<br />• Cost-sensitive workloads   | • 50% cost reduction on tokens<br />• 8 PM - 6 AM PT                   | • Only applies to Swarm Completions<br />• Time window restriction |

### Industry Solutions

**Industry-Specific Swarm Patterns**

| Industry       | Use Case                     | Applications                                                                                         |
| -------------- | ---------------------------- | ---------------------------------------------------------------------------------------------------- |
| **Finance**    | Automated trading desk       | • Portfolio management<br />• Risk assessment<br />• Market analysis<br />• Trading execution        |
| **Healthcare** | Clinical workflow automation | • Patient analysis<br />• Diagnostic support<br />• Treatment planning<br />• Follow-up care         |
| **Legal**      | Legal document processing    | • Document review<br />• Case analysis<br />• Contract review<br />• Compliance checks               |
| **E-commerce** | E-commerce operations        | • Product management<br />• Pricing optimization<br />• Customer support<br />• Inventory management |

### Error Handling

**Advanced Error Management Strategies**

| Error Code | Strategy          | Recovery Pattern                       |
| ---------- | ----------------- | -------------------------------------- |
| 400        | Input Validation  | Pre-request validation with fallback   |
| 401        | Auth Management   | Secure key rotation and storage        |
| 429        | Rate Limiting     | Exponential backoff with queuing       |
| 500        | Resilience        | Retry with circuit breaking            |
| 503        | High Availability | Multi-region redundancy                |
| 504        | Timeout Handling  | Adaptive timeouts with partial results |

## Choosing the Right Swarm Architecture

### Decision Framework

Use this framework to select the optimal swarm architecture for your use case:

1. **Task Complexity Analysis**
   * Complex tasks → `HierarchicalSwarm` or `MultiAgentRouter`
   * Dynamic tasks → `AgentRearrange`

2. **Workflow Pattern**
   * Linear processes → `SequentialWorkflow`
   * Parallel operations → `ConcurrentWorkflow`
   * Collaborative tasks → `GroupChat`

3. **Domain Requirements**
   * Multi-domain expertise → `MixtureOfAgents`
   * Data processing → `BatchedGridWorkflow`
   * Quality assurance → `MajorityVoting`

### Industry-Specific Recommendations

#### Finance

**Financial Applications**

* Risk Analysis: `HierarchicalSwarm`
* Market Research: `MixtureOfAgents`
* Trading Strategies: `ConcurrentWorkflow`
* Portfolio Management: `BatchedGridWorkflow`

#### Healthcare

**Healthcare Applications**

* Patient Analysis: `SequentialWorkflow`
* Research Review: `MajorityVoting`
* Treatment Planning: `GroupChat`
* Medical Records: `MultiAgentRouter`

#### Legal

**Legal Applications**

* Document Review: `SequentialWorkflow`
* Case Analysis: `MixtureOfAgents`
* Compliance Check: `HierarchicalSwarm`
* Contract Analysis: `ConcurrentWorkflow`

## Production Best Practices

### Best Practices Summary

**Recommended Patterns**

* Use appropriate swarm types for tasks
* Implement robust error handling
* Monitor and log executions
* Cache repeated results
* Rotate API keys regularly
* Choose appropriate service tier based on task urgency
* Schedule non-urgent tasks during off-peak hours (8 PM - 6 AM PT) for Swarm Completions to benefit from night-time discount

**Anti-patterns to Avoid**

* Hardcoding API keys
* Ignoring rate limits
* Missing error handling
* Excessive agent count
* Inadequate monitoring
* Not implementing retry logic for failed requests

### Performance Benchmarks

**Typical Performance Metrics**

| Metric         | Target Range | Warning Threshold |
| -------------- | ------------ | ----------------- |
| Response Time  | \< 2s        | > 5s              |
| Success Rate   | > 99%        | \< 95%            |
| Cost per Task  | \< \$0.05    | > \$0.10          |
| Cache Hit Rate | > 80%        | \< 60%            |
| Error Rate     | \< 1%        | > 5%              |
| Retry Rate     | \< 10%       | > 30%             |

### Additional Resources

**Useful Links**

* [API Dashboard](https://swarms.world/platform/api-keys)
