ctxone 1.3.2

The `ctx` CLI for CTXone, memory and context for AI agents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Session token-usage metrics: parse Claude Code JSONL, compute costs,
//! detect units of work, and render tabular summaries.

use chrono::{DateTime, Utc};
use serde::Serialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

// ── Pricing (per million tokens) ─────────────────────────────────────────────

struct ModelPricing {
    input: f64,
    output: f64,
    cache_write: f64,
    cache_read: f64,
}

fn pricing_for(model: &str) -> ModelPricing {
    if model.contains("claude-opus-4") {
        ModelPricing {
            input: 15.00,
            output: 75.00,
            cache_write: 18.75,
            cache_read: 1.50,
        }
    } else if model.contains("claude-sonnet-4")
        || model.contains("claude-sonnet-3-7")
        || model.contains("claude-3-7-sonnet")
        || model.contains("claude-sonnet-3-5")
        || model.contains("claude-3-5-sonnet")
    {
        ModelPricing {
            input: 3.00,
            output: 15.00,
            cache_write: 3.75,
            cache_read: 0.30,
        }
    } else if model.contains("claude-haiku-4") || model.contains("claude-3-5-haiku") {
        ModelPricing {
            input: 0.80,
            output: 4.00,
            cache_write: 1.00,
            cache_read: 0.08,
        }
    } else if model.contains("claude-haiku") {
        ModelPricing {
            input: 0.25,
            output: 1.25,
            cache_write: 0.30,
            cache_read: 0.03,
        }
    } else if model.contains("claude-opus") {
        ModelPricing {
            input: 15.00,
            output: 75.00,
            cache_write: 18.75,
            cache_read: 1.50,
        }
    } else if model.contains("claude") {
        ModelPricing {
            input: 3.00,
            output: 15.00,
            cache_write: 3.75,
            cache_read: 0.30,
        }
    } else {
        // Unknown / non-Anthropic
        ModelPricing {
            input: 3.00,
            output: 15.00,
            cache_write: 0.00,
            cache_read: 0.00,
        }
    }
}

// ── Data model ────────────────────────────────────────────────────────────────

#[derive(Debug, Default, Serialize, Clone)]
pub struct TurnMetrics {
    pub model: String,
    pub timestamp: String,
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub cache_read_tokens: u64,
    pub cache_creation_tokens: u64,
    pub tool_calls: u64,
    pub web_searches: u64,
    pub web_fetches: u64,
}

#[derive(Debug, Default, Serialize, Clone)]
pub struct UnitOfWork {
    pub index: usize,
    pub start_ts: String,
    pub end_ts: String,
    pub duration_seconds: f64,
    pub turns: u64,
    pub tool_calls: u64,
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub cache_read_tokens: u64,
    pub cache_creation_tokens: u64,
    pub cache_hit_rate: f64,
    pub model: String,
    pub cost_usd: f64,
}

#[derive(Debug, Default, Serialize, Clone)]
pub struct SessionMetrics {
    pub session_id: String,
    pub source_file: String,
    pub first_ts: String,
    pub last_ts: String,
    pub wall_seconds: f64,
    pub turns: u64,
    pub tool_calls: u64,
    pub web_searches: u64,
    pub web_fetches: u64,
    pub input_tokens: u64,
    pub output_tokens: u64,
    pub cache_read_tokens: u64,
    pub cache_creation_tokens: u64,
    pub models: HashMap<String, u64>,
    pub cache_hit_rate: f64,
    pub cost_usd: f64,
    pub cost_no_cache_usd: f64,
    pub cache_savings_usd: f64,
    pub units: Vec<UnitOfWork>,
    #[serde(skip)]
    pub turn_details: Vec<TurnMetrics>,
}

impl SessionMetrics {
    pub fn finalize(&mut self, gap_minutes: f64) {
        if !self.first_ts.is_empty() && !self.last_ts.is_empty() {
            if let (Ok(t0), Ok(t1)) = (
                self.first_ts.parse::<DateTime<Utc>>(),
                self.last_ts.parse::<DateTime<Utc>>(),
            ) {
                self.wall_seconds = (t1 - t0).num_seconds().max(0) as f64;
            }
        }

        let total_input = self.input_tokens + self.cache_read_tokens + self.cache_creation_tokens;
        self.cache_hit_rate = if total_input > 0 {
            self.cache_read_tokens as f64 / total_input as f64
        } else {
            0.0
        };

        self.cost_usd = self.compute_cost(true);
        self.cost_no_cache_usd = self.compute_cost(false);
        self.cache_savings_usd = (self.cost_no_cache_usd - self.cost_usd).max(0.0);
        self.units = detect_units(&self.turn_details, gap_minutes);
    }

