appam 0.1.1

High-throughput, traceable, reliable Rust agent framework for long-horizon AI sessions and easy extensibility
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//! Anthropic Messages API type definitions.
//!
//! Complete type system for the Anthropic Messages API, including requests,
//! responses, content blocks, tools, and streaming events.
//!
//! # API Version
//!
//! These types correspond to `anthropic-version: 2023-06-01`.
//!
//! # Key Differences from OpenRouter
//!
//! - System prompt is a top-level parameter, not a message role
//! - Content is an array of typed blocks, not simple strings
//! - Tool definitions use `input_schema` instead of `parameters`
//! - Streaming uses different event types
//! - Tool use is embedded in message content, not separate output items

use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

/// Messages API request.
///
/// Top-level request structure for creating a message with Claude.
///
/// # Required Fields
///
/// - `model`: Model identifier
/// - `max_tokens`: Maximum tokens to generate (strict limit)
/// - `messages`: Conversation history
///
/// # Important
///
/// - System prompt goes in `system` field, NOT as a message
/// - Messages must alternate user/assistant (consecutive turns auto-combined)
/// - `max_tokens` is enforced strictly (no auto-adjustment)
/// - Top-level `cache_control` is Anthropic's automatic prompt-caching helper
///   for the direct Anthropic and Azure Anthropic transports
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageRequest {
    /// Model identifier (e.g., "claude-sonnet-4-5")
    pub model: String,

    /// Maximum tokens to generate (strict limit)
    pub max_tokens: u32,

    /// Conversation messages
    ///
    /// Must alternate between user and assistant roles. Consecutive messages
    /// with the same role are automatically combined by the API.
    pub messages: Vec<Message>,

    /// Top-level prompt cache control.
    ///
    /// This mirrors Anthropic's request-level `cache_control` field. When
    /// present, Anthropic automatically applies the cache breakpoint to the
    /// last cacheable block in the request instead of requiring the client to
    /// manually inject `cache_control` into individual content blocks.
    ///
    /// The field is intentionally optional because requests without prompt
    /// caching should omit it entirely. AWS Bedrock uses block-level
    /// `cache_control` checkpoints instead of this top-level helper.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,

    /// System prompt (optional)
    ///
    /// Provides context and instructions. Can be a string or array of text blocks.
    /// This is NOT a message role - it's a top-level parameter.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<SystemPrompt>,

    /// Tool definitions (optional)
    ///
    /// Array of client tools and/or server tools (web search, bash, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Tool>>,

    /// Tool choice strategy (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,

    /// Enable streaming (optional, default: false)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// Temperature (0.0-1.0, optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// Top-p sampling (0.0-1.0, optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// Top-k sampling (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_k: Option<u32>,

    /// Custom stop sequences (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_sequences: Option<Vec<String>>,

    /// Extended thinking configuration (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<ThinkingParam>,

    /// Request metadata (optional)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<MetadataParam>,
}

/// System prompt parameter.
///
/// Can be either a simple string or an array of text blocks (for caching).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SystemPrompt {
    /// Simple string system prompt
    String(String),
    /// Array of system text blocks (for cache control)
    Blocks(Vec<SystemBlock>),
}

/// System text block.
///
/// Used when applying cache control to system prompts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemBlock {
    /// Block type (always "text")
    #[serde(rename = "type")]
    pub block_type: String,
    /// Text content
    pub text: String,
    /// Optional cache control
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

/// Cache control for prompt caching.
///
/// Marks content for caching to reduce latency and costs.
/// Can be applied to tools, system blocks, or message content blocks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheControl {
    /// Cache type (always "ephemeral")
    #[serde(rename = "type")]
    pub cache_type: String,
    /// Time-to-live ("5m" or "1h")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ttl: Option<String>,
}

impl CacheControl {
    /// Create a cache control with 5-minute TTL.
    pub fn ephemeral_5m() -> Self {
        Self {
            cache_type: "ephemeral".to_string(),
            ttl: Some("5m".to_string()),
        }
    }

    /// Create a cache control with 1-hour TTL.
    pub fn ephemeral_1h() -> Self {
        Self {
            cache_type: "ephemeral".to_string(),
            ttl: Some("1h".to_string()),
        }
    }
}

