pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
pub cache_read_tokens: u32,
pub cache_write_tokens: u32,
pub reasoning_tokens: Option<u32>,
pub total_tokens: u32,
pub cost: Option<Cost>,
}Expand description
LLM 请求的 token 用量统计。
字段语义遵循“包含式总量 + 非重叠分解“惯例 (与 OpenAI / Anthropic / AI SDK 对齐):
input_tokens— 总输入 token,包含缓存读/写output_tokens— 总输出 token,包含推理 tokentotal_tokens— provider 报告的总量,或input + output
非重叠分解:
cache_read_tokens+cache_write_tokens+ 非缓存部分 =input_tokensreasoning_tokens≤output_tokens
部分 provider 不报告细分字段,此时为 0 或由上层推断。
Fields§
§input_tokens: u32总输入 token(含缓存读/写)
output_tokens: u32总输出 token(含推理)
cache_read_tokens: u32从缓存读取的输入 token
cache_write_tokens: u32写入缓存的输入 token
reasoning_tokens: Option<u32>推理/思考 token(output 的子集),None 表示 provider 未报告
total_tokens: u32总 token(provider 报告值或 input + output)
cost: Option<Cost>费用明细,仅在上层持有定价表时可计算
Implementations§
Source§impl Usage
impl Usage
Sourcepub fn visible_output_tokens(&self) -> u32
pub fn visible_output_tokens(&self) -> u32
可见输出 token = output - reasoning,至少为 0。
Sourcepub fn non_cached_input_tokens(&self) -> u32
pub fn non_cached_input_tokens(&self) -> u32
非缓存输入 token = input - cache_read - cache_write,至少为 0。
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Usage
impl<'de> Deserialize<'de> for Usage
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Usage
Auto Trait Implementations§
impl Freeze for Usage
impl RefUnwindSafe for Usage
impl Send for Usage
impl Sync for Usage
impl Unpin for Usage
impl UnsafeUnpin for Usage
impl UnwindSafe for Usage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more