claude-agent 0.2.25

Rust SDK for building AI agents with Anthropic's Claude - Direct API, no CLI dependency
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
//! API response types.

use std::collections::HashMap;

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use super::ContentBlock;
use super::citations::{Citation, SearchResultLocationCitation};

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct TokenUsage {
    pub input_tokens: u64,
    pub output_tokens: u64,
    #[serde(default)]
    pub cache_read_input_tokens: u64,
    #[serde(default)]
    pub cache_creation_input_tokens: u64,
}

impl TokenUsage {
    pub fn total(&self) -> u64 {
        self.input_tokens + self.output_tokens
    }

    pub fn context_usage(&self) -> u64 {
        self.input_tokens + self.cache_read_input_tokens + self.cache_creation_input_tokens
    }

    pub fn add(&mut self, other: &TokenUsage) {
        self.input_tokens += other.input_tokens;
        self.output_tokens += other.output_tokens;
        self.cache_read_input_tokens += other.cache_read_input_tokens;
        self.cache_creation_input_tokens += other.cache_creation_input_tokens;
    }

    pub fn add_usage(&mut self, usage: &Usage) {
        self.input_tokens += usage.input_tokens as u64;
        self.output_tokens += usage.output_tokens as u64;
        self.cache_read_input_tokens += usage.cache_read_input_tokens.unwrap_or(0) as u64;
        self.cache_creation_input_tokens += usage.cache_creation_input_tokens.unwrap_or(0) as u64;
    }

    pub fn cache_hit_rate(&self) -> f64 {
        if self.input_tokens == 0 {
            return 0.0;
        }
        (self.cache_read_input_tokens as f64 / self.input_tokens as f64).clamp(0.0, 1.0)
    }
}