/// Message in the conversation.
///
/// Contains a role and array of content blocks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// Message role
    pub role: MessageRole,
    /// Content blocks
    pub content: Vec<ContentBlock>,
}

/// Message role.
///
/// Only user and assistant are allowed in messages array.
/// System is a top-level parameter, not a message role.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// User message
    User,
    /// Assistant message
    Assistant,
}

/// Content block in a message.
///
/// Messages contain arrays of typed content blocks.
///
/// # User Message Blocks
///
/// - `text`: Plain text
/// - `image`: Image (base64 or URL)
/// - `document`: Document/PDF
/// - `tool_result`: Result of tool execution
///
/// # Assistant Message Blocks
///
/// - `text`: Plain text response
/// - `tool_use`: Tool invocation request
/// - `thinking`: Reasoning trace (extended thinking)
/// - `redacted_thinking`: Encrypted reasoning
///
/// # Critical Ordering
///
/// In user messages with tool results, `tool_result` blocks MUST come
/// before any `text` blocks, or the API returns a 400 error.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlock {
    /// Plain text content.
    Text {
        /// Text content
        text: String,
        /// Optional cache control
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },

    /// Image content.
    Image {
        /// Image source (base64 or URL)
        source: ImageSource,
        /// Optional cache control
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },

    /// Document content (PDF or plain text).
    Document {
        /// Document source
        source: DocumentSource,
        /// Optional document title
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Optional cache control
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },

    /// Tool use request (assistant only).
    ToolUse {
        /// Unique tool call ID
        id: String,
        /// Tool name
        name: String,
        /// Tool input (JSON object)
        input: JsonValue,
        /// Optional cache control
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },

    /// Tool execution result (user only).
    ///
    /// MUST come before any text blocks in user messages.
    ToolResult {
        /// ID of the tool call this is a result for
        tool_use_id: String,
        /// Result content (string or array of content blocks)
        content: ToolResultContent,
        /// Whether this is an error result
        #[serde(skip_serializing_if = "Option::is_none")]
        is_error: Option<bool>,
        /// Optional cache control
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },

    /// Thinking content (assistant only, from extended thinking).
    Thinking {
        /// Reasoning content (may be summarized in Claude 4)
        thinking: String,
        /// Cryptographic signature for verification
        signature: String,
    },

    /// Redacted thinking (assistant only).
    ///
    /// Thinking content flagged by safety systems is encrypted.
    /// Can be passed back to API for continuity but not human-readable.
    RedactedThinking {
        /// Encrypted thinking data
        data: String,
    },
}

/// Tool result content.
///
/// Tool results can be simple strings or arrays of content blocks (text, images).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToolResultContent {
    /// Simple string result
    String(String),
    /// Array of content blocks (text, image, document)
    Blocks(Vec<ToolResultBlock>),
}

/// Content block allowed in tool results.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ToolResultBlock {
    /// Text content
    Text {
        /// The text content of the tool result
        text: String,
    },
    /// Image content
    Image {
        /// Image source specification (base64 or URL)
        source: ImageSource,
    },
    /// Document content
    Document {
        /// Document source specification
        source: DocumentSource,
    },
}

/// Image source specification.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ImageSource {
    /// Base64-encoded image.
    Base64 {
        /// Media type (image/jpeg, image/png, image/gif, image/webp)
        media_type: String,
        /// Base64-encoded data
        data: String,
    },
    /// Image URL.
    Url {
        /// Image URL
        url: String,
    },
}

/// Document source specification.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DocumentSource {
    /// Base64-encoded PDF.
    Base64 {
        /// Media type (application/pdf)
        media_type: String,
        /// Base64-encoded data
        data: String,
    },
    /// PDF URL.
    Url {
        /// PDF URL
        url: String,
    },
    /// Plain text document.
    Text {
        /// Media type (text/plain)
        media_type: String,
        /// Text content
        data: String,
    },
}

