cc-agent-sdk 0.1.7

claude agent sdk
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
//! Example: Error Recovery Patterns
//!
//! This example demonstrates error recovery strategies and patterns
//! for building resilient applications with the Claude Agent SDK.
//!
//! What it demonstrates:
//! 1. Retry with exponential backoff
//! 2. Circuit breaker pattern
//! 3. Graceful degradation
//! 4. Error context preservation
//! 5. Recovery strategy selection

use anyhow::Result;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Simulated error types that can occur
#[derive(Debug, Clone)]
enum SimulatedError {
    NetworkTimeout,
    RateLimitExceeded { retry_after_secs: u64 },
    InvalidApiKey,
    ServiceUnavailable,
    ContextTooLong,
    Unknown(String),
}

impl std::fmt::Display for SimulatedError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NetworkTimeout => write!(f, "Network timeout"),
            Self::RateLimitExceeded { retry_after_secs } => {
                write!(f, "Rate limit exceeded, retry after {}s", retry_after_secs)
            }
            Self::InvalidApiKey => write!(f, "Invalid API key"),
            Self::ServiceUnavailable => write!(f, "Service unavailable"),
            Self::ContextTooLong => write!(f, "Context too long"),
            Self::Unknown(msg) => write!(f, "Unknown error: {}", msg),
        }
    }
}

impl std::error::Error for SimulatedError {}

/// Recovery strategy types
#[derive(Debug, Clone)]
enum RecoveryStrategy {
    /// Retry immediately
    RetryImmediate,
    /// Retry with exponential backoff
    RetryWithBackoff { max_attempts: usize, base_delay_ms: u64 },
    /// Wait for specified duration
    WaitThenRetry { delay_secs: u64 },
    /// Use fallback value
    UseFallback(String),
    /// Fail immediately
    FailFast,
}

/// Circuit breaker state
#[derive(Debug, Clone, PartialEq)]
enum CircuitState {
    Closed,
    Open { until: Instant },
    HalfOpen,
}

/// Circuit breaker for preventing cascading failures
#[derive(Debug)]
struct CircuitBreaker {
    state: CircuitState,
    failure_count: u64,
    success_count: u64,
    failure_threshold: u64,
    recovery_timeout: Duration,
    last_failure: Option<Instant>,
}

impl CircuitBreaker {
    fn new(failure_threshold: u64, recovery_timeout: Duration) -> Self {
        Self {
            state: CircuitState::Closed,
            failure_count: 0,
            success_count: 0,
            failure_threshold,
            recovery_timeout,
            last_failure: None,
        }
    }

    fn can_execute(&mut self) -> bool {
        match &self.state {
            CircuitState::Closed => true,
            CircuitState::Open { until } => {
                if Instant::now() >= *until {
                    self.state = CircuitState::HalfOpen;
                    true
                } else {
                    false
                }
            }
            CircuitState::HalfOpen => true,
        }
    }

    fn record_success(&mut self) {
        self.success_count += 1;
        if self.state == CircuitState::HalfOpen {
            self.state = CircuitState::Closed;
            self.failure_count = 0;
        }
    }

    fn record_failure(&mut self) {
        self.failure_count += 1;
        self.last_failure = Some(Instant::now());

        if self.failure_count >= self.failure_threshold {
            self.state = CircuitState::Open {
                until: Instant::now() + self.recovery_timeout,
            };
        }
    }

    fn state(&self) -> &CircuitState {
        &self.state
    }
}

fn main() -> Result<()> {
    println!("=== Error Recovery Patterns Examples ===\n");

    retry_with_backoff_example()?;
    circuit_breaker_example()?;
    graceful_degradation_example()?;
    error_context_preservation_example()?;
    recovery_strategy_selection_example()?;

    Ok(())
}

