feat: add --last to report the most recent N periods - #1498
Conversation
Reports that group rows by day, week, or month all filter through the same compact YYYYMMDD `--since` bound, so a shortcut for "the most recent N periods" only needs to resolve that bound. `last_periods_since` takes the report's calendar unit, the count, the current date already resolved in the report timezone, and the week start the report buckets with. Passing the date in keeps the helper deterministic in tests, and reusing `week_start` keeps the window aligned with the weekly bucketing itself instead of duplicating the weekday math. Co-authored-by: ryoppippi <[email protected]>
Checking today's or this week's usage meant working out the dates and passing `--since` by hand. `-l, --last N` now names the window in the unit the report already groups by: `ccusage daily --last 1` is today, `ccusage weekly --last 1` is the current week, and `ccusage monthly --last 1` is this month. It works the same way on the per-agent reports, so `ccusage codex daily --last 7` covers the last seven days. The parser only records the count, because the calendar unit it counts depends on the command; the binary resolves it into `--since` once the command is known, using the report timezone for "today" and the command's own week start. Every loader already honors `--since`, so nothing downstream changes. `--last` is rejected on the session, blocks, and statusline reports, which have no calendar period, and alongside `--since`, `--until`, or `--sections`, where the intended window would be ambiguous. Co-authored-by: ryoppippi <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (14)
📝 WalkthroughWalkthroughThe CLI adds a validated ChangesRecent periods filtering
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant Parser
participant CLIResolver
participant CoreWindow
User->>Parser: provide --last N and report command
Parser->>Parser: validate and store shared.last
Parser-->>CLIResolver: return parsed Cli
CLIResolver->>CoreWindow: calculate period start
CoreWindow-->>CLIResolver: return since date
CLIResolver-->>User: continue with resolved report window
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
ccusage-guide | 834e1d3 | Commit Preview URL Branch Preview URL |
Jul 27 2026, 01:13 PM |
Cover `--last` where readers look for date filtering: a general section in the CLI options guide with the rules that are easy to trip over (the count includes the current period, the week start follows the report, and it cannot be mixed with `--since`, `--until`, or `--sections`), and a short per-report section in the daily, weekly, and monthly guides that links back to it. The README gains the three shortcut examples next to the existing `--since`/`--until` example and a feature bullet, since that list is what people scan before reading the guides. Co-authored-by: ryoppippi <[email protected]>
ccusage
@ccusage/ccusage-darwin-arm64
@ccusage/ccusage-darwin-x64
@ccusage/ccusage-linux-arm64
@ccusage/ccusage-linux-x64
@ccusage/ccusage-win32-x64
commit: |
`-l` was the short alias of `--locale` until 8792ff5 removed that option, so reusing it would silently rebind a flag people may still have in scripts and aliases. The long form is explicit enough on its own, and a test now records that `-l` stays unclaimed. Co-authored-by: ryoppippi <[email protected]>
-l, --last to report the most recent N periods--last to report the most recent N periods
ccusage performance comparisonPR SHA: Performance comparison skipped. Base package URL was not ready before 300.000s. Fixture performance comparison requires a base package when --base-dir is not provided. Base package: |
ccusage performance comparisonPR SHA: Performance comparison skipped. Base package URL was not ready before 300.000s. Fixture performance comparison requires a base package when --base-dir is not provided. Base package: |
ccusage performance comparisonPR SHA: Performance comparison skipped. Base package URL was not ready before 300.000s. Fixture performance comparison requires a base package when --base-dir is not provided. Base package: |
ccusage performance comparisonPR SHA: Performance comparison skipped. Base package URL was not ready before 300.000s. Fixture performance comparison requires a base package when --base-dir is not provided. Base package: |
ccusage performance comparisonPR SHA: Performance comparison skipped. Base package URL was not ready before 300.000s. Fixture performance comparison requires a base package when --base-dir is not provided. Base package: |
ccusage performance comparisonPR SHA: Performance comparison skipped. Base package URL was not ready before 300.000s. Fixture performance comparison requires a base package when --base-dir is not provided. Base package: |
There was a problem hiding this comment.
Reviewed changes — adds --last <N> to daily/weekly/monthly reports, auto-resolved to a YYYYMMDD --since bound so users can say "today" or "last 3 weeks" without date arithmetic. Rejected on non-period commands and alongside conflicting flags.
- New
last_periods_sinceinccusage-core— pure date math that converts a calendar unit plus a count into a compactYYYYMMDDstart, respecting the report's week-start convention. - Parser support —
--last Nis a shared option parsed asu32(rejecting 0 and non-numeric values), with alast_option_errorgate that blocks it onsession,blocks,statusline, and alongside--since/--until. - Binary resolution —
last_window::resolvemaps each command to its period unit, resolves "today" in the report timezone, and writes the computed--sinceback intoSharedArgs. Bareccusageis treated as a unified daily. cli-commands.json/cli-help.jsoncoverage — newlast_optionsgroup wired into every period-report path (unified and per-agent).- Docs — README and per-report guide pages got "Recent Periods" sections with examples and cross-links.
- Tests — unit tests for date math (day/week/month, week-start permutations, year wraparound, zero-to-one clamp), parser rejection paths, and binary resolution of every command shape.
- Cleanup — extracted the repeated
eprintln/process::exit(2)pattern intoexit_with_usage.
✅ No new issues found.
@v0 or keep the SHA fresh with Dependabot | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

