# LLM Providers in Multi-Swarm

Multi-Swarm supports multiple Language Model (LLM) providers, each offering unique capabilities and strengths. This guide explains how to configure and use different providers effectively.

## Configuration Management

As of version 1.0.5, Multi-Swarm provides a centralized configuration system for managing model settings:

```python
from multi_swarm.utils.config_manager import ConfigManager

# Initialize configuration manager
config_manager = ConfigManager()

# Get model configuration
model_config = config_manager.get_model_config(
    provider="claude",
    model_name="claude-3-5-sonnet-latest"
)

# Update task preferences
config_manager.update_task_preference(
    provider="claude",
    task="code_generation",
    model_name="claude-3-5-sonnet-latest"
)
```

For migration guidance, see the [Migration Guide](migration-guide.md).

## Supported Providers

### Claude (Anthropic)

Claude models are particularly strong in:
- Complex reasoning and analysis
- Code generation and review
- Technical writing and documentation
- Research synthesis

#### Available Models

1. **claude-3-5-sonnet-latest**
   - Balanced performance and cost
   - Best for general tasks and code generation
   - Recommended for most use cases
   ```python
   provider_config = {
       "model": "claude-3-5-sonnet-latest",
       "api_version": "2024-03",
       "max_tokens": 4096
   }
   ```

2. **claude-3-5-opus-latest**
   - Highest capability model
   - Best for complex research and analysis
   - Recommended for advanced tasks
   ```python
   provider_config = {
       "model": "claude-3-5-opus-latest",
       "api_version": "2024-03",
       "max_tokens": 4096
   }
   ```

### Gemini (Google)

Gemini models excel at:
- Fast processing and response
- Multimodal tasks
- Real-time operations
- Data analysis

#### Available Models

1. **gemini-2.0-flash-exp**
   - High-performance model
   - Best for real-time operations
   - Efficient processing
   ```python
   provider_config = {
       "model": "gemini-2.0-flash-exp",
       "api_version": "2024-01",
       "max_tokens": 4096
   }
   ```

## Task-Specific Model Selection

The framework now automatically selects the most appropriate model based on the agent's role and task:

### Development Tasks
```python
# Will automatically use claude-3-5-sonnet-latest
developer = Agent(
    name="Developer",
    description="Handles technical implementation"
)
```

### Research Tasks
```python
# Will automatically use claude-3-5-opus-latest
researcher = Agent(
    name="Researcher",
    description="Conducts in-depth analysis"
)
```

### Real-time Operations
```python
# Will automatically use gemini-2.0-flash-exp
operator = Agent(
    name="Operator",
    description="Handles real-time operations"
)
```

## Provider Configuration

### Environment Variables

Required environment variables for each provider:

```bash
# For Claude
ANTHROPIC_API_KEY=your_claude_key

# For Gemini
GOOGLE_API_KEY=your_gemini_key
```

### Configuration Options

Common configuration options for all providers:

```python
provider_config = {
    "model": "model_name",
    "api_version": "api_version",
    "max_tokens": 4096,
    "temperature": 0.7,
    "top_p": 0.95,
    "frequency_penalty": 0.0,
    "presence_penalty": 0.0
}
```

Provider-specific options:

#### Claude
```python
claude_config = {
    "model": "claude-3-5-sonnet-latest",
    "api_version": "2024-03",
    "max_tokens": 4096,
    "temperature": 0.7,
    "top_p": 0.95,
    "top_k": 40,
    "stop_sequences": ["Human:", "Assistant:"]
}
```

#### Gemini
```python
gemini_config = {
    "model": "gemini-2.0-flash-exp",
    "api_version": "2024-01",
    "max_tokens": 4096,
    "temperature": 0.7,
    "top_p": 0.95,
    "top_k": 40,
    "candidate_count": 1
}
```

## Best Practices

### Model Selection
1. Use Claude for:
   - Complex reasoning tasks
   - Code generation and review
   - Technical writing
   - Research synthesis

2. Use Gemini for:
   - Real-time operations
   - Quick responses
   - Data processing
   - Multimodal tasks

### Configuration
1. Set appropriate temperature:
   - 0.3-0.5 for precise tasks
   - 0.6-0.8 for creative tasks
   - 0.9+ for highly creative tasks

2. Adjust max_tokens based on task:
   - 1024 for short responses
   - 2048 for medium responses
   - 4096 for long responses

3. Use stop sequences effectively:
   - Define clear conversation boundaries
   - Prevent response continuation
   - Maintain consistent format

## Error Handling

The framework includes robust error handling for provider-specific issues:

```python
try:
    response = agent.generate_response(prompt)
except ProviderError as e:
    if isinstance(e, RateLimitError):
        # Handle rate limiting
        time.sleep(e.retry_after)
    elif isinstance(e, AuthenticationError):
        # Handle authentication issues
        logger.error("API key invalid or expired")
    elif isinstance(e, QuotaExceededError):
        # Handle quota issues
        logger.error("API quota exceeded")
    else:
        # Handle other provider errors
        logger.error(f"Provider error: {str(e)}")
```

## Monitoring and Logging

The framework provides built-in monitoring for provider usage:

```python
# Get provider usage metrics
metrics = agent.get_provider_metrics()

# Log provider performance
logger.info(f"Provider metrics: {metrics}")
```

## Custom Provider Integration

You can integrate custom providers by implementing the `BaseProvider` interface:

```python
from multi_swarm.providers import BaseProvider

class CustomProvider(BaseProvider):
    def __init__(self, config: dict):
        super().__init__(config)
        # Initialize custom provider

    async def generate(self, prompt: str) -> str:
        # Implement custom generation logic
        pass

    async def stream(self, prompt: str) -> AsyncIterator[str]:
        # Implement custom streaming logic
        pass
```

## Performance Optimization

Tips for optimizing provider performance:

1. Use streaming for long responses
2. Implement response caching
3. Set appropriate timeouts
4. Monitor token usage
5. Implement rate limiting
6. Use batching when possible

## Cost Management

Strategies for managing provider costs:

1. Monitor token usage
2. Use cheaper models for simple tasks
3. Implement usage quotas
4. Cache common responses
5. Use streaming to limit unnecessary tokens 