/// Demonstrates retry with exponential backoff
fn retry_with_backoff_example() -> Result<()> {
    println!("=== Retry with Exponential Backoff ===\n");

    let strategy = RecoveryStrategy::RetryWithBackoff {
        max_attempts: 5,
        base_delay_ms: 100,
    };

    println!("Strategy: {:?}", strategy);
    println!();

    // Simulate an operation that eventually succeeds
    let attempt_count = Arc::new(AtomicU64::new(0));
    let mut total_delay = 0u64;

    println!("Attempting operation with retry...");

    for attempt in 1..=5 {
        let current_attempt = attempt_count.fetch_add(1, Ordering::SeqCst) + 1;

        // Simulate failure for first 3 attempts
        let result = if current_attempt <= 3 {
            Err(SimulatedError::NetworkTimeout)
        } else {
            Ok("Operation succeeded!")
        };

        match result {
            Ok(response) => {
                println!("  Attempt {}: {}", attempt, response);
                break;
            }
            Err(e) => {
                if attempt < 5 {
                    let delay_ms = 100 * 2u64.pow(attempt as u32 - 1);
                    total_delay += delay_ms;
                    println!(
                        "  Attempt {}: {} - retrying in {}ms",
                        attempt, e, delay_ms
                    );
                    // In real code: tokio::time::sleep(Duration::from_millis(delay_ms)).await;
                } else {
                    println!("  Attempt {}: {} - giving up", attempt, e);
                }
            }
        }
    }

    println!("\nTotal retry delay: {}ms", total_delay);
    println!();

    Ok(())
}

/// Demonstrates circuit breaker pattern
fn circuit_breaker_example() -> Result<()> {
    println!("=== Circuit Breaker Pattern ===\n");

    let mut circuit_breaker = CircuitBreaker::new(3, Duration::from_secs(30));

    println!("Circuit Breaker Configuration:");
    println!("  Failure threshold: 3");
    println!("  Recovery timeout: 30s");
    println!();

    // Simulate a series of operations
    let operations = vec![
        (true, "Request 1"),
        (false, "Request 2"),
        (false, "Request 3"),
        (false, "Request 4"), // Should trip circuit
        (true, "Request 5"), // Should be blocked
        (true, "Request 6"), // Should be blocked
    ];

    for (success, name) in operations {
        if circuit_breaker.can_execute() {
            println!("  {} - Executing...", name);

            if success {
                circuit_breaker.record_success();
                println!("    Success - Circuit: {:?}", circuit_breaker.state());
            } else {
                circuit_breaker.record_failure();
                println!("    Failure - Circuit: {:?}", circuit_breaker.state());
            }
        } else {
            println!("  {} - BLOCKED (Circuit Open)", name);
        }
    }

    println!("\nCircuit breaker prevents cascading failures by blocking");
    println!("requests when the service is unhealthy.");
    println!();

    Ok(())
}

/// Demonstrates graceful degradation
fn graceful_degradation_example() -> Result<()> {
    println!("=== Graceful Degradation ===\n");

    // Define fallback chain
    let fallback_chain = vec![
        ("Primary Model", true),   // Try primary first
        ("Secondary Model", true), // Fall back to secondary
        ("Cache", true),           // Use cached response
        ("Static Response", true), // Return static response
    ];

    println!("Fallback Chain:");
    for (i, (name, _)) in fallback_chain.iter().enumerate() {
        println!("  {}. {}", i + 1, name);
    }
    println!();

    // Simulate degradation scenario
    let failing_until = 2; // First 2 options fail

    for (i, (name, _)) in fallback_chain.iter().enumerate() {
        let attempt = i + 1;

        if attempt <= failing_until {
            println!("  Attempt {}: {} - FAILED (simulated error)", attempt, name);
        } else {
            println!("  Attempt {}: {} - SUCCESS", attempt, name);
            println!("  Using degraded response from: {}", name);
            break;
        }
    }

    println!("\nGraceful degradation ensures service remains available");
    println!("even when primary features fail, with reduced functionality.");
    println!();

    // Show feature flags for degradation
    println!("Feature Flags for Degradation:");
    println!("  - Full AI responses: disabled (using cache)");
    println!("  - Real-time streaming: disabled");
    println!("  - Session persistence: enabled");
    println!("  - Basic queries: enabled");
    println!();

    Ok(())
}