Fixes #1300
What do you want to change?
Add a
--last <N>option to the reports that group rows by a calendar period, counting in whatever unit the report already uses:Why?
Checking today's or this week's usage currently means working out the dates yourself and passing
--since. This keeps the command surface as it is — no new subcommands — and maps one option onto the day/week/month granularity each report already has, which is the shape suggested in #1300 and preferred over thetoday/this-weekcommands proposed in #1321.How?
ccusage-coregainslast_periods_since, which turns a unit plus a count into the compactYYYYMMDD--sincebound the loaders already understand. It takes "today" as an argument so it stays deterministic in tests, and reusesweek_startso the window lines up with the report's own weekly bucketing.--sinceright after parsing, using the report timezone for "today" and the command's own week start (Monday everywhere exceptccusage claude weekly, which honors-w). Nothing downstream changes.--lastis rejected onsession,blocks, andstatusline, which have no calendar period, and alongside--since,--until, or--sections, where the intended window would be ambiguous.-lused to be--localeuntil 8792ff5 removed that option, so--lastdeliberately leaves it unclaimed and a test records that.Covered by unit tests for the date math, the parse and rejection paths, and the resolution of each command shape, plus a manual check against a dated fixture (
daily --last 1→ today only,weekly --last 2→ the current and previous week,monthly --last 2→ this month and last).Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is enabled.Summary by cubic
Add
--last <N>to daily/weekly/monthly reports to show only the most recent N periods, auto-resolved to--since. Works on unified and per‑agent reports to make “today/this week/this month” easy.New Features
daily,weekly, andmonthlyfor unified and per‑agent commands; bareccusage --last Napplies to daily.--last Nto compactYYYYMMDD--sinceusing the report timezone; weekly windows align to the report’s week start (Monday by default;ccusage claude weeklyrespects-w).session,blocks, andstatusline, and when combined with--since,--until, or--sections; N must be a positive integer.last_periods_sinceinccusage-core, resolution logic in theccusageCLI, and updatedccusage-cliparser/help; guides and README document “Recent Periods” with examples; tests cover parsing, date math, and rejection paths.Refactors
-lshort alias (formerly--locale);--lasthas no short form, and a test locks in that-lremains unused.Written for commit 834e1d3. Summary will update on new commits.
Summary by CodeRabbit
New Features
--last <N>option for daily, weekly, and monthly reports.Documentation