/// Tool definition.
///
/// Defines a tool that Claude can invoke.
///
/// # Tool Types
///
/// - **Client tools**: Custom tools (type omitted or "custom")
/// - **Server tools**: Anthropic-hosted tools (specific type strings)
///
/// Server tools:
/// - `web_search_20250305`: Web search
/// - `web_fetch_20250910`: Web page fetching
/// - `bash_20250124`: Bash command execution
/// - `code_execution_20250825`: Python code execution
/// - `text_editor_20250728`: File editing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tool {
    /// Tool type (optional for client tools, required for server tools)
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub tool_type: Option<String>,

    /// Tool name
    pub name: String,

    /// Tool description (strongly recommended)
    ///
    /// Should be detailed (3-4+ sentences) explaining:
    /// - What the tool does
    /// - When to use it
    /// - What each parameter means
    /// - Important caveats or limitations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Input schema (JSON Schema)
    ///
    /// Defines the shape of the `input` that Claude will generate.
    /// Only for client tools.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_schema: Option<JsonValue>,

    /// Optional cache control
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,

    // Server tool specific fields
    /// Max uses for server tools (web search, web fetch)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_uses: Option<u32>,

    /// Allowed domains for web search/fetch
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_domains: Option<Vec<String>>,

    /// Blocked domains for web search/fetch
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blocked_domains: Option<Vec<String>>,
}

impl Tool {
    /// Create a client tool definition.
    pub fn client(name: String, description: String, input_schema: JsonValue) -> Self {
        Self {
            tool_type: None,
            name,
            description: Some(description),
            input_schema: Some(input_schema),
            cache_control: None,
            max_uses: None,
            allowed_domains: None,
            blocked_domains: None,
        }
    }

    /// Create a web search server tool.
    pub fn web_search(max_uses: Option<u32>) -> Self {
        Self {
            tool_type: Some("web_search_20250305".to_string()),
            name: "web_search".to_string(),
            description: None,
            input_schema: None,
            cache_control: None,
            max_uses,
            allowed_domains: None,
            blocked_domains: None,
        }
    }

    /// Create a web fetch server tool.
    pub fn web_fetch() -> Self {
        Self {
            tool_type: Some("web_fetch_20250910".to_string()),
            name: "web_fetch".to_string(),
            description: None,
            input_schema: None,
            cache_control: None,
            max_uses: None,
            allowed_domains: None,
            blocked_domains: None,
        }
    }
}

/// Tool choice strategy.
///
/// Controls how Claude uses the provided tools.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ToolChoice {
    /// Claude decides whether to use tools (default)
    Auto {
        /// If true, prevents Claude from using multiple tools in parallel
        #[serde(skip_serializing_if = "Option::is_none")]
        disable_parallel_tool_use: Option<bool>,
    },
    /// Claude must use at least one tool
    Any {
        /// If true, prevents Claude from using multiple tools in parallel
        #[serde(skip_serializing_if = "Option::is_none")]
        disable_parallel_tool_use: Option<bool>,
    },
    /// Force Claude to use a specific tool
    Tool {
        /// Tool name to force
        name: String,
        /// If true, prevents Claude from using multiple tools in parallel
        #[serde(skip_serializing_if = "Option::is_none")]
        disable_parallel_tool_use: Option<bool>,
    },
    /// Claude cannot use tools
    None,
}

/// Extended thinking parameter.
///
/// Enables Claude's reasoning process with configurable token budget.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ThinkingParam {
    /// Thinking enabled
    Enabled {
        /// Token budget for reasoning (min: 1024, must be < max_tokens)
        budget_tokens: u32,
    },
    /// Thinking disabled
    Disabled,
}

/// Request metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataParam {
    /// External user identifier (UUID/hash, no PII)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,
}

/// Messages API response.
///
/// Returned from the API for non-streaming requests, or accumulated
/// from streaming events.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageResponse {
    /// Unique message ID
    pub id: String,

    /// Object type (always "message")
    #[serde(rename = "type")]
    pub object_type: String,

    /// Role (always "assistant")
    pub role: String,

    /// Content blocks
    pub content: Vec<ContentBlock>,

    /// Model that generated the response
    pub model: String,

    /// Stop reason
    pub stop_reason: String,

    /// Stop sequence (if stop_reason is "stop_sequence")
    pub stop_sequence: Option<String>,

    /// Token usage
    pub usage: Usage,
}

