paladin-ai 0.4.3

Enterprise AI orchestration framework with multi-agent coordination patterns
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# LLM Provider Expansion Guide

**Paladin Multi-Provider Support**

This document provides a comprehensive comparison of LLM providers supported by Paladin and guidance for configuring and using them effectively.

---

## Table of Contents

- [Overview]#overview
- [Provider Comparison]#provider-comparison
- [Configuration Guide]#configuration-guide
- [Use Case Recommendations]#use-case-recommendations
- [Migration Guide]#migration-guide
- [Performance Characteristics]#performance-characteristics

---

## Overview

Paladin supports multiple LLM providers out of the box, allowing you to choose the best provider for your specific needs. All providers implement the same `LlmPort` trait, making it easy to switch between them without changing your application logic.

### Supported Providers

1. **OpenAI** (GPT-4, GPT-3.5-turbo, GPT-4 Vision)
2. **DeepSeek** (DeepSeek-Chat, DeepSeek-Coder)
3. **Anthropic** (Claude 3.5 Sonnet, Claude 3 Opus/Haiku)

---

## Provider Comparison

| Feature | OpenAI | DeepSeek | Anthropic |
|---------|--------|----------|-----------|
| **Streaming** | ✅ Yes | ✅ Yes | ✅ Yes |
| **Tool Calling** | ✅ Yes | ✅ Yes | ✅ Yes |
| **Function Calling** | ✅ Yes | ✅ Yes | ✅ Yes |
| **Vision/Images** | ✅ GPT-4V | ❌ No | ✅ Claude 3+ |
| **Max Context** | 128K (GPT-4) | 64K | 200K (Claude 3) |
| **Best For** | General purpose, production | Cost-effective, reasoning | Safety-critical, analysis |
| **Pricing** | $$ | $ | $$$ |
| **Latency** | Low | Low | Low-Medium |

### Detailed Feature Matrix

#### OpenAI
- **Strengths**:
  - Most mature ecosystem with extensive tooling
  - Wide range of models (GPT-4, GPT-3.5-turbo, GPT-4 Vision)
  - Excellent for general-purpose applications
  - Strong vision/multimodal capabilities
  - Large community and documentation

- **Limitations**:
  - Higher cost compared to alternatives
  - Context window smaller than Claude
  - Rate limiting on free tier

- **Ideal Use Cases**:
  - Production deployments requiring reliability
  - Applications needing vision/image analysis
  - General-purpose AI assistants
  - Well-documented, standard use cases

#### DeepSeek
- **Strengths**:
  - Most cost-effective option
  - Strong reasoning and code generation
  - High throughput capabilities
  - Good for analytical tasks
  - Competitive performance at lower cost

- **Limitations**:
  - Smaller context window (64K)
  - No vision support
  - Newer ecosystem, less community resources

- **Ideal Use Cases**:
  - Cost-sensitive deployments
  - Code generation and analysis
  - Logical reasoning tasks
  - High-volume/batch processing
  - Internal tooling and development

#### Anthropic Claude
- **Strengths**:
  - Largest context window (200K tokens)
  - Strong safety and ethical guidelines
  - Excellent for complex analysis
  - Superior long-document processing
  - Strong instruction following

- **Limitations**:
  - Higher cost
  - Claude-specific API differences (system messages separate)
  - Requires max_tokens parameter

- **Ideal Use Cases**:
  - Safety-critical applications
  - Complex document analysis
  - Long-context reasoning
  - Compliance and governance
  - Medical/legal/financial applications

---

## Configuration Guide

### Environment Variables

All providers can be configured via environment variables:

```bash
# OpenAI
export OPENAI_API_KEY="sk-..."

# DeepSeek
export DEEPSEEK_API_KEY="..."
export DEEPSEEK_BASE_URL="https://api.deepseek.com/v1"  # Optional
export DEEPSEEK_MODEL="deepseek-chat"                    # Optional

# Anthropic
export ANTHROPIC_API_KEY="sk-ant-..."
export ANTHROPIC_BASE_URL="https://api.anthropic.com/v1" # Optional
export ANTHROPIC_MODEL="claude-3-5-sonnet-20241022"      # Optional
```

### Configuration Files

Add provider configurations to `config.yml`:

```yaml
llm:
  # Default provider if multiple are configured
  default_provider: "openai"

  openai:
    api_key: "${OPENAI_API_KEY}"
    base_url: "https://api.openai.com/v1"
    model: "gpt-4"
    timeout_seconds: 30

  deepseek:
    api_key: "${DEEPSEEK_API_KEY}"
    base_url: "https://api.deepseek.com/v1"
    model: "deepseek-chat"
    timeout_seconds: 60

  anthropic:
    api_key: "${ANTHROPIC_API_KEY}"
    base_url: "https://api.anthropic.com/v1"
    model: "claude-3-5-sonnet-20241022"
    timeout_seconds: 30
```

