You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5f31039
Browse filesBrowse the repository at this point in the historyBrowse files
fix(all): count only in-window Claude usage in unified session reports (ccusage#2052)
* fix(all): count only in-window Claude usage in unified session reports
The unified session report built each Claude session from every entry
and then kept it only when its last activity fell inside the window. A
session that continued past `--until` vanished with its in-window usage,
and a session started before `--since` brought its earlier usage along,
so `ccusage session` disagreed with `ccusage claude session` and the
focused reports no longer added up to the unified one.
Claude entries are now filtered by date before sessions are built, like
`ccusage claude session` and the other unified sources. OpenCode
`claude-agent` rows still match by whole session, so a turn crossing
midnight is counted once, but only sessions with in-window usage count
and a row must fall inside the Claude source's date range.
`DedupedEntries` keeps the survivor index of each merged copy, so a
copy's session counts on the day of the kept entry, as in the daily
loader.
Fixesccusage#2016
* docs(all): describe in-window Claude session totals in unified reports
Refs ccusage#2016
* test(all): assert which source keeps a session that runs past --until
The regression test only checked the 100-token total. The old logic also
reached 100: it dropped the Claude session whose last activity fell after
--until and kept the OpenCode claude-agent copy of the same turn instead.
The test now asserts each row's source and session, so it fails on the old
behavior (an OpenCode `ses-agent` row) and passes only when the Claude
session `claude-native-a` keeps the turn.
Refs ccusage#2016
* test(claude): cover cross-session copy attribution on a different day
The unified tests used the same date for a turn and its copy in another
session, so they could not tell whether a copy's session is dated by the
survivor or skipped when the survivor is not counted. A direct test of
DedupedEntries::main_thread_session_days now repeats a turn a day earlier
in another session and asserts that the copy's session lands on the
survivor's day and drops out when is_counted rejects the survivor.
Refs ccusage#2016
---------
Co-authored-by: igorovh <[email protected]>
Copy file name to clipboardExpand all lines: docs/guide/opencode/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -83,7 +83,7 @@ The `claude-agent` provider runs Claude Code under the hood, so each turn is als
83
83
ccusage matches OpenCode rows to Claude usage by Claude Code session, not by individual turn:
84
84
85
85
-**Daily, weekly, and monthly reports** skip an OpenCode `claude-agent` row when the Claude source counted usage from the same Claude Code session on the row's date, in the Claude source's timezone. Rows still count when the Claude Code log is gone, holds no usage for that day, holds only subagent usage, or falls outside the Claude source's date range.
86
-
-**Session reports**match whole Claude Code sessions, because a Claude session row holds all of its usage, including days outside the Claude source's date range. Resumed and forked transcripts repeat earlier turns, and ccusage keeps one copy of each repeated turn. The OpenCode rows of a session whose own main-thread turns survive are skipped when every transcript holding them is included. The rows of a session whose turns all survive under other sessions are skipped only when every transcript holding those copies is included. When all transcripts are included, which one keeps a repeated turn does not affect the total. When the Claude window includes only some of them, a repeated turn can be left out, if a session's rows are skipped while its kept copy is in an excluded transcript, or counted twice, if the turns that decide whether a session's rows are skipped (its own surviving main-thread turns, or its kept copies when it has none) span included and excluded transcripts, since its rows are then kept.
86
+
-**Session reports**count only the usage of each Claude Code session inside the Claude source's date range. They skip an OpenCode `claude-agent` row when the Claude source counted main-thread usage from the same session in that range and the row's date is in the range too. Resumed and forked transcripts repeat earlier turns, and ccusage keeps one copy of each repeated turn; the session of a dropped copy counts on the day of the kept one, as in daily reports.
87
87
88
88
Because OpenCode dates a turn by when it started and Claude Code dates each model call, a turn that runs across midnight can be off in the daily family. It is counted twice when Claude has no usage from that session on the day the turn started. With `--until` on that day, the calls Claude logs after midnight are left out, even though OpenCode dates the whole turn on the earlier day.
ccusage claude session --since $(date -d '7 days ago' +%Y%m%d)
140
140
```
141
141
142
-
For `ccusage claude session`, date filters are applied before session totals are calculated, so token and cost totals represent only the entries inside the selected window. Both bounds are inclusive, and `--timezone` determines the local date used for filtering.
142
+
For `ccusage claude session` and the Claude rows of `ccusage session`, date filters are applied before session totals are calculated, so token and cost totals represent only the entries inside the selected window. A session that continues past `--until` still shows its usage from inside the window. Both bounds are inclusive, and `--timezone` determines the local date used for filtering.
Copy file name to clipboardExpand all lines: rust/adapters/opencode/src/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ SQLite databases are the primary source. Legacy JSON messages under `storage/mes
16
16
17
17
OpenCode v2 forks copy the parent's `session_message` rows with new ids but the same `seq`. When `session_v2.fork_session_id` and `fork_boundary` resolve against the parent's rows, fork rows at or below the last copied `seq` (the boundary for `through`, the parent's last row before it for `before`) are skipped as inherited history. The fork's `session_v2` counters start at zero and hold only fork-local usage, so they remain a valid session fallback. Unresolvable boundaries and pre-`seq` schemas keep every row.
18
18
19
-
The Claude Agent provider (`claude-agent`) runs Claude Code, which also writes its own transcript. Its v2 rows record that Claude Code session as `content[].native.sessionID`, which `load_entries_with_native_sessions` returns with each entry. Unified daily-family reports drop a row when the Claude source counted main-thread usage of that session on the row's date, in the Claude source's timezone and date range; session reports match whole sessions, since a Claude session row holds all of its usage, using the session ID the transcript records rather than its file name. A resumed or forked transcript repeats earlier turns, sometimes under the original session ID, and Claude's dedup keeps one copy of each. `should_replace_deduped_entry` prefers main-thread copies, then more tokens, then a copy with speed metadata over one without (the daily loader's `should_replace_deduped_daily_entry` compares cost before speed); only among copies equal on all of these does the first read win, with files sorted by path and named by UUID, so which transcript keeps a turn is effectively arbitrary; when all of them are reported, the choice does not affect the total. A session with surviving main-thread usage under its own recorded ID counts as included when the report includes every transcript holding that usage; copies it lost to other sessions are not consulted. A session with no surviving main-thread usage of its own counts as included when every transcript holding its kept copies is included. When a Claude date window includes only some of the transcripts involved, a repeated turn can go either way: a skipped session's rows for it are dropped although its kept copy is in an excluded transcript, or a session keeps its rows when the usage that decides it (its own surviving main-thread usage, or its kept copies when it has none) spans included and excluded transcripts, so the turns Claude counts in the included transcript are counted twice. The usage is then priced by the resolved model ID rather than the `opus`/`sonnet` alias OpenCode stores. The match is per session or session day, not per turn: `native.messageID` is the transcript line `uuid`, and one OpenCode row sums several API calls, so rows do not map one-to-one to Claude entries. This relies on the transcript holding every main-thread turn OpenCode records for that session; it also holds turns OpenCode drops, such as interrupted steps saved with `tokens: null`. A row is dated by its turn's start while Claude dates each call, so in daily-family reports a turn that crosses midnight in the Claude source's timezone is counted by both sources when Claude has no usage of that session on the earlier day, and with `--until` on that day the calls Claude logs after midnight are left out although OpenCode dates the whole turn on the earlier day. Only v2 `session_message` rows can carry the link: OpenCode added the provider after moving to SQLite v2, so legacy JSON messages and the v1 `message` table hold no `claude-agent` rows. `ccusage opencode` runs through `ccusage_adapter_all::run_opencode`, which matches by session day in every report kind, as `ccusage claude session` counts only in-window days, so focused reports add up to the unified daily one; it reads Claude only when a loaded row carries a native session, treats an unreadable Claude source as counting nothing, and `--include-claude-agent` keeps every row. `opencode::run` keeps every row.
19
+
The Claude Agent provider (`claude-agent`) runs Claude Code, which also writes its own transcript. Its v2 rows record that Claude Code session as `content[].native.sessionID`, which `load_entries_with_native_sessions` returns with each entry. Unified daily-family reports drop a row when the Claude source counted main-thread usage of that session on the row's date, in the Claude source's timezone and date range; session reports drop a row when the Claude source counted in-window main-thread usage of that session and the row's date is in the Claude source's date range, since a Claude session row holds only its in-window usage. Both match the session ID the transcript records rather than its file name. A resumed or forked transcript repeats earlier turns, sometimes under the original session ID, and Claude's dedup keeps one copy of each; as in the daily loader, the session of a dropped copy counts on the day of the kept one. The usage is then priced by the resolved model ID rather than the `opus`/`sonnet` alias OpenCode stores. The match is per session or session day, not per turn: `native.messageID` is the transcript line `uuid`, and one OpenCode row sums several API calls, so rows do not map one-to-one to Claude entries. This relies on the transcript holding every main-thread turn OpenCode records for that session; it also holds turns OpenCode drops, such as interrupted steps saved with `tokens: null`. A row is dated by its turn's start while Claude dates each call, so in daily-family reports a turn that crosses midnight in the Claude source's timezone is counted by both sources when Claude has no usage of that session on the earlier day, and with `--until` on that day the calls Claude logs after midnight are left out although OpenCode dates the whole turn on the earlier day. Only v2 `session_message` rows can carry the link: OpenCode added the provider after moving to SQLite v2, so legacy JSON messages and the v1 `message` table hold no `claude-agent` rows. `ccusage opencode` runs through `ccusage_adapter_all::run_opencode`, which matches by session day in every report kind, as `ccusage claude session` counts only in-window days, so focused reports add up to the unified daily one; it reads Claude only when a loaded row carries a native session, treats an unreadable Claude source as counting nothing, and `--include-claude-agent` keeps every row. `opencode::run` keeps every row.
0 commit comments