/// Token usage statistics.
///
/// Fields default to 0 when absent because different events include
/// different subsets of usage data:
/// - `message_start`: full breakdown (input, output, cache)
/// - `message_delta`: only `output_tokens` (cumulative)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Usage {
    /// Input tokens consumed
    #[serde(default)]
    pub input_tokens: u32,

    /// Output tokens generated
    #[serde(default)]
    pub output_tokens: u32,

    /// Tokens written to cache (if caching enabled)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cache_creation_input_tokens: Option<u32>,

    /// Tokens read from cache (if caching enabled)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cache_read_input_tokens: Option<u32>,
}

/// Error response from the API.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorResponse {
    /// Error type (always "error")
    #[serde(rename = "type")]
    pub error_type: String,

    /// Error details
    pub error: ErrorDetail,
}

/// Error detail.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorDetail {
    /// Error type
    #[serde(rename = "type")]
    pub error_type: String,

    /// Human-readable error message
    pub message: String,

    /// Retry-after duration in seconds (for rate limit errors)
    ///
    /// When present, indicates how long to wait before retrying.
    /// This is typically provided by the API in rate limit responses.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retry_after: Option<u32>,
}

impl ErrorDetail {
    /// Check if this error is retryable.
    ///
    /// Returns true for transient errors that may succeed on retry:
    /// - `rate_limit_error` (429)
    /// - `overloaded_error` (529)
    /// - `api_error` (500) - Internal server errors
    ///
    /// All other errors are considered non-retryable.
    ///
    /// Note: HTTP status codes 502, 503, and 504 are also retried even if they
    /// don't have a structured error response. See `AnthropicClient::is_status_code_retryable`.
    pub fn is_retryable(&self) -> bool {
        matches!(
            self.error_type.as_str(),
            "rate_limit_error" | "overloaded_error" | "api_error"
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_message_request_serialization() {
        let req = MessageRequest {
            model: "claude-sonnet-4-5".to_string(),
            max_tokens: 1024,
            messages: vec![Message {
                role: MessageRole::User,
                content: vec![ContentBlock::Text {
                    text: "Hello!".to_string(),
                    cache_control: None,
                }],
            }],
            cache_control: Some(CacheControl::ephemeral_5m()),
            system: Some(SystemPrompt::String("You are helpful.".to_string())),
            tools: None,
            tool_choice: None,
            stream: Some(true),
            temperature: None,
            top_p: None,
            top_k: None,
            stop_sequences: None,
            thinking: None,
            metadata: None,
        };

        let json = serde_json::to_value(&req).unwrap();
        assert_eq!(json["model"], "claude-sonnet-4-5");
        assert_eq!(json["max_tokens"], 1024);
        assert!(json["stream"].as_bool().unwrap());
        assert_eq!(json["cache_control"]["type"], "ephemeral");
        assert_eq!(json["cache_control"]["ttl"], "5m");
    }

    #[test]
    fn test_tool_creation() {
        let tool = Tool::client(
            "get_weather".to_string(),
            "Get weather".to_string(),
            json!({
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            }),
        );

        assert_eq!(tool.name, "get_weather");
        assert!(tool.input_schema.is_some());
    }

    #[test]
    fn test_cache_control() {
        let cc = CacheControl::ephemeral_5m();
        assert_eq!(cc.cache_type, "ephemeral");
        assert_eq!(cc.ttl.as_ref().unwrap(), "5m");

        let cc_1h = CacheControl::ephemeral_1h();
        assert_eq!(cc_1h.ttl.as_ref().unwrap(), "1h");
    }

    #[test]
    fn test_tool_choice_serialization() {
        let auto = ToolChoice::Auto {
            disable_parallel_tool_use: Some(true),
        };
        let json = serde_json::to_value(&auto).unwrap();
        assert_eq!(json["type"], "auto");
        assert!(json["disable_parallel_tool_use"].as_bool().unwrap());

        let tool = ToolChoice::Tool {
            name: "get_weather".to_string(),
            disable_parallel_tool_use: None,
        };
        let json = serde_json::to_value(&tool).unwrap();
        assert_eq!(json["type"], "tool");
        assert_eq!(json["name"], "get_weather");
    }

    #[test]
    fn test_error_detail_is_retryable() {
        // Retryable errors
        let rate_limit_error = ErrorDetail {
            error_type: "rate_limit_error".to_string(),
            message: "Rate limit exceeded".to_string(),
            retry_after: Some(60),
        };
        assert!(rate_limit_error.is_retryable());

        let overloaded_error = ErrorDetail {
            error_type: "overloaded_error".to_string(),
            message: "Service overloaded".to_string(),
            retry_after: None,
        };
        assert!(overloaded_error.is_retryable());

        let api_error = ErrorDetail {
            error_type: "api_error".to_string(),
            message: "Internal server error".to_string(),
            retry_after: None,
        };
        assert!(api_error.is_retryable());

        // Non-retryable errors
        let invalid_request = ErrorDetail {
            error_type: "invalid_request_error".to_string(),
            message: "Invalid request".to_string(),
            retry_after: None,
        };
        assert!(!invalid_request.is_retryable());

        let auth_error = ErrorDetail {
            error_type: "authentication_error".to_string(),
            message: "Invalid API key".to_string(),
            retry_after: None,
        };
        assert!(!auth_error.is_retryable());

        let not_found = ErrorDetail {
            error_type: "not_found_error".to_string(),
            message: "Not found".to_string(),
            retry_after: None,
        };
        assert!(!not_found.is_retryable());
    }

    #[test]
    fn test_error_response_serialization() {
        let error_json = json!({
            "type": "error",
            "error": {
                "type": "rate_limit_error",
                "message": "Rate limit exceeded",
                "retry_after": 30
            }
        });

        let error_response: ErrorResponse = serde_json::from_value(error_json).unwrap();
        assert_eq!(error_response.error.error_type, "rate_limit_error");
        assert_eq!(error_response.error.message, "Rate limit exceeded");
        assert_eq!(error_response.error.retry_after, Some(30));
        assert!(error_response.error.is_retryable());
    }

    #[test]
    fn test_usage_with_cache_tokens() {
        // Test usage parsing with all optional cache fields populated
        let usage_json = json!({
            "input_tokens": 1000,
            "output_tokens": 500,
            "cache_creation_input_tokens": 200,
            "cache_read_input_tokens": 800
        });

        let usage: Usage = serde_json::from_value(usage_json).unwrap();
        assert_eq!(usage.input_tokens, 1000);
        assert_eq!(usage.output_tokens, 500);
        assert_eq!(usage.cache_creation_input_tokens, Some(200));
        assert_eq!(usage.cache_read_input_tokens, Some(800));
    }

    #[test]
    fn test_usage_without_cache_tokens() {
        // Test usage parsing when cache fields are absent
        let usage_json = json!({
            "input_tokens": 1000,
            "output_tokens": 500
        });

        let usage: Usage = serde_json::from_value(usage_json).unwrap();
        assert_eq!(usage.input_tokens, 1000);
        assert_eq!(usage.output_tokens, 500);
        assert_eq!(usage.cache_creation_input_tokens, None);
        assert_eq!(usage.cache_read_input_tokens, None);
    }

    #[test]
    fn test_unified_usage_conversion_from_anthropic() {
        // Test conversion from Anthropic Usage to UnifiedUsage
        let usage = Usage {
            input_tokens: 1000,
            output_tokens: 500,
            cache_creation_input_tokens: Some(200),
            cache_read_input_tokens: Some(800),
        };

        // Simulate the conversion logic in client.rs (MessageDelta handler)
        let unified = crate::llm::unified::UnifiedUsage {
            input_tokens: usage.input_tokens,
            output_tokens: usage.output_tokens,
            cache_creation_input_tokens: usage.cache_creation_input_tokens,
            cache_read_input_tokens: usage.cache_read_input_tokens,
            reasoning_tokens: None, // Anthropic doesn't separate reasoning tokens
        };

        assert_eq!(unified.input_tokens, 1000);
        assert_eq!(unified.output_tokens, 500);
        assert_eq!(unified.cache_creation_input_tokens, Some(200));
        assert_eq!(unified.cache_read_input_tokens, Some(800));
        assert_eq!(unified.reasoning_tokens, None);
    }
}