/// Demonstrates error context preservation
fn error_context_preservation_example() -> Result<()> {
    println!("=== Error Context Preservation ===\n");

    // Simulate an error chain
    #[derive(Debug)]
    struct ErrorContext {
        operation: String,
        timestamp: Instant,
        attempt: u32,
        previous_errors: Vec<String>,
        metadata: std::collections::HashMap<String, String>,
    }

    let context = ErrorContext {
        operation: "query".to_string(),
        timestamp: Instant::now(),
        attempt: 3,
        previous_errors: vec![
            "Network timeout at attempt 1".to_string(),
            "Rate limit at attempt 2".to_string(),
        ],
        metadata: {
            let mut map = std::collections::HashMap::new();
            map.insert("model".to_string(), "claude-sonnet-4".to_string());
            map.insert("session_id".to_string(), "sess_123".to_string());
            map.insert("user_id".to_string(), "user_456".to_string());
            map
        },
    };

    println!("Error Context:");
    println!("  Operation: {}", context.operation);
    println!("  Attempt: {}", context.attempt);
    println!("  Previous Errors:");
    for (i, err) in context.previous_errors.iter().enumerate() {
        println!("    {}: {}", i + 1, err);
    }
    println!("  Metadata:");
    for (key, value) in &context.metadata {
        println!("    {}: {}", key, value);
    }

    println!("\nContext preservation enables:");
    println!("  - Better debugging with full error history");
    println!("  - Intelligent retry decisions based on error patterns");
    println!("  - Proper error reporting to users");
    println!("  - Analytics and monitoring");
    println!();

    Ok(())
}

/// Demonstrates recovery strategy selection
fn recovery_strategy_selection_example() -> Result<()> {
    println!("=== Recovery Strategy Selection ===\n");

    // Map errors to recovery strategies
    let error_strategies = vec![
        (
            SimulatedError::NetworkTimeout,
            RecoveryStrategy::RetryWithBackoff {
                max_attempts: 3,
                base_delay_ms: 100,
            },
        ),
        (
            SimulatedError::RateLimitExceeded { retry_after_secs: 60 },
            RecoveryStrategy::WaitThenRetry { delay_secs: 60 },
        ),
        (
            SimulatedError::InvalidApiKey,
            RecoveryStrategy::FailFast,
        ),
        (
            SimulatedError::ServiceUnavailable,
            RecoveryStrategy::RetryWithBackoff {
                max_attempts: 5,
                base_delay_ms: 1000,
            },
        ),
        (
            SimulatedError::ContextTooLong,
            RecoveryStrategy::UseFallback("Please shorten your message.".to_string()),
        ),
    ];

    println!("Error → Recovery Strategy Mapping:\n");

    for (error, strategy) in &error_strategies {
        println!("Error: {}", error);
        println!("  Strategy: {:?}", strategy);
        println!();
    }

    // Strategy selection logic
    println!("Strategy Selection Logic:\n");

    let scenarios = vec![
        ("Temporary network issue", "Retry with backoff"),
        ("API rate limit hit", "Wait then retry"),
        ("Invalid credentials", "Fail fast, alert admin"),
        ("Service outage", "Circuit breaker, use fallback"),
        ("Input too large", "Truncate or reject with message"),
    ];

    for (scenario, action) in scenarios {
        println!("  Scenario: {}", scenario);
        println!("  Action: {}", action);
        println!();
    }

    // Composite recovery pattern
    println!("Composite Recovery Pattern:");
    println!("  1. Try primary method");
    println!("  2. If transient error → retry with backoff");
    println!("  3. If rate limited → wait and retry");
    println!("  4. If service unavailable → use circuit breaker");
    println!("  5. If permanent error → fail fast");
    println!("  6. If fallback available → use degraded response");
    println!();

    Ok(())
}

/// Retry executor with backoff (for real async usage)
#[allow(dead_code)]
async fn retry_with_backoff<T, E, F, Fut>(
    mut operation: F,
    max_attempts: usize,
    base_delay_ms: u64,
) -> std::result::Result<T, E>
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = std::result::Result<T, E>>,
    E: std::fmt::Display,
{
    let mut last_error = None;

    for attempt in 1..=max_attempts {
        match operation().await {
            Ok(result) => return Ok(result),
            Err(e) => {
                last_error = Some(e);

                if attempt < max_attempts {
                    let delay = Duration::from_millis(base_delay_ms * 2u64.pow(attempt as u32 - 1));
                    tokio::time::sleep(delay).await;
                }
            }
        }
    }

    // This is a bit of a hack since we can't easily return the error
    panic!("All retry attempts failed")
}

/// Timeout wrapper with fallback (for real async usage)
#[allow(dead_code)]
async fn with_timeout_and_fallback<T, F, Fut>(
    operation: F,
    timeout: Duration,
    fallback: T,
) -> T
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = std::result::Result<T, anyhow::Error>>,
    T: Clone,
{
    match tokio::time::timeout(timeout, operation()).await {
        Ok(Ok(result)) => result,
        Ok(Err(_)) | Err(_) => fallback,
    }
}