Skip to main content

lean_ctx_protocol/
usage.rs

1use serde::{Deserialize, Serialize};
2
3/// Granular token usage breakdown.
4#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
5pub struct UsageBreakdownV1 {
6    pub input_tokens: u64,
7    pub output_tokens: u64,
8    pub cached_input_tokens: u64,
9    pub cache_write_tokens: u64,
10    pub reasoning_tokens: u64,
11    #[serde(default, skip_serializing_if = "Vec::is_empty")]
12    pub other_units: Vec<MeasuredUnitV1>,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub struct MeasuredUnitV1 {
17    pub unit_type: String,
18    pub quantity: u64,
19}
20
21#[cfg(test)]
22mod tests {
23    use crate::{MeasuredUnitV1, UsageBreakdownV1};
24
25    #[test]
26    fn serialization_round_trip() {
27        let usage = UsageBreakdownV1 {
28            input_tokens: 10,
29            output_tokens: 5,
30            cached_input_tokens: 3,
31            cache_write_tokens: 2,
32            reasoning_tokens: 1,
33            other_units: vec![MeasuredUnitV1 {
34                unit_type: "image".to_owned(),
35                quantity: 1,
36            }],
37        };
38        let json = serde_json::to_string(&usage).expect("usage should serialize");
39        let decoded = serde_json::from_str(&json).expect("usage should deserialize");
40        assert_eq!(usage, decoded);
41    }
42}