### Programmatic Configuration

#### OpenAI

```rust
use paladin::infrastructure::adapters::llm::openai_adapter::OpenAILlmAdapter;
use std::time::Duration;

let adapter = OpenAILlmAdapter::new(
    api_key,
    None, // Use default base URL
    Some(Duration::from_secs(30))
)?;
```

#### DeepSeek

```rust
use paladin::infrastructure::adapters::llm::deepseek_adapter::{
    DeepSeekAdapter, DeepSeekConfig
};

// From environment
let config = DeepSeekConfig::from_env()?;
let adapter = DeepSeekAdapter::new(config)?;

// Or custom
let config = DeepSeekConfig::new(
    api_key,
    "https://api.deepseek.com/v1".to_string(),
    "deepseek-chat".to_string()
);
let adapter = DeepSeekAdapter::new(config)?;
```

#### Anthropic

```rust
use paladin::infrastructure::adapters::llm::anthropic_adapter::{
    AnthropicAdapter, AnthropicConfig
};

// From environment
let config = AnthropicConfig::from_env()?;
let adapter = AnthropicAdapter::new(config)?;

// Or custom
let config = AnthropicConfig::new(
    api_key,
    "https://api.anthropic.com/v1".to_string(),
    "claude-3-5-sonnet-20241022".to_string()
);
let adapter = AnthropicAdapter::new(config)?;
```

---

## Use Case Recommendations

### When to Use OpenAI

**Best for:**
- General-purpose AI applications
- Production deployments requiring proven reliability
- Applications needing vision/image analysis
- Multimodal applications
- Projects with complex tooling requirements

**Example Use Cases:**
- Customer support chatbots
- Content generation systems
- Image analysis and description
- General AI assistants
- Document Q&A systems

### When to Use DeepSeek

**Best for:**
- Cost-sensitive deployments
- Code generation and analysis
- Logical reasoning tasks
- High-volume batch processing
- Internal development tools

**Example Use Cases:**
- Code review automation
- Test generation
- Documentation generation
- Internal knowledge bases
- Analytical pipelines

### When to Use Anthropic Claude

**Best for:**
- Safety-critical applications
- Long-document analysis
- Complex reasoning tasks
- Compliance-sensitive domains
- High-stakes decision support

**Example Use Cases:**
- Legal document analysis
- Medical record processing
- Financial compliance checking
- Research paper analysis
- Complex contract review

---

## Migration Guide

### From OpenAI to DeepSeek

DeepSeek uses an OpenAI-compatible API, making migration straightforward:

```rust
// Before (OpenAI)
let llm_port = Arc::new(OpenAILlmAdapter::new(
    openai_key,
    None,
    Some(Duration::from_secs(30))
)?);

// After (DeepSeek)
let config = DeepSeekConfig::from_env()?;
let llm_port = Arc::new(DeepSeekAdapter::new(config)?);

// Your Paladin code remains the same
let paladin = PaladinBuilder::new(llm_port)
    .system_prompt("Your prompt")
    .build()?;
```

**Considerations:**
- DeepSeek has no vision support
- Context window is 64K vs 128K for GPT-4
- Response style may differ slightly

### From OpenAI to Anthropic

Anthropic Claude requires some adjustments due to API differences:

```rust
// Before (OpenAI)
let llm_port = Arc::new(OpenAILlmAdapter::new(
    openai_key,
    None,
    Some(Duration::from_secs(30))
)?);

// After (Anthropic)
let config = AnthropicConfig::from_env()?;
let llm_port = Arc::new(AnthropicAdapter::new(config)?);

// Your Paladin code remains the same
let paladin = PaladinBuilder::new(llm_port)
    .system_prompt("Your prompt")
    .build()?;
```

**Key Differences:**
- Claude requires `max_tokens` parameter (defaults to 4096)
- System messages are sent separately
- Larger context window (200K tokens)
- Different SSE streaming format

### Provider Fallback Pattern

Implement graceful fallback for higher reliability:

```rust
use paladin::paladin_ports::output::llm_port::LlmPort;
use std::sync::Arc;

fn create_llm_provider() -> Result<Arc<dyn LlmPort>, Box<dyn std::error::Error>> {
    // Try DeepSeek first (cost-effective)
    if let Ok(config) = DeepSeekConfig::from_env() {
        if let Ok(adapter) = DeepSeekAdapter::new(config) {
            return Ok(Arc::new(adapter));
        }
    }

    // Fallback to Anthropic (powerful)
    if let Ok(config) = AnthropicConfig::from_env() {
        if let Ok(adapter) = AnthropicAdapter::new(config) {
            return Ok(Arc::new(adapter));
        }
    }

    // Final fallback to OpenAI (default)
    let api_key = std::env::var("OPENAI_API_KEY")?;
    Ok(Arc::new(OpenAILlmAdapter::new(
        api_key,
        None,
        Some(Duration::from_secs(30))
    )?))
}
```