    pub fn dominant_model(&self) -> &str {
        self.models
            .iter()
            .max_by_key(|&(_, &v)| v)
            .map(|(k, _)| k.as_str())
            .unwrap_or("unknown")
    }

    /// Compute cost per-turn using each turn's own model pricing, then sum.
    /// Falls back to dominant-model pricing when turn_details is empty (aggregates).
    fn compute_cost(&self, with_cache: bool) -> f64 {
        if !self.turn_details.is_empty() {
            self.turn_details
                .iter()
                .map(|t| {
                    let p = pricing_for(&t.model);
                    if with_cache {
                        t.input_tokens as f64 * p.input / 1_000_000.0
                            + t.output_tokens as f64 * p.output / 1_000_000.0
                            + t.cache_read_tokens as f64 * p.cache_read / 1_000_000.0
                            + t.cache_creation_tokens as f64 * p.cache_write / 1_000_000.0
                    } else {
                        let total_in =
                            t.input_tokens + t.cache_read_tokens + t.cache_creation_tokens;
                        total_in as f64 * p.input / 1_000_000.0
                            + t.output_tokens as f64 * p.output / 1_000_000.0
                    }
                })
                .sum()
        } else {
            let p = pricing_for(self.dominant_model());
            let output_cost = self.output_tokens as f64 * p.output / 1_000_000.0;
            if with_cache {
                self.input_tokens as f64 * p.input / 1_000_000.0
                    + output_cost
                    + self.cache_read_tokens as f64 * p.cache_read / 1_000_000.0
                    + self.cache_creation_tokens as f64 * p.cache_write / 1_000_000.0
            } else {
                let total_input =
                    self.input_tokens + self.cache_read_tokens + self.cache_creation_tokens;
                total_input as f64 * p.input / 1_000_000.0 + output_cost
            }
        }
    }

    /// Accumulate another session into this aggregate.
    pub fn add(&mut self, other: &SessionMetrics) {
        self.turns += other.turns;
        self.tool_calls += other.tool_calls;
        self.web_searches += other.web_searches;
        self.web_fetches += other.web_fetches;
        self.input_tokens += other.input_tokens;
        self.output_tokens += other.output_tokens;
        self.cache_read_tokens += other.cache_read_tokens;
        self.cache_creation_tokens += other.cache_creation_tokens;
        self.wall_seconds += other.wall_seconds;
        self.cost_usd += other.cost_usd;
        self.cost_no_cache_usd += other.cost_no_cache_usd;
        self.cache_savings_usd += other.cache_savings_usd;
        for (model, count) in &other.models {
            *self.models.entry(model.clone()).or_insert(0) += count;
        }
        if self.first_ts.is_empty()
            || (!other.first_ts.is_empty() && other.first_ts < self.first_ts)
        {
            self.first_ts = other.first_ts.clone();
        }
        if other.last_ts > self.last_ts {
            self.last_ts = other.last_ts.clone();
        }
        let total_input = self.input_tokens + self.cache_read_tokens + self.cache_creation_tokens;
        self.cache_hit_rate = if total_input > 0 {
            self.cache_read_tokens as f64 / total_input as f64
        } else {
            0.0
        };
    }
}

// ── Unit-of-work detection ─────────────────────────────────────────────────────

pub fn detect_units(turns: &[TurnMetrics], gap_minutes: f64) -> Vec<UnitOfWork> {
    if turns.is_empty() {
        return vec![];
    }
    let gap_secs = gap_minutes * 60.0;
    let mut units: Vec<UnitOfWork> = vec![];
    let mut bucket: Vec<&TurnMetrics> = vec![];

    for turn in turns {
        let split = bucket
            .last()
            .map(|prev| {
                if let (Ok(t0), Ok(t1)) = (
                    prev.timestamp.parse::<DateTime<Utc>>(),
                    turn.timestamp.parse::<DateTime<Utc>>(),
                ) {
                    (t1 - t0).num_seconds() as f64 > gap_secs
                } else {
                    false
                }
            })
            .unwrap_or(false);

        if split {
            units.push(build_unit(units.len(), &bucket));
            bucket.clear();
        }
        bucket.push(turn);
    }
    if !bucket.is_empty() {
        units.push(build_unit(units.len(), &bucket));
    }
    units
}

