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 c788127
Browse filesBrowse the repository at this point in the historyBrowse files
authored
fix(copilot): report session credits missing from modelMetrics (ccusage#1824)
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.
28
28
29
29
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.
30
30
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.
32
32
33
33
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`.
34
34
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
+
35
37
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.
36
38
37
39
```bash
@@ -68,6 +70,7 @@ These views support `--json` for structured output, `--compact` for narrow termi
68
70
-**Input tokens** - session-state `inputTokens` is normalized to uncached input after subtracting cache reads and cache writes.
69
71
-**Reasoning tokens** - session-state reasoning tokens are already included in output tokens; OpenTelemetry reasoning is included when total usage metadata shows it is separate.
70
72
-**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.
0 commit comments