1use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
8#[serde(deny_unknown_fields)]
9pub struct ModelUsage {
10 pub input_tokens: u64,
11 pub cached_input_tokens: Option<u64>,
12 pub output_tokens: u64,
13 pub reasoning_output_tokens: Option<u64>,
14 pub total_tokens: u64,
15}
16
17impl ModelUsage {
18 #[must_use]
20 pub fn new(input_tokens: u64, output_tokens: u64) -> Self {
21 let total_tokens = input_tokens
22 .checked_add(output_tokens)
23 .expect("model usage total token count overflowed");
24 Self::with_details(input_tokens, None, output_tokens, None, total_tokens)
25 }
26
27 #[must_use]
29 pub const fn with_details(
30 input_tokens: u64,
31 cached_input_tokens: Option<u64>,
32 output_tokens: u64,
33 reasoning_output_tokens: Option<u64>,
34 total_tokens: u64,
35 ) -> Self {
36 Self {
37 input_tokens,
38 cached_input_tokens,
39 output_tokens,
40 reasoning_output_tokens,
41 total_tokens,
42 }
43 }
44
45 #[must_use]
47 pub const fn input_tokens(&self) -> u64 {
48 self.input_tokens
49 }
50
51 #[must_use]
53 pub const fn cached_input_tokens(&self) -> Option<u64> {
54 self.cached_input_tokens
55 }
56
57 #[must_use]
59 pub const fn output_tokens(&self) -> u64 {
60 self.output_tokens
61 }
62
63 #[must_use]
65 pub const fn reasoning_output_tokens(&self) -> Option<u64> {
66 self.reasoning_output_tokens
67 }
68
69 #[must_use]
71 pub const fn total_tokens(&self) -> u64 {
72 self.total_tokens
73 }
74
75 #[must_use]
77 pub fn checked_add(self, other: Self) -> Option<Self> {
78 Some(Self {
79 input_tokens: self.input_tokens.checked_add(other.input_tokens)?,
80 cached_input_tokens: checked_add_optional(
81 self.cached_input_tokens,
82 other.cached_input_tokens,
83 )?,
84 output_tokens: self.output_tokens.checked_add(other.output_tokens)?,
85 reasoning_output_tokens: checked_add_optional(
86 self.reasoning_output_tokens,
87 other.reasoning_output_tokens,
88 )?,
89 total_tokens: self.total_tokens.checked_add(other.total_tokens)?,
90 })
91 }
92}
93
94fn checked_add_optional(left: Option<u64>, right: Option<u64>) -> Option<Option<u64>> {
95 match (left, right) {
96 (Some(left), Some(right)) => Some(Some(left.checked_add(right)?)),
97 (None, _) | (_, None) => Some(None),
98 }
99}
100
101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
103#[serde(deny_unknown_fields)]
104pub struct SessionUsage {
105 pub total: ModelUsage,
106 pub last: ModelUsage,
107 pub context: Option<UsageContextWindow>,
108 pub compaction: Option<CompactionUsageWindow>,
109 #[serde(default, skip_serializing_if = "Option::is_none")]
114 pub subagent_total: Option<ModelUsage>,
115}
116
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
119#[serde(deny_unknown_fields)]
120pub struct UsageContextWindow {
121 pub resolved_model_window_tokens: u64,
122 pub effective_window_tokens: u64,
123 pub source: ContextWindowSource,
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
128#[serde(deny_unknown_fields)]
129pub struct CompactionUsageWindow {
130 pub auto_compaction_enabled: bool,
131 pub dynamic_body_estimated_tokens: u64,
133 pub body_budget_tokens: u64,
134 pub compact_at_tokens: u64,
136}
137
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
140#[serde(rename_all = "snake_case")]
141pub enum ContextWindowSource {
142 ExplicitConfig,
144 ProviderCapabilities,
146 BundledCatalog,
148 Fallback,
150}
151
152impl ContextWindowSource {
153 #[must_use]
155 pub const fn as_str(self) -> &'static str {
156 match self {
157 Self::ExplicitConfig => "explicit_config",
158 Self::ProviderCapabilities => "provider_capabilities",
159 Self::BundledCatalog => "bundled_catalog",
160 Self::Fallback => "fallback",
161 }
162 }
163}