xai-openapi 0.1.1

Rust types for the xAI API (Grok models)
Documentation
//! Token usage tracking types for xAI API responses.

use serde::{Deserialize, Serialize};

#[allow(unused_imports)]
use crate::prelude::*;

/// Token usage information for chat completions.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Usage {
    /// Total prompt token used.
    pub prompt_tokens: i32,

    /// Total completion token used.
    pub completion_tokens: i32,

    /// Total token used, the sum of prompt token and completion token amount.
    pub total_tokens: i32,

    /// Breakdown of prompt token usage of different types.
    pub prompt_tokens_details: PromptUsageDetail,

    /// Breakdown of completion token usage of different types.
    pub completion_tokens_details: CompletionUsageDetail,

    /// Number of individual live search source used.
    pub num_sources_used: i32,
}

/// Details of prompt usage.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct PromptUsageDetail {
    /// Text prompt token used.
    pub text_tokens: i32,

    /// Audio prompt token used.
    pub audio_tokens: i32,

    /// Image prompt token used.
    pub image_tokens: i32,

    /// Token cached by xAI from previous requests and reused for this request.
    pub cached_tokens: i32,
}

/// Details of completion usage.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CompletionUsageDetail {
    /// Tokens generated by the model for reasoning.
    pub reasoning_tokens: i32,

    /// Audio input tokens generated by the model.
    pub audio_tokens: i32,

    /// The number of tokens in the prediction that appeared in the completion.
    pub accepted_prediction_tokens: i32,

    /// The number of tokens in the prediction that did not appear in the completion.
    pub rejected_prediction_tokens: i32,
}

/// Token usage information for the Responses API.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ModelUsage {
    /// Number of input tokens used.
    pub input_tokens: i32,

    /// Breakdown of the input tokens.
    pub input_tokens_details: InputTokensDetails,

    /// Number of output tokens used.
    pub output_tokens: i32,

    /// Breakdown of the output tokens.
    pub output_tokens_details: OutputTokensDetails,

    /// Total tokens used.
    pub total_tokens: i32,

    /// Number of sources used (for live search).
    pub num_sources_used: i32,

    /// Number of server side tools used.
    pub num_server_side_tools_used: i32,

    /// Cost in nano US dollars for this request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cost_in_nano_usd: Option<i64>,

    /// Details about the server side tool usage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub server_side_tool_usage_details: Option<ServerSideToolUsageDetails>,
}

/// Input tokens details.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct InputTokensDetails {
    /// Token cached by xAI from previous requests and reused for this request.
    pub cached_tokens: i32,
}

/// Output tokens details.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct OutputTokensDetails {
    /// Tokens generated by the model for reasoning.
    pub reasoning_tokens: i32,
}

/// Details about the server side tool usage.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ServerSideToolUsageDetails {
    /// Number of web search calls.
    pub web_search_calls: i32,

    /// Number of X search calls.
    pub x_search_calls: i32,

    /// Number of code interpreter calls.
    pub code_interpreter_calls: i32,

    /// Number of file search calls.
    pub file_search_calls: i32,

    /// Number of MCP calls.
    pub mcp_calls: i32,

    /// Number of document search calls.
    pub document_search_calls: i32,
}

/// Token usage information for embeddings.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct EmbeddingUsage {
    /// Prompt token used.
    pub prompt_tokens: i32,

    /// Total token used.
    pub total_tokens: i32,
}

/// Token usage information for the Messages API (Anthropic-compatible).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct MessageUsage {
    /// Number of input tokens used.
    pub input_tokens: i32,

    /// (Unsupported) Number of tokens written to the cache when creating a new entry.
    pub cache_creation_input_tokens: i32,

    /// Number of tokens retrieved from the cache for this request.
    pub cache_read_input_tokens: i32,

    /// Number of output tokens used.
    pub output_tokens: i32,
}