Skip to content

Commit c788127

Browse files
github-actions[bot]erwindamsmaryoppippi
authored
fix(copilot): report session credits missing from modelMetrics (ccusage#1824)
Fixes ccusage#1823 Co-authored-by: Erwin Damsma <[email protected]> Co-authored-by: ryoppippi <[email protected]>
1 parent 05311f3 commit c788127

13 files changed

Lines changed: 913 additions & 86 deletions

‎docs/guide/copilot/index.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ pnpm dlx ccusage copilot --help
2424

2525
## Data Source
2626

27-
The CLI reads Copilot session-state shutdown events from `${COPILOT_HOME:-~/.copilot}/session-state/*/events.jsonl` by default. It also reads OpenTelemetry JSONL files recursively from `${COPILOT_HOME:-~/.copilot}/otel/**/*.jsonl` and includes the single explicit file pointed to by `COPILOT_OTEL_FILE_EXPORTER_PATH`. Set `COPILOT_HOME` to the Copilot data root when the default `~/.copilot` directory has been relocated.
27+
The CLI reads Copilot session-state shutdown and usage-checkpoint events from `${COPILOT_HOME:-~/.copilot}/session-state/*/events.jsonl` by default. It also reads OpenTelemetry JSONL files recursively from `${COPILOT_HOME:-~/.copilot}/otel/**/*.jsonl` and includes the single explicit file pointed to by `COPILOT_OTEL_FILE_EXPORTER_PATH`. Set `COPILOT_HOME` to the Copilot data root when the default `~/.copilot` directory has been relocated.
2828

2929
Session-state files do not require OpenTelemetry configuration. Shutdown usage is cumulative per canonical session/model pair, and resumed sessions emit another shutdown snapshot. ccusage reports the first snapshot as-is and each later snapshot as the difference from its predecessor, so usage follows the resume dates while totals stay unchanged. For date-bounded reports, snapshots before `--since` remain available as subtraction baselines, and snapshots after `--until` are excluded. When both sources contain the same session/model pair, the session-state usage is used and matching OpenTelemetry rows are suppressed only when their timestamps are at or before the latest raw shutdown visible through `--until`; rows emitted after that timestamp by a resumed session are retained. OpenTelemetry rows for other session/model pairs remain available.
3030

31-
For session-state, only `session.shutdown` events are used. For each `data.modelMetrics.<model>` entry, ccusage reads its `usage` fields and uses any positive `requests.count` for `messageCount`; each retained OpenTelemetry usage row contributes one message. `requests.cost` is ignored and costs continue to use ccusage's normal token pricing. In session-state data, `inputTokens` includes both cache buckets, so ccusage derives uncached input as `max(inputTokens - cacheReadTokens - cacheWriteTokens, 0)` before populating `inputTokens`; cache reads and cache writes are then reported separately. `reasoningTokens` is a subset of `outputTokens`, so it is not added again to output, total tokens, or cost. Copilot model IDs with `-1m` or `-1m-internal` suffixes, such as `claude-opus-4.6-1m`, are normalized to their priced model name for pricing and source deduplication.
31+
For session-state token usage, only `session.shutdown` events are used. For each `data.modelMetrics.<model>` entry, ccusage reads its `usage` fields and uses any positive `requests.count` for `messageCount`; each retained OpenTelemetry usage row contributes one message. `requests.cost` is ignored and costs continue to use ccusage's normal token pricing. In session-state data, `inputTokens` includes both cache buckets, so ccusage derives uncached input as `max(inputTokens - cacheReadTokens - cacheWriteTokens, 0)` before populating `inputTokens`; cache reads and cache writes are then reported separately. `reasoningTokens` is a subset of `outputTokens`, so it is not added again to output, total tokens, or cost. Copilot model IDs with `-1m` or `-1m-internal` suffixes, such as `claude-opus-4.6-1m`, are normalized to their priced model name for pricing and source deduplication.
3232

3333
For example, a `claude-opus-4.7` shutdown with `inputTokens=100`, `outputTokens=50`, `cacheReadTokens=10`, and `cacheWriteTokens=20` is reported as 70 input tokens, 50 output tokens, 20 cache-creation tokens, and 10 cache-read tokens. With the embedded Opus pricing, its calculated cost is exactly `$0.00173`.
3434

35+
Copilot also records the credits a session has used: a session-wide `totalNanoAiu` on every `session.shutdown` and on the `session.usage_checkpoint` events written during the session. One AIU is one GitHub AI credit, billed at $0.01. The per-model breakdown only covers the running Copilot process, so a resumed or restarted session can shut down with an empty or partial `modelMetrics` while its session total still includes everything. ccusage compares the session total with the per-model `totalNanoAiu` values reported so far, at every shutdown and at each checkpoint after a session's last shutdown, which covers sessions that are still open or ended without a shutdown. Credits that stay unexplained at every later snapshot are reported as a cost-only row for the `unknown` model with zero tokens, dated at the first snapshot from which they stay unexplained. A breakdown that a later shutdown catches up on is not counted twice. Sessions without checkpoints, which come from older Copilot versions that restart both totals on every resume, sessions with a shutdown that lacks either credit total, and sessions that also have OpenTelemetry rows keep token-only reporting. These rows carry a precomputed cost, so `--mode calculate` reports them as $0.
36+
3537
Enable these variables before starting or resuming a Copilot CLI session when you want OTel data. Sessions that ran without OpenTelemetry file export remain readable from their session-state files.
3638

3739
```bash
@@ -68,6 +70,7 @@ These views support `--json` for structured output, `--compact` for narrow termi
6870
- **Input tokens** - session-state `inputTokens` is normalized to uncached input after subtracting cache reads and cache writes.
6971
- **Reasoning tokens** - session-state reasoning tokens are already included in output tokens; OpenTelemetry reasoning is included when total usage metadata shows it is separate.
7072
- **Pricing** - costs are calculated from LiteLLM pricing data using the normalized model name; both `-1m` and `-1m-internal` suffixes are removed.
73+
- **Unattributed credits** - session credits missing from the per-model breakdown are reported as `unknown` rows at $0.01 per AI credit.
7174

7275
## Environment Variables
7376

‎rust/adapters/copilot/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ separate. Session-state reasoning tokens are already included in output tokens;
3333
reasoning is included when total usage metadata shows it is separate. Internal model suffixes such
3434
as `-1m` and `-1m-internal` are removed before pricing and source deduplication.
3535

36+
`modelMetrics` only covers the running Copilot process, while the session-wide `totalNanoAiu`
37+
on `session.shutdown` and `session.usage_checkpoint` covers the whole session (1 AIU is one
38+
GitHub AI credit, $0.01). At every shutdown, and at checkpoints after the last one, the session total
39+
minus the per-model `totalNanoAiu` reported so far is unexplained; what stays unexplained at every
40+
later snapshot becomes cost-only `unknown` entries, dated where it first stays. Sessions without checkpoints
41+
(older Copilot versions restart both totals on every resume), with a shutdown missing either total,
42+
or with OpenTelemetry rows get no such entries.
43+
3644
Reads plain files through `ccusage-adapter-common`, which handles walking, size-balanced
3745
chunking, and ordered parallel reads.
3846

0 commit comments

Comments
 (0)