---

## Performance Characteristics

### Latency Comparison (Approximate)

| Provider | First Token (p50) | First Token (p95) | Throughput |
|----------|-------------------|-------------------|------------|
| OpenAI GPT-4 | 500-800ms | 1-2s | Medium |
| OpenAI GPT-3.5 | 200-400ms | 500ms-1s | High |
| DeepSeek | 300-600ms | 800ms-1.5s | High |
| Anthropic Claude | 400-700ms | 1-2s | Medium |

*Note: Actual performance varies based on request size, load, and region*

### Cost Comparison (Approximate)

**Per 1M Tokens (Input/Output):**

| Provider | Model | Input | Output |
|----------|-------|-------|--------|
| OpenAI | GPT-4 | $10 | $30 |
| OpenAI | GPT-3.5-turbo | $0.50 | $1.50 |
| DeepSeek | deepseek-chat | $0.10 | $0.20 |
| Anthropic | Claude 3.5 Sonnet | $3 | $15 |

*Prices are approximate and subject to change*

### Scaling Considerations

**OpenAI:**
- Rate limits: Tier-based (requests/min, tokens/min)
- Horizontal scaling: Good
- Burst capacity: Moderate

**DeepSeek:**
- Rate limits: Generous
- Horizontal scaling: Excellent (high throughput)
- Burst capacity: High

**Anthropic:**
- Rate limits: Tier-based
- Horizontal scaling: Good
- Burst capacity: Moderate

---

## Best Practices

### 1. Use Provider Capabilities

Query provider capabilities before attempting operations:

```rust
let caps = provider.get_capabilities();

if caps.supports_vision {
    // Send image-based requests
}

if caps.supports_streaming {
    // Use streaming for better UX
}
```

### 2. Set Appropriate Timeouts

Different providers may have different response times:

```rust
// Higher timeout for Claude with long contexts
let claude_config = AnthropicConfig::new(/* ... */);
// Timeout handled internally

// Standard timeout for others
let openai = OpenAILlmAdapter::new(
    api_key,
    None,
    Some(Duration::from_secs(30))
)?;
```

### 3. Handle Provider-Specific Errors

```rust
match provider.generate(&request).await {
    Ok(response) => // Handle response,
    Err(LlmError::RateLimitExceeded { retry_after }) => {
        tokio::time::sleep(Duration::from_secs(retry_after)).await;
        // Retry
    }
    Err(LlmError::AuthenticationError(_)) => {
        // Check API keys
    }
    Err(e) => // Handle other errors
}
```

### 4. Monitor Usage and Costs

```rust
let response = provider.generate(&request).await?;

// Log token usage
println!("Input tokens: {}", response.usage.prompt_tokens);
println!("Output tokens: {}", response.usage.completion_tokens);
println!("Total cost: ${}", calculate_cost(&response, provider_name));
```

---

## Troubleshooting

### Authentication Errors

**Issue:** `LlmError::AuthenticationError`

**Solutions:**
1. Verify API key is set correctly
2. Check API key has necessary permissions
3. Ensure API key hasn't expired
4. Verify base URL is correct for your region

### Rate Limiting

**Issue:** `LlmError::RateLimitExceeded`

**Solutions:**
1. Implement exponential backoff (built-in to adapters)
2. Consider upgrading API tier
3. Implement request queuing
4. Switch to provider with higher limits

### Timeout Errors

**Issue:** `LlmError::Timeout`

**Solutions:**
1. Increase timeout duration
2. Reduce request complexity
3. Check network connectivity
4. Consider switching to streaming mode

### Context Length Errors

**Issue:** `LlmError::InvalidRequest` (context too long)

**Solutions:**
1. Reduce input size
2. Switch to provider with larger context (Claude: 200K)
3. Implement context windowing
4. Summarize older conversation history

---

## Additional Resources

- [Paladin Examples]../examples/ - Working code examples
- [Contributing Providers Guide]./CONTRIBUTING_PROVIDERS.md - Add new providers
- [API Documentation]https://docs.rs/paladin - Full API reference
- [GitHub Issues]https://github.com/DF3NDR/paladin/issues - Report issues

---

**Last Updated:** January 2026  
**Version:** 0.1.0