Skip to content

Commit 7f130ed

Browse files
Merge remote-tracking branch 'origin/main' into spinner
Co-authored-by: ryoppippi <[email protected]>
2 parents 84cd6de + 24a3ed0 commit 7f130ed

17 files changed

Lines changed: 683 additions & 48 deletions

File tree

‎apps/ccusage/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ bunx ccusage daily --by-agent --json
142142

143143
# Filters and options
144144
bunx ccusage daily --since 2026-04-25 --until 2026-05-16
145+
bunx ccusage daily --last 1 # Today
146+
bunx ccusage weekly --last 1 # This week
147+
bunx ccusage monthly --last 1 # This month
145148
bunx ccusage daily --json # JSON output
146149
bunx ccusage daily --no-cost # Hide cost columns and JSON cost fields
147150
bunx ccusage daily --timezone UTC # Use UTC timezone
@@ -167,6 +170,7 @@ bunx ccusage monthly --compact # Compact monthly report
167170
- 🤖 **Model Tracking**: See which models are used across supported sources
168171
- 📊 **Model Breakdown**: View per-model cost breakdown with `--breakdown` flag
169172
- 📅 **Date Filtering**: Filter reports by date range using `--since` and `--until`
173+
- ⏱️ **Recent Periods**: Jump to today, this week, or this month with `--last 1` on any daily, weekly, or monthly report
170174
- 📁 **Custom Paths**: Support for custom local data directory locations
171175
- 🎨 **Beautiful Output**: Colorful table-formatted display with automatic responsive layout
172176
- 📱 **Smart Tables**: Automatic compact mode for narrow terminals (< 100 characters) with essential columns

‎docs/guide/cli-options.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ ccusage monthly --since 20260101
2121
ccusage session --until 20260531
2222
```
2323

24+
### Recent Periods
25+
26+
Instead of working out dates, ask for the most recent periods of whatever the report groups by:
27+
28+
```bash
29+
# Today
30+
ccusage daily --last 1
31+
32+
# This week
33+
ccusage weekly --last 1
34+
35+
# This month
36+
ccusage monthly --last 1
37+
38+
# The last seven days, and the last three months
39+
ccusage daily --last 7
40+
ccusage monthly --last 3
41+
```
42+
43+
The count is inclusive of the current period, so `--last 2` on a daily report covers yesterday and today. Weeks start on the same day the report buckets by, which is Monday everywhere except `ccusage claude weekly`, where `--start-of-week` decides.
44+
45+
`--last` works on every daily, weekly, and monthly report, including the per-agent ones such as `ccusage codex daily --last 1`. It is not available on `session`, `blocks`, or `statusline`, which have no calendar period, and it cannot be combined with `--since`, `--until`, or `--sections`.
46+
2447
### Output Format
2548

2649
Control how data is displayed:

‎docs/guide/daily-reports.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ ccusage daily --since 20260510 --until 20260516
6767
ccusage daily --since 20260501
6868
```
6969

70+
### Recent Days
71+
72+
Skip the date arithmetic when you only want the days just gone:
73+
74+
```bash
75+
# Today
76+
ccusage daily --last 1
77+
78+
# The last seven days, including today
79+
ccusage daily --last 7
80+
```
81+
82+
`--last` cannot be combined with `--since` or `--until`. See [Command-Line Options](/guide/cli-options#recent-periods) for how it behaves on the other reports.
83+
7084
### Sort Order
7185

7286
Control the order of dates:

‎docs/guide/monthly-reports.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ ccusage monthly --since $(date -d '6 months ago' +%Y%m%d)
7171
Even though you specify full dates (YYYYMMDD), monthly reports group by month. The filters determine which months to include.
7272
:::
7373

74+
### Recent Months
75+
76+
`--last` counts whole months, so the shell date arithmetic above is not needed:
77+
78+
```bash
79+
# This month so far
80+
ccusage monthly --last 1
81+
82+
# This month and the five before it
83+
ccusage monthly --last 6
84+
```
85+
86+
`--last` cannot be combined with `--since` or `--until`.
87+
7488
### Sort Order
7589

7690
```bash

‎docs/guide/weekly-reports.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ ccusage weekly --since 20260501 --until 20260531
6969
ccusage weekly --since 20260420
7070
```
7171

72+
### Recent Weeks
73+
74+
Count back in whole weeks instead of dates:
75+
76+
```bash
77+
# This week so far
78+
ccusage weekly --last 1
79+
80+
# This week and the three before it
81+
ccusage weekly --last 4
82+
```
83+
84+
The window starts on the same weekday the report buckets by, so `ccusage claude weekly --last 1 -w monday` starts on Monday. `--last` cannot be combined with `--since` or `--until`.
85+
7286
### Sort Order
7387

7488
Control the order of weeks:

‎rust/adapters/codex/src/loader.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ mod tests {
582582
assert_eq!(events[2].input_tokens, 9);
583583
assert_eq!(events[2].output_tokens, 4);
584584
assert_eq!(events[2].reasoning_output_tokens, 1);
585-
assert_eq!(events[2].total_tokens, 14);
585+
assert_eq!(events[2].total_tokens, 13);
586586
}
587587

588588
#[test]

‎rust/adapters/codex/src/types.rs‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,13 @@ impl<'de> Deserialize<'de> for CodexRawUsage {
288288
.unwrap_or(0),
289289
output_tokens: output,
290290
reasoning_output_tokens: reasoning,
291+
// Codex reports reasoning as a subset of output, so a derived total
292+
// must not add it on top. A recorded zero total means the field is
293+
// unusable rather than that the turn spent nothing, so derive it too.
291294
total_tokens: fields
292295
.total_tokens
293-
.filter(|total| *total > 0 || input + output + reasoning == 0)
294-
.unwrap_or(input + output + reasoning),
296+
.filter(|total| *total > 0)
297+
.unwrap_or_else(|| input.saturating_add(output)),
295298
})
296299
}
297300
}
@@ -478,3 +481,90 @@ where
478481