impl From<&Usage> for TokenUsage {
    fn from(usage: &Usage) -> Self {
        Self {
            input_tokens: usage.input_tokens as u64,
            output_tokens: usage.output_tokens as u64,
            cache_read_input_tokens: usage.cache_read_input_tokens.unwrap_or(0) as u64,
            cache_creation_input_tokens: usage.cache_creation_input_tokens.unwrap_or(0) as u64,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiResponse {
    pub id: String,
    #[serde(rename = "type")]
    pub response_type: String,
    pub role: String,
    pub content: Vec<ContentBlock>,
    pub model: String,
    pub stop_reason: Option<StopReason>,
    pub stop_sequence: Option<String>,
    pub usage: Usage,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub context_management: Option<ContextManagementResponse>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContextManagementResponse {
    #[serde(default)]
    pub applied_edits: Vec<AppliedEdit>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppliedEdit {
    #[serde(rename = "type")]
    pub edit_type: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cleared_tool_uses: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cleared_thinking_turns: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cleared_input_tokens: Option<u64>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopReason {
    EndTurn,
    MaxTokens,
    StopSequence,
    ToolUse,
    /// Model refused to generate output due to safety reasons.
    /// When using structured outputs, the response may not match the schema.
    Refusal,
}

/// Server-side tool usage from API response.
///
/// This is returned in the `usage.server_tool_use` field when server-side
/// tools (web search, web fetch) are used by the API.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct ServerToolUseUsage {
    /// Number of server-side web search requests.
    #[serde(default)]
    pub web_search_requests: u32,
    /// Number of server-side web fetch requests.
    #[serde(default)]
    pub web_fetch_requests: u32,
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct Usage {
    pub input_tokens: u32,
    pub output_tokens: u32,
    #[serde(default)]
    pub cache_read_input_tokens: Option<u32>,
    #[serde(default)]
    pub cache_creation_input_tokens: Option<u32>,
    /// Server-side tool usage (web search, web fetch).
    #[serde(default)]
    pub server_tool_use: Option<ServerToolUseUsage>,
}

impl Usage {
    pub fn total(&self) -> u32 {
        self.input_tokens + self.output_tokens
    }

    pub fn context_usage(&self) -> u32 {
        self.input_tokens
            + self.cache_read_input_tokens.unwrap_or(0)
            + self.cache_creation_input_tokens.unwrap_or(0)
    }

    pub fn estimated_cost(&self, model: &str) -> Decimal {
        crate::budget::pricing::global_pricing_table().calculate(model, self)
    }

    /// Get server-side web search request count.
    pub fn server_web_search_requests(&self) -> u32 {
        self.server_tool_use
            .as_ref()
            .map(|s| s.web_search_requests)
            .unwrap_or(0)
    }

    /// Get server-side web fetch request count.
    pub fn server_web_fetch_requests(&self) -> u32 {
        self.server_tool_use
            .as_ref()
            .map(|s| s.web_fetch_requests)
            .unwrap_or(0)
    }

    /// Check if any server-side tools were used.
    pub fn has_server_tool_use(&self) -> bool {
        self.server_tool_use
            .as_ref()
            .map(|s| s.web_search_requests > 0 || s.web_fetch_requests > 0)
            .unwrap_or(false)
    }
}

impl ApiResponse {
    pub fn text(&self) -> String {
        self.content
            .iter()
            .filter_map(|block| block.as_text())
            .collect::<Vec<_>>()
            .join("")
    }

    pub fn wants_tool_use(&self) -> bool {
        self.stop_reason == Some(StopReason::ToolUse)
    }

    pub fn tool_uses(&self) -> Vec<&super::ToolUseBlock> {
        self.content
            .iter()
            .filter_map(|block| match block {
                ContentBlock::ToolUse(tool_use) => Some(tool_use),
                _ => None,
            })
            .collect()
    }

    pub fn thinking_blocks(&self) -> Vec<&super::ThinkingBlock> {
        self.content
            .iter()
            .filter_map(|block| block.as_thinking())
            .collect()
    }

    pub fn has_thinking(&self) -> bool {
        self.content.iter().any(|block| block.is_thinking())
    }

    pub fn all_citations(&self) -> Vec<&Citation> {
        self.content
            .iter()
            .filter_map(|block| block.citations())
            .flatten()
            .collect()
    }

    pub fn has_citations(&self) -> bool {
        self.content.iter().any(|block| block.has_citations())
    }

    pub fn citations_by_document(&self) -> HashMap<usize, Vec<&Citation>> {
        let mut map: HashMap<usize, Vec<&Citation>> = HashMap::new();
        for citation in self.all_citations() {
            if let Some(doc_idx) = citation.document_index() {
                map.entry(doc_idx).or_default().push(citation);
            }
        }
        map
    }

    pub fn search_citations(&self) -> Vec<&SearchResultLocationCitation> {
        self.all_citations()
            .into_iter()
            .filter_map(|c| match c {
                Citation::SearchResultLocation(src) => Some(src),
                _ => None,
            })
            .collect()
    }

    pub fn applied_edits(&self) -> &[AppliedEdit] {
        self.context_management
            .as_ref()
            .map(|cm| cm.applied_edits.as_slice())
            .unwrap_or_default()
    }

    pub fn cleared_tokens(&self) -> u64 {
        self.applied_edits()
            .iter()
            .filter_map(|e| e.cleared_input_tokens)
            .sum()
    }

    /// Get server-side tool uses from the response content.
    pub fn server_tool_uses(&self) -> Vec<&super::ServerToolUseBlock> {
        self.content
            .iter()
            .filter_map(|block| block.as_server_tool_use())
            .collect()
    }

    /// Check if the response contains any server-side tool use.
    pub fn has_server_tool_use(&self) -> bool {
        self.content.iter().any(|block| block.is_server_tool_use())
    }

    /// Get server-side web search request count from usage.
    pub fn server_web_search_requests(&self) -> u32 {
        self.usage.server_web_search_requests()
    }

    /// Get server-side web fetch request count from usage.
    pub fn server_web_fetch_requests(&self) -> u32 {
        self.usage.server_web_fetch_requests()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamEvent {
    MessageStart {
        message: MessageStartData,
    },
    ContentBlockStart {
        index: usize,
        content_block: ContentBlock,
    },
    ContentBlockDelta {
        index: usize,
        delta: ContentDelta,
    },
    ContentBlockStop {
        index: usize,
    },
    MessageDelta {
        delta: MessageDeltaData,
        usage: Usage,
    },
    MessageStop,
    Ping,
    Error {
        error: StreamError,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageStartData {
    pub id: String,
    #[serde(rename = "type")]
    pub message_type: String,
    pub role: String,
    pub model: String,
    pub usage: Usage,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentDelta {
    TextDelta { text: String },
    InputJsonDelta { partial_json: String },
    ThinkingDelta { thinking: String },
    SignatureDelta { signature: String },
    CitationsDelta { citation: Citation },
}

impl ContentDelta {
    pub fn is_citation(&self) -> bool {
        matches!(self, Self::CitationsDelta { .. })
    }

    pub fn as_citation(&self) -> Option<&Citation> {
        match self {
            Self::CitationsDelta { citation } => Some(citation),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageDeltaData {
    pub stop_reason: Option<StopReason>,
    pub stop_sequence: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamError {
    #[serde(rename = "type")]
    pub error_type: String,
    pub message: String,
}

#[derive(Debug, Clone)]
pub enum CompactResult {
    NotNeeded,
    Compacted {
        original_count: usize,
        new_count: usize,
        saved_tokens: usize,
        summary: String,
    },
    Skipped {
        reason: String,
    },
}

/// Per-model usage statistics tracking.
///
/// This tracks API usage for each model separately, enabling accurate
/// cost attribution when multiple models are used (e.g., Haiku for
/// tool summarization, Opus for main agent).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ModelUsage {
    /// Input tokens consumed by this model.
    pub input_tokens: u32,
    /// Output tokens generated by this model.
    pub output_tokens: u32,
    /// Cache read tokens.
    #[serde(default)]
    pub cache_read_input_tokens: u32,
    /// Cache creation tokens.
    #[serde(default)]
    pub cache_creation_input_tokens: u32,
    /// Number of local web search requests made by this model.
    #[serde(default)]
    pub web_search_requests: u32,
    /// Number of local web fetch requests made by this model.
    #[serde(default)]
    pub web_fetch_requests: u32,
    /// Estimated cost in USD for this model's usage.
    #[serde(default)]
    pub cost_usd: Decimal,
    /// Context window size for this model.
    #[serde(default)]
    pub context_window: u64,
}

impl ModelUsage {
    pub fn from_usage(usage: &Usage, model: &str) -> Self {
        let cost = usage.estimated_cost(model);
        let context_window = crate::models::context_window::for_model(model);
        let (web_search, web_fetch) = usage
            .server_tool_use
            .as_ref()
            .map(|s| (s.web_search_requests, s.web_fetch_requests))
            .unwrap_or((0, 0));
        Self {
            input_tokens: usage.input_tokens,
            output_tokens: usage.output_tokens,
            cache_read_input_tokens: usage.cache_read_input_tokens.unwrap_or(0),
            cache_creation_input_tokens: usage.cache_creation_input_tokens.unwrap_or(0),
            web_search_requests: web_search,
            web_fetch_requests: web_fetch,
            cost_usd: cost,
            context_window,
        }
    }

    pub fn add(&mut self, other: &ModelUsage) {
        self.input_tokens = self.input_tokens.saturating_add(other.input_tokens);
        self.output_tokens = self.output_tokens.saturating_add(other.output_tokens);
        self.cache_read_input_tokens = self
            .cache_read_input_tokens
            .saturating_add(other.cache_read_input_tokens);
        self.cache_creation_input_tokens = self
            .cache_creation_input_tokens
            .saturating_add(other.cache_creation_input_tokens);
        self.web_search_requests = self
            .web_search_requests
            .saturating_add(other.web_search_requests);
        self.web_fetch_requests = self
            .web_fetch_requests
            .saturating_add(other.web_fetch_requests);
        self.cost_usd += other.cost_usd;
    }

    pub fn add_usage(&mut self, usage: &Usage, model: &str) {
        self.input_tokens = self.input_tokens.saturating_add(usage.input_tokens);
        self.output_tokens = self.output_tokens.saturating_add(usage.output_tokens);
        self.cache_read_input_tokens = self
            .cache_read_input_tokens
            .saturating_add(usage.cache_read_input_tokens.unwrap_or(0));
        self.cache_creation_input_tokens = self
            .cache_creation_input_tokens
            .saturating_add(usage.cache_creation_input_tokens.unwrap_or(0));
        self.cost_usd += usage.estimated_cost(model);
        if let Some(ref server_usage) = usage.server_tool_use {
            self.web_search_requests = self
                .web_search_requests
                .saturating_add(server_usage.web_search_requests);
            self.web_fetch_requests = self
                .web_fetch_requests
                .saturating_add(server_usage.web_fetch_requests);
        }
    }

    pub fn total_tokens(&self) -> u32 {
        self.input_tokens + self.output_tokens
    }
}

/// Server-side tool usage statistics.
///
/// Tracks usage of server-side tools that are executed by the Anthropic API
/// rather than locally. This is parsed from the API response's `server_tool_use`
/// field when available.
///
/// **Important distinction:**
/// - `server_tool_use`: Tools executed by Anthropic's servers (e.g., server-side RAG)
/// - `tool_stats["WebSearch"]`: Local WebSearch tool calls (via DuckDuckGo etc.)
/// - `modelUsage.*.webSearchRequests`: Per-model local web search counts
///
/// Currently, Anthropic's API may return this field for certain features like
/// built-in web search or retrieval. If the API doesn't return this field,
/// values remain at 0.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ServerToolUse {
    /// Number of server-side web search requests (from API response).
    pub web_search_requests: u32,
    /// Number of server-side web fetch requests (from API response).
    pub web_fetch_requests: u32,
}

impl ServerToolUse {
    /// Record a web search request.
    pub fn record_web_search(&mut self) {
        self.web_search_requests += 1;
    }

    /// Record a web fetch request.
    pub fn record_web_fetch(&mut self) {
        self.web_fetch_requests += 1;
    }

    /// Check if any server tools were used.
    pub fn has_usage(&self) -> bool {
        self.web_search_requests > 0 || self.web_fetch_requests > 0
    }

    /// Add counts from API response's server_tool_use usage.
    pub fn add_from_usage(&mut self, usage: &ServerToolUseUsage) {
        self.web_search_requests += usage.web_search_requests;
        self.web_fetch_requests += usage.web_fetch_requests;
    }
}

/// Record of a permission denial for a tool request.
///
/// Tracks when and why a tool execution was blocked, useful for
/// debugging and audit logging.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionDenial {
    /// Name of the tool that was denied.
    pub tool_name: String,
    /// The tool use ID from the API request.
    pub tool_use_id: String,
    /// The input that was provided to the tool.
    pub tool_input: serde_json::Value,
    /// Reason for the denial.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    /// Timestamp when the denial occurred.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

impl PermissionDenial {
    /// Create a new permission denial record.
    pub fn new(
        tool_name: impl Into<String>,
        tool_use_id: impl Into<String>,
        tool_input: serde_json::Value,
    ) -> Self {
        Self {
            tool_name: tool_name.into(),
            tool_use_id: tool_use_id.into(),
            tool_input,
            reason: None,
            timestamp: Some(chrono::Utc::now()),
        }
    }

    /// Add a reason for the denial.
    pub fn reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }
}

#[cfg(test)]
mod tests {
    use rust_decimal_macros::dec;

    use super::*;

    #[test]
    fn test_usage_total() {
        let usage = Usage {
            input_tokens: 100,
            output_tokens: 50,
            ..Default::default()
        };
        assert_eq!(usage.total(), 150);
    }

    #[test]
    fn test_usage_cost() {
        let usage = Usage {
            input_tokens: 1_000_000,
            output_tokens: 1_000_000,
            ..Default::default()
        };
        // Sonnet long context (1M > 200K): $3 * 2 input + $15 output = $21
        let cost = usage.estimated_cost("claude-sonnet-4-5");
        assert_eq!(cost, dec!(21));
    }

    #[test]
    fn test_model_usage_from_usage() {
        let usage = Usage {
            input_tokens: 1000,
            output_tokens: 500,
            cache_read_input_tokens: Some(100),
            cache_creation_input_tokens: Some(50),
            ..Default::default()
        };
        let model_usage = ModelUsage::from_usage(&usage, "claude-sonnet-4-5");
        assert_eq!(model_usage.input_tokens, 1000);
        assert_eq!(model_usage.output_tokens, 500);
        assert_eq!(model_usage.cache_read_input_tokens, 100);
        assert!(model_usage.cost_usd > Decimal::ZERO);
    }

    #[test]
    fn test_model_usage_add() {
        let mut usage1 = ModelUsage {
            input_tokens: 100,
            output_tokens: 50,
            cost_usd: dec!(0.01),
            ..Default::default()
        };
        let usage2 = ModelUsage {
            input_tokens: 200,
            output_tokens: 100,
            cost_usd: dec!(0.02),
            ..Default::default()
        };
        usage1.add(&usage2);
        assert_eq!(usage1.input_tokens, 300);
        assert_eq!(usage1.output_tokens, 150);
        assert_eq!(usage1.cost_usd, dec!(0.03));
    }

    #[test]
    fn test_server_tool_use() {
        let mut stu = ServerToolUse::default();
        assert!(!stu.has_usage());

        stu.record_web_search();
        assert!(stu.has_usage());
        assert_eq!(stu.web_search_requests, 1);

        stu.record_web_fetch();
        assert_eq!(stu.web_fetch_requests, 1);
    }

    #[test]
    fn test_permission_denial() {
        let denial = PermissionDenial::new(
            "WebSearch",
            "tool_123",
            serde_json::json!({"query": "test"}),
        )
        .reason("User denied");

        assert_eq!(denial.tool_name, "WebSearch");
        assert_eq!(denial.reason, Some("User denied".to_string()));
        assert!(denial.timestamp.is_some());
    }

    #[test]
    fn test_server_tool_use_usage_parsing() {
        let json = r#"{
            "input_tokens": 1000,
            "output_tokens": 500,
            "server_tool_use": {
                "web_search_requests": 3,
                "web_fetch_requests": 2
            }
        }"#;
        let usage: Usage = serde_json::from_str(json).unwrap();
        assert_eq!(usage.input_tokens, 1000);
        assert_eq!(usage.output_tokens, 500);
        assert!(usage.has_server_tool_use());
        assert_eq!(usage.server_web_search_requests(), 3);
        assert_eq!(usage.server_web_fetch_requests(), 2);
    }

    #[test]
    fn test_server_tool_use_usage_empty() {
        let json = r#"{
            "input_tokens": 100,
            "output_tokens": 50
        }"#;
        let usage: Usage = serde_json::from_str(json).unwrap();
        assert!(!usage.has_server_tool_use());
        assert_eq!(usage.server_web_search_requests(), 0);
        assert_eq!(usage.server_web_fetch_requests(), 0);
    }

    #[test]
    fn test_server_tool_use_add_from_usage() {
        let mut stu = ServerToolUse::default();
        let usage = ServerToolUseUsage {
            web_search_requests: 2,
            web_fetch_requests: 1,
        };
        stu.add_from_usage(&usage);
        assert_eq!(stu.web_search_requests, 2);
        assert_eq!(stu.web_fetch_requests, 1);

        // Add more
        stu.add_from_usage(&usage);
        assert_eq!(stu.web_search_requests, 4);
        assert_eq!(stu.web_fetch_requests, 2);
    }
}