fn build_unit(index: usize, turns: &[&TurnMetrics]) -> UnitOfWork {
    let start_ts = turns
        .first()
        .map(|t| t.timestamp.clone())
        .unwrap_or_default();
    let end_ts = turns
        .last()
        .map(|t| t.timestamp.clone())
        .unwrap_or_default();

    let duration_seconds = if let (Ok(t0), Ok(t1)) = (
        start_ts.parse::<DateTime<Utc>>(),
        end_ts.parse::<DateTime<Utc>>(),
    ) {
        (t1 - t0).num_seconds().max(0) as f64
    } else {
        0.0
    };

    let input: u64 = turns.iter().map(|t| t.input_tokens).sum();
    let output: u64 = turns.iter().map(|t| t.output_tokens).sum();
    let cache_read: u64 = turns.iter().map(|t| t.cache_read_tokens).sum();
    let cache_creation: u64 = turns.iter().map(|t| t.cache_creation_tokens).sum();
    let tool_calls: u64 = turns.iter().map(|t| t.tool_calls).sum();

    let total_input = input + cache_read + cache_creation;
    let cache_hit_rate = if total_input > 0 {
        cache_read as f64 / total_input as f64
    } else {
        0.0
    };

    let model = turns
        .iter()
        .filter(|t| !t.model.is_empty())
        .last()
        .map(|t| t.model.clone())
        .unwrap_or_default();

    let p = pricing_for(&model);
    let cost_usd = input as f64 * p.input / 1_000_000.0
        + output as f64 * p.output / 1_000_000.0
        + cache_read as f64 * p.cache_read / 1_000_000.0
        + cache_creation as f64 * p.cache_write / 1_000_000.0;

    UnitOfWork {
        index,
        start_ts,
        end_ts,
        duration_seconds,
        turns: turns.len() as u64,
        tool_calls,
        input_tokens: input,
        output_tokens: output,
        cache_read_tokens: cache_read,
        cache_creation_tokens: cache_creation,
        cache_hit_rate,
        model,
        cost_usd,
    }
}

// ── Parsing ────────────────────────────────────────────────────────────────────

pub fn parse_session_metrics(path: &Path, gap_minutes: f64) -> SessionMetrics {
    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("warn: could not read {}: {}", path.display(), e);
            return SessionMetrics::default();
        }
    };

    let session_id = path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown")
        .to_string();

    let mut sm = SessionMetrics {
        session_id,
        source_file: path.to_string_lossy().to_string(),
        ..Default::default()
    };

    let mut seen_uuids: std::collections::HashSet<String> = std::collections::HashSet::new();

    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let entry: serde_json::Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => continue,
        };

        if entry.get("type").and_then(|v| v.as_str()) != Some("assistant") {
            continue;
        }

        if let Some(uuid) = entry.get("uuid").and_then(|v| v.as_str()) {
            if !seen_uuids.insert(uuid.to_string()) {
                continue;
            }
        }

        let msg = match entry.get("message") {
            Some(m) => m,
            None => continue,
        };

        let usage = match msg.get("usage") {
            Some(u) => u,
            None => continue,
        };

        let input = usage
            .get("input_tokens")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);
        let output = usage
            .get("output_tokens")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);
        let cache_read = usage
            .get("cache_read_input_tokens")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);
        let cache_creation = usage
            .get("cache_creation_input_tokens")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);

        if input == 0 && output == 0 && cache_read == 0 && cache_creation == 0 {
            continue;
        }

        let model = normalize_model(
            msg.get("model")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown"),
        );

        let timestamp = entry
            .get("timestamp")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        let mut tool_calls = 0u64;
        let mut web_searches = 0u64;
        let mut web_fetches = 0u64;
        if let Some(blocks) = msg.get("content").and_then(|v| v.as_array()) {
            for block in blocks {
                if block.get("type").and_then(|v| v.as_str()) == Some("tool_use") {
                    tool_calls += 1;
                    match block.get("name").and_then(|v| v.as_str()) {
                        Some("WebSearch") | Some("web_search") => web_searches += 1,
                        Some("WebFetch") | Some("web_fetch") => web_fetches += 1,
                        _ => {}
                    }
                }
            }
        }

        if (sm.first_ts.is_empty() || timestamp < sm.first_ts) && !timestamp.is_empty() {
            sm.first_ts = timestamp.clone();
        }
        if timestamp > sm.last_ts {
            sm.last_ts = timestamp.clone();
        }

        sm.input_tokens += input;
        sm.output_tokens += output;
        sm.cache_read_tokens += cache_read;
        sm.cache_creation_tokens += cache_creation;
        sm.tool_calls += tool_calls;
        sm.web_searches += web_searches;
        sm.web_fetches += web_fetches;
        sm.turns += 1;
        *sm.models.entry(model.clone()).or_insert(0) += 1;

        sm.turn_details.push(TurnMetrics {
            model,
            timestamp,
            input_tokens: input,
            output_tokens: output,
            cache_read_tokens: cache_read,
            cache_creation_tokens: cache_creation,
            tool_calls,
            web_searches,
            web_fetches,
        });
    }

    sm.finalize(gap_minutes);
    sm
}