479482
deserializer.deserialize_any(OptionalU64Visitor)
480483
}
484+
485+
#[cfg(test)]
486+
mod tests {
487+
use super::*;
488+
489+
#[test]
490+
fn derives_total_tokens_without_double_counting_reasoning() {
491+
let usage: CodexRawUsage = serde_json::from_str(
492+
r#"{
493+
"input_tokens": 100,
494+
"output_tokens": 50,
495+
"reasoning_output_tokens": 20
496+
}"#,
497+
)
498+
.expect("usage should deserialize");
499+
500+
assert_eq!(usage.total_tokens, 150);
501+
}
502+
503+
#[test]
504+
fn derives_explicit_zero_total_tokens_without_double_counting_reasoning() {
505+
let usage: CodexRawUsage = serde_json::from_str(
506+
r#"{
507+
"input_tokens": 9,
508+
"output_tokens": 4,
509+
"reasoning_output_tokens": 1,
510+
"total_tokens": 0
511+
}"#,
512+
)
513+
.expect("usage should deserialize");
514+
515+
assert_eq!(usage.total_tokens, 13);
516+
}
517+
518+
#[test]
519+
fn keeps_a_recorded_total() {
520+
// Deliberately not input plus output, so deriving instead of preserving
521+
// the recorded value cannot pass this test.
522+
let usage: CodexRawUsage = serde_json::from_str(
523+
r#"{
524+
"input_tokens": 100,
525+
"output_tokens": 50,
526+
"reasoning_output_tokens": 20,
527+
"total_tokens": 151
528+
}"#,
529+
)
530+
.expect("usage should deserialize");
531+
532+
assert_eq!(usage.total_tokens, 151);
533+
}
534+
535+
#[test]
536+
fn saturates_a_derived_total_instead_of_overflowing() {
537+
let usage: CodexRawUsage = serde_json::from_str(
538+
r#"{
539+
"input_tokens": 18446744073709551615,
540+
"output_tokens": 5
541+
}"#,
542+
)
543+
.expect("usage should deserialize");
544+
545+
assert_eq!(usage.total_tokens, u64::MAX);
546+
}
547+
548+
#[test]
549+
fn derives_total_tokens_from_openai_field_spellings() {
550+
let usage: CodexRawUsage = serde_json::from_str(
551+
r#"{
552+
"prompt_tokens": 50,
553+
"cached_tokens": 5,
554+
"completion_tokens": 12,
555+
"reasoning_tokens": 4
556+
}"#,
557+
)
558+
.expect("usage should deserialize");
559+
560+
assert_eq!(usage.total_tokens, 62);
561+
}
562+
563+
#[test]
564+
fn leaves_an_empty_usage_total_at_zero() {
565+
let usage: CodexRawUsage =
566+
serde_json::from_str(r#"{"total_tokens": 0}"#).expect("usage should deserialize");
567+
568+
assert_eq!(usage.total_tokens, 0);
569+
}
570+
}

0 commit comments

Comments
 (0)