Skip to main content

agentik_sdk/types/
shared.rs

1use serde::{Deserialize, Serialize};
2
3/// Request ID for tracking API requests
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5pub struct RequestId(pub String);
6
7impl RequestId {
8    pub fn new(id: impl Into<String>) -> Self {
9        Self(id.into())
10    }
11    
12    pub fn as_str(&self) -> &str {
13        &self.0
14    }
15}
16
17impl std::fmt::Display for RequestId {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(f, "{}", self.0)
20    }
21}
22
23/// Usage information for API requests
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
25pub struct Usage {
26    /// The number of input tokens which were used
27    pub input_tokens: u32,
28    
29    /// The number of output tokens which were used
30    pub output_tokens: u32,
31    
32    /// The number of input tokens used to create the cache entry
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub cache_creation_input_tokens: Option<u32>,
35    
36    /// The number of input tokens read from the cache
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub cache_read_input_tokens: Option<u32>,
39    
40    /// Server tool usage statistics
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub server_tool_use: Option<ServerToolUsage>,
43    
44    /// Service tier used for the request
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub service_tier: Option<String>,
47}
48
49/// Server tool usage statistics
50#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
51pub struct ServerToolUsage {
52    /// Number of web search tool requests made
53    pub web_search_requests: u32,
54}
55
56impl Usage {
57    /// Get the total number of tokens used
58    pub fn total_tokens(&self) -> u32 {
59        self.input_tokens + self.output_tokens
60    }
61    
62    /// Get the total input tokens including cache tokens
63    pub fn total_input_tokens(&self) -> u32 {
64        self.input_tokens 
65            + self.cache_creation_input_tokens.unwrap_or(0)
66            + self.cache_read_input_tokens.unwrap_or(0)
67    }
68}
69
70/// Base trait for responses that include request IDs
71pub trait HasRequestId {
72    fn request_id(&self) -> Option<&RequestId>;
73}