fn normalize_model(model: &str) -> String {
    // Strip 8-digit date suffix: "claude-haiku-4-5-20251001" -> "claude-haiku-4-5"
    if let Some(pos) = model.rfind('-') {
        let suffix = &model[pos + 1..];
        if suffix.len() == 8 && suffix.chars().all(|c| c.is_ascii_digit()) {
            return model[..pos].to_string();
        }
    }
    model.to_string()
}

// ── File discovery ─────────────────────────────────────────────────────────────

/// Find all Claude Code session files for a given project directory.
pub fn find_session_files(project_dir: &Path) -> Vec<PathBuf> {
    crate::ingest::find_session_files(project_dir)
}

/// List every project in ~/.claude/projects/ and return (label, files) pairs.
///
/// This was a verbatim second copy of the walk in `ingest.rs` — same hash
/// decoding, same mtime sort — which meant a discovery fix had to be made
/// twice. Both now go through the Claude Code source.
pub fn all_project_sessions() -> Vec<(String, Vec<PathBuf>)> {
    crate::ingest::find_all_session_files()
}

// ── Formatting helpers ─────────────────────────────────────────────────────────

pub fn fmt_tokens(n: u64) -> String {
    if n >= 1_000_000 {
        format!("{:.1}M", n as f64 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.1}K", n as f64 / 1_000.0)
    } else {
        n.to_string()
    }
}

pub fn fmt_duration(secs: f64) -> String {
    let s = secs as u64;
    if s >= 3600 {
        format!("{}h {:02}m", s / 3600, (s % 3600) / 60)
    } else if s >= 60 {
        format!("{}m {:02}s", s / 60, s % 60)
    } else {
        format!("{}s", s)
    }
}

pub fn fmt_ts_short(ts: &str) -> String {
    // "2025-01-02T15:04:05.000Z" -> "2025-01-02 15:04"
    ts.get(..16)
        .map(|s| s.replace('T', " "))
        .unwrap_or_else(|| ts.to_string())
}

/// Render a single-session or aggregate metrics table to stdout.
pub fn render_metrics(sm: &SessionMetrics, label: &str, gap: f64, verbose: bool) {
    let bar = "─".repeat(62);
    println!("\n{}", bar);
    println!("  Session Metrics — {}", label);
    println!("{}", bar);

    let model_str = {
        let mut v: Vec<(&String, &u64)> = sm.models.iter().collect();
        v.sort_by_key(|&(_, &c)| std::cmp::Reverse(c));
        v.iter()
            .take(3)
            .map(|(m, _)| m.as_str())
            .collect::<Vec<_>>()
            .join(", ")
    };

    println!("  Turns            {:>8}  (model: {})", sm.turns, model_str);
    println!(
        "  Tool calls       {:>8}  (web search: {}, fetch: {})",
        sm.tool_calls, sm.web_searches, sm.web_fetches
    );
    if sm.wall_seconds > 0.0 {
        println!("  Wall clock       {:>8}", fmt_duration(sm.wall_seconds));
    }

    println!();
    println!("  Token Usage");
    println!("    Input (fresh)  {:>10}", fmt_tokens(sm.input_tokens));
    println!("    Output         {:>10}", fmt_tokens(sm.output_tokens));
    let total_in = sm.input_tokens + sm.cache_read_tokens + sm.cache_creation_tokens;
    let effective = total_in + sm.output_tokens;
    println!(
        "    Cache read     {:>10}  ({:.1}% hit rate, billed at ~10% input rate)",
        fmt_tokens(sm.cache_read_tokens),
        sm.cache_hit_rate * 100.0
    );
    println!(
        "    Cache write    {:>10}  (billed at ~125% input rate)",
        fmt_tokens(sm.cache_creation_tokens)
    );
    println!(
        "    Total input    {:>10}  (fresh + cache read + cache write)",
        fmt_tokens(total_in)
    );
    println!(
        "    Effective      {:>10}  (total input + output processed)",
        fmt_tokens(effective)
    );

    // Effective rate = actual cost per million effective tokens (reveals true discount)
    let eff_rate = if effective > 0 {
        sm.cost_usd / effective as f64 * 1_000_000.0
    } else {
        0.0
    };
    let list_rate = if effective > 0 {
        sm.cost_no_cache_usd / effective as f64 * 1_000_000.0
    } else {
        0.0
    };

    println!();
    println!("  Cost  (per-turn model pricing)");
    println!(
        "    With cache     {:>10}  (${:.2}/M effective)",
        format!("${:.4}", sm.cost_usd),
        eff_rate
    );
    println!(
        "    Without cache  {:>10}  (${:.2}/M effective)",
        format!("${:.4}", sm.cost_no_cache_usd),
        list_rate
    );
    println!(
        "    Savings        {:>10}  ({:.1}% reduction from cache discount)",
        format!("${:.4}", sm.cache_savings_usd),
        if sm.cost_no_cache_usd > 0.0 {
            sm.cache_savings_usd / sm.cost_no_cache_usd * 100.0
        } else {
            0.0
        }
    );
    if sm.turns > 0 {
        println!(
            "    Avg / turn     {:>10}",
            format!("${:.5}", sm.cost_usd / sm.turns as f64)
        );
    }

    if !sm.units.is_empty() {
        println!();
        println!("  Units of Work  (gap = {:.0} min)", gap);
        for u in &sm.units {
            let start = fmt_ts_short(&u.start_ts);
            println!(
                "    #{:<2}  {}  {:>6}  {:>3} turns  {:>8} in  {:>7} out  {:>5.1}% cache  ${:.4}",
                u.index + 1,
                start,
                fmt_duration(u.duration_seconds),
                u.turns,
                fmt_tokens(u.input_tokens),
                fmt_tokens(u.output_tokens),
                u.cache_hit_rate * 100.0,
                u.cost_usd,
            );
        }
    }

    if verbose && !sm.turn_details.is_empty() {
        println!();
        println!("  Turn Detail");
        println!(
            "    {:4}  {:16}  {:>8}  {:>7}  {:>8}  {:>8}  {}",
            "#", "Timestamp", "Input", "Output", "CacheRd", "CacheWr", "Model"
        );
        for (i, t) in sm.turn_details.iter().enumerate() {
            println!(
                "    {:4}  {:16}  {:>8}  {:>7}  {:>8}  {:>8}  {}",
                i + 1,
                fmt_ts_short(&t.timestamp),
                fmt_tokens(t.input_tokens),
                fmt_tokens(t.output_tokens),
                fmt_tokens(t.cache_read_tokens),
                fmt_tokens(t.cache_creation_tokens),
                t.model,
            );
        }
    }

    println!("{}", bar);
}

/// Render a compact list row for `--list` mode.
pub fn render_list_row(sm: &SessionMetrics) {
    let ts = fmt_ts_short(&sm.first_ts);
    let savings_pct = if sm.cost_no_cache_usd > 0.0 {
        sm.cache_savings_usd / sm.cost_no_cache_usd * 100.0
    } else {
        0.0
    };
    println!(
        "  {}  {:16}  {:>3} turns  {:>8} in  {:>7} out  {:>5.1}% cache  ${:.4}  (saved {:.0}%)",
        &sm.session_id[..8.min(sm.session_id.len())],
        ts,
        sm.turns,
        fmt_tokens(sm.input_tokens),
        fmt_tokens(sm.output_tokens),
        sm.cache_hit_rate * 100.0,
        sm.cost_usd,
        savings_pct,
    );
}

/// Format a savings summary: "$X.XX (saved Y.Y%)" for compact views.
pub fn fmt_cost_with_savings(cost: f64, savings: f64, no_cache: f64) -> String {
    let pct = if no_cache > 0.0 {
        savings / no_cache * 100.0
    } else {
        0.0
    };
    format!("${:.2}  (saved {:.0}% vs no-cache)", cost, pct)
}