xai_openapi/
usage.rs

1//! Token usage tracking types for xAI API responses.
2
3use serde::{Deserialize, Serialize};
4
5#[allow(unused_imports)]
6use crate::prelude::*;
7
8/// Token usage information for chat completions.
9#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
10pub struct Usage {
11    /// Total prompt token used.
12    pub prompt_tokens: i32,
13
14    /// Total completion token used.
15    pub completion_tokens: i32,
16
17    /// Total token used, the sum of prompt token and completion token amount.
18    pub total_tokens: i32,
19
20    /// Breakdown of prompt token usage of different types.
21    pub prompt_tokens_details: PromptUsageDetail,
22
23    /// Breakdown of completion token usage of different types.
24    pub completion_tokens_details: CompletionUsageDetail,
25
26    /// Number of individual live search source used.
27    pub num_sources_used: i32,
28}
29
30/// Details of prompt usage.
31#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
32pub struct PromptUsageDetail {
33    /// Text prompt token used.
34    pub text_tokens: i32,
35
36    /// Audio prompt token used.
37    pub audio_tokens: i32,
38
39    /// Image prompt token used.
40    pub image_tokens: i32,
41
42    /// Token cached by xAI from previous requests and reused for this request.
43    pub cached_tokens: i32,
44}
45
46/// Details of completion usage.
47#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
48pub struct CompletionUsageDetail {
49    /// Tokens generated by the model for reasoning.
50    pub reasoning_tokens: i32,
51
52    /// Audio input tokens generated by the model.
53    pub audio_tokens: i32,
54
55    /// The number of tokens in the prediction that appeared in the completion.
56    pub accepted_prediction_tokens: i32,
57
58    /// The number of tokens in the prediction that did not appear in the completion.
59    pub rejected_prediction_tokens: i32,
60}
61
62/// Token usage information for the Responses API.
63#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
64pub struct ModelUsage {
65    /// Number of input tokens used.
66    pub input_tokens: i32,
67
68    /// Breakdown of the input tokens.
69    pub input_tokens_details: InputTokensDetails,
70
71    /// Number of output tokens used.
72    pub output_tokens: i32,
73
74    /// Breakdown of the output tokens.
75    pub output_tokens_details: OutputTokensDetails,
76
77    /// Total tokens used.
78    pub total_tokens: i32,
79
80    /// Number of sources used (for live search).
81    pub num_sources_used: i32,
82
83    /// Number of server side tools used.
84    pub num_server_side_tools_used: i32,
85
86    /// Cost in nano US dollars for this request.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub cost_in_nano_usd: Option<i64>,
89
90    /// Details about the server side tool usage.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub server_side_tool_usage_details: Option<ServerSideToolUsageDetails>,
93}
94
95/// Input tokens details.
96#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
97pub struct InputTokensDetails {
98    /// Token cached by xAI from previous requests and reused for this request.
99    pub cached_tokens: i32,
100}
101
102/// Output tokens details.
103#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
104pub struct OutputTokensDetails {
105    /// Tokens generated by the model for reasoning.
106    pub reasoning_tokens: i32,
107}
108
109/// Details about the server side tool usage.
110#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
111pub struct ServerSideToolUsageDetails {
112    /// Number of web search calls.
113    pub web_search_calls: i32,
114
115    /// Number of X search calls.
116    pub x_search_calls: i32,
117
118    /// Number of code interpreter calls.
119    pub code_interpreter_calls: i32,
120
121    /// Number of file search calls.
122    pub file_search_calls: i32,
123
124    /// Number of MCP calls.
125    pub mcp_calls: i32,
126
127    /// Number of document search calls.
128    pub document_search_calls: i32,
129}
130
131/// Token usage information for embeddings.
132#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
133pub struct EmbeddingUsage {
134    /// Prompt token used.
135    pub prompt_tokens: i32,
136
137    /// Total token used.
138    pub total_tokens: i32,
139}
140
141/// Token usage information for the Messages API (Anthropic-compatible).
142#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
143pub struct MessageUsage {
144    /// Number of input tokens used.
145    pub input_tokens: i32,
146
147    /// (Unsupported) Number of tokens written to the cache when creating a new entry.
148    pub cache_creation_input_tokens: i32,
149
150    /// Number of tokens retrieved from the cache for this request.
151    pub cache_read_input_tokens: i32,
152
153    /// Number of output tokens used.
154    pub output_tokens: i32,
155}