lean_ctx/core/stats/
model.rs1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Serialize, Deserialize, Default, Clone)]
6pub struct StatsStore {
7 pub total_commands: u64,
8 pub total_input_tokens: u64,
9 pub total_output_tokens: u64,
10 pub first_use: Option<String>,
11 pub last_use: Option<String>,
12 pub commands: HashMap<String, CommandStats>,
13 pub daily: Vec<DayStats>,
14 #[serde(default)]
15 pub cep: CepStats,
16}
17
18#[derive(Serialize, Deserialize, Clone, Default)]
20pub struct CepStats {
21 pub sessions: u64,
22 pub total_cache_hits: u64,
23 pub total_cache_reads: u64,
24 pub total_tokens_original: u64,
25 pub total_tokens_compressed: u64,
26 pub modes: HashMap<String, u64>,
27 pub scores: Vec<CepSessionSnapshot>,
28 #[serde(default)]
29 pub last_session_pid: Option<u32>,
30 #[serde(default)]
31 pub last_session_original: Option<u64>,
32 #[serde(default)]
33 pub last_session_compressed: Option<u64>,
34}
35
36#[derive(Serialize, Deserialize, Clone)]
38pub struct CepSessionSnapshot {
39 pub timestamp: String,
40 pub score: u32,
41 pub cache_hit_rate: u32,
42 pub mode_diversity: u32,
43 pub compression_rate: u32,
44 pub tool_calls: u64,
45 pub tokens_saved: u64,
46 pub complexity: String,
47}
48
49#[derive(Serialize, Deserialize, Clone, Default, Debug)]
51pub struct CommandStats {
52 pub count: u64,
53 pub input_tokens: u64,
54 pub output_tokens: u64,
55}
56
57#[derive(Serialize, Deserialize, Clone, Default)]
59pub struct DayStats {
60 pub date: String,
61 pub commands: u64,
62 pub input_tokens: u64,
63 pub output_tokens: u64,
64 #[serde(default)]
68 pub version: String,
69}
70
71pub struct GainSummary {
73 pub total_saved: u64,
74 pub total_calls: u64,
75}
76
77pub const DEFAULT_INPUT_PRICE_PER_M: f64 = 2.50;
79pub const DEFAULT_OUTPUT_PRICE_PER_M: f64 = 10.0;
80
81pub struct CostModel {
83 pub input_price_per_m: f64,
84 pub output_price_per_m: f64,
85 pub avg_verbose_output_per_call: u64,
86 pub avg_concise_output_per_call: u64,
87}
88
89impl Default for CostModel {
90 fn default() -> Self {
91 let env_model = std::env::var("LEAN_CTX_MODEL")
92 .or_else(|_| std::env::var("LCTX_MODEL"))
93 .ok();
94 let pricing = crate::core::gain::model_pricing::ModelPricing::load();
95 let quote = pricing.quote(env_model.as_deref());
96 Self {
97 input_price_per_m: quote.cost.input_per_m,
98 output_price_per_m: quote.cost.output_per_m,
99 avg_verbose_output_per_call: 180,
100 avg_concise_output_per_call: 120,
101 }
102 }
103}
104
105pub struct CostBreakdown {
107 pub input_cost_without: f64,
108 pub input_cost_with: f64,
109 pub output_cost_without: f64,
110 pub output_cost_with: f64,
111 pub total_cost_without: f64,
112 pub total_cost_with: f64,
113 pub total_saved: f64,
114 pub estimated_output_tokens_without: u64,
115 pub estimated_output_tokens_with: u64,
116 pub output_tokens_saved: u64,
117}
118
119impl CostModel {
120 pub fn calculate(&self, store: &StatsStore) -> CostBreakdown {
122 let input_cost_without =
123 store.total_input_tokens as f64 / 1_000_000.0 * self.input_price_per_m;
124 let input_cost_with =
125 store.total_output_tokens as f64 / 1_000_000.0 * self.input_price_per_m;
126
127 let input_saved = store
128 .total_input_tokens
129 .saturating_sub(store.total_output_tokens);
130 let compression_rate = if store.total_input_tokens > 0 {
131 input_saved as f64 / store.total_input_tokens as f64
132 } else {
133 0.0
134 };
135 let est_output_without = store.total_commands * self.avg_verbose_output_per_call;
136 let est_output_with = if compression_rate > 0.01 {
137 store.total_commands * self.avg_concise_output_per_call
138 } else {
139 est_output_without
140 };
141 let output_saved = est_output_without.saturating_sub(est_output_with);
142
143 let output_cost_without = est_output_without as f64 / 1_000_000.0 * self.output_price_per_m;
144 let output_cost_with = est_output_with as f64 / 1_000_000.0 * self.output_price_per_m;
145
146 let total_without = input_cost_without + output_cost_without;
147 let total_with = input_cost_with + output_cost_with;
148
149 CostBreakdown {
150 input_cost_without,
151 input_cost_with,
152 output_cost_without,
153 output_cost_with,
154 total_cost_without: total_without,
155 total_cost_with: total_with,
156 total_saved: total_without - total_with,
157 estimated_output_tokens_without: est_output_without,
158 estimated_output_tokens_with: est_output_with,
159 output_tokens_saved: output_saved,
160 }
161 }
162}