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 1322e4c
Browse filesBrowse the repository at this point in the historyBrowse files
feat(adapter/copilot): add --mode credits for GitHub AI Credits
After June 1, 2026 GitHub Copilot bills usage in AI Credits at $0.01/credit
instead of premium-request multipliers. The Copilot CLI already reports
`totalNanoAiu` (nano AI Units = 10^-9 credits) per `session.shutdown` event.
This adds `--mode credits` as a fourth global cost mode that bills directly
from the credit counts Copilot ships, bypassing LiteLLM token pricing. Newer
Copilot model variants that aren't yet in the LiteLLM dataset now report
correct cost in credits mode.
Cross-crate scope (per plan ccusage#1174)
* `CostMode::Credits` added to `ccusage-cli/src/types.rs:171`, parsed by
`parse_cost_mode` and surfaced in `--mode` choices alongside the
existing trio.
* `ConfigCostMode::Credits` added to `config_schema.rs:466` + `From`
impl at `config.rs:459-464`. Regenerated `apps/ccusage/config-schema.json`
(snapshot accepted).
* `cost.rs::calculate_cost_for_usage` has a new `Credits` arm that falls
back to `Auto` semantics, used by non-Copilot adapters; they have no
credit channel so token pricing remains the right answer when a user
passes `--mode credits` to a non-Copilot report.
* `gemini`, `kilo`, `kimi` exhaustive `match mode` sites updated to
the same fall-back behavior.
Copilot loader
* Per-model `totalNanoAiu` and aggregate `data.totalNanoAiu` parsed from
session-state shutdowns and surfaced as `CopilotUsageEntry.nano_aiu`.
* `LoadedEntry.credits` populated from `nano_aiu / 1e9` regardless of
selected mode, so JSON output always carries the raw credit count.
* In `Credits` mode, `LoadedEntry.cost = credits × $0.01` and the
Copilot-local `missing_pricing_model_for_usage` call is suppressed -
api-equivalent pricing is irrelevant in this mode. The suppression is
Copilot-local rather than global in `cost.rs` to avoid hiding legitimate
missing-pricing warnings for other adapters whose `--mode credits`
falls back to `Auto`.
Empirical fixes (plan rounds 12-15)
* Skip rule relaxed: zero-token zero-request rows are now kept when
`metrics.totalNanoAiu > 0`. Real local data has 4 such credit-only rows
in `1135875b-...` and `76dc438a-...` (~$5.30 total) that would
otherwise be silently dropped.
* Content-based dedup key (`credit-shutdown:{session}:{model}:{naiu}`)
for credit-only rows. The 4 real-data rows are actually 2 unique
snapshots x 2 duplicates each (identical event-IDs would have produced
$2.65 of inflation under the plain `shutdown:{event.id}:{model}` key);
the content-based key collapses exact duplicates via the loader-side
HashMap dedup added in Piece A.
* Synthetic aggregate-only credit entry (forward-compat guard) emits when
`data.totalNanoAiu` is non-null but no per-model row carries it. Not
observed in 200-file local corpus today; suppressed when per-model AIU
is also present so we never double-count.
Tests
* 13 new parser tests cover `totalNanoAiu` parsing, credit-only row
retention, content-based dedup, token-bearing event-id stability,
aggregate-only synthetic entries, and pre-AIU legacy schema returning
`None`.
* 5 new loader tests cover end-to-end `--mode credits` cost math,
credit-only row carrying $1.54 from real-data sample, duplicate-row
collapse to one entry, `Auto` mode still using token pricing when AIU
is present, and aggregate-only synthetic entry billing.
Docs
* `docs/guide/cost-modes.md`: new `credits` section with how-it-works,
example output, and explicit non-Copilot fallback semantics.
* `docs/guide/cli-options.md`: cross-link to cost-modes for the new mode.
* `docs/guide/copilot/index.md`: new 'Pricing modes' section explaining
the credit math and skip-rule relaxation.
Smoke test on real local data (73 days, 7.56B tokens, 23 unique models):
Credits-mode: $5,003.02 total, 500,302.25 credits aggregated.
Auto-mode (Piece A baseline): $6,577.77 api-equivalent.
Pieces A (events.jsonl reader) and B (credits mode) are now both shipped.
Piece C (`--mode api-equiv` alias) remains optional per the plan.
Refs ccusage#1174
Co-authored-by: Copilot <[email protected]>
Copy file name to clipboardExpand all lines: docs/guide/copilot/index.md
+18-1Lines changed: 18 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,12 +66,29 @@ Without these variables, ccusage relies on the session-state source — which is
66
66
67
67
These views support `--json` for structured output, `--compact` for narrow terminals, and `--offline` for cached pricing data.
68
68
69
+
## Pricing modes
70
+
71
+
`ccusage copilot` supports the standard cost modes (`auto`, `calculate`, `display`) plus a Copilot-specific `credits` mode:
72
+
73
+
```bash
74
+
# Default: API-equivalent cost from LiteLLM token pricing.
75
+
ccusage copilot daily
76
+
77
+
# Credits mode: bill from GitHub Copilot's AI Credit counts ($0.01/credit).
78
+
ccusage copilot daily --mode credits
79
+
```
80
+
81
+
In credits mode, ccusage reads `totalNanoAiu` from each `session.shutdown` event and converts directly to USD (`credits × $0.01`). No LiteLLM token pricing lookup happens, so newer Copilot model variants that aren't yet in the LiteLLM dataset still report correct cost. The `credits` field is also exposed in `--json` output regardless of the selected mode, so you can run with `--mode auto` and still see the raw credit counts.
82
+
83
+
Credit-only `session.shutdown` rows (zero tokens, zero requests, non-null `totalNanoAiu`) are kept even when the default skip rule would otherwise discard them. Identical duplicate credit-only snapshots within the same session are collapsed via content-based deduplication.
84
+
69
85
## What Gets Calculated
70
86
71
87
-**Token usage** — for session-state, `inputTokens` in the source schema already includes both cache reads and cache writes, so ccusage subtracts both to recover the "fresh" input bucket. For OTel, chat spans are preferred with inference logs and agent-turn logs as fallbacks.
72
88
-**Cache tokens** — cache read and cache creation token attributes are counted from `cacheReadTokens` / `cacheWriteTokens` (session-state) or the corresponding `gen_ai.usage.cache_*` attributes (OTel).
73
89
-**Reasoning tokens** — provider-dependent. For OpenAI (`gpt-*`) and Anthropic (`claude-*`) models reasoning is already inside `outputTokens` and is not double-counted. For Google (`gemini-*`) models reasoning is reported separately and is summed into the output cost bucket.
74
-
-**Pricing** — costs are calculated from LiteLLM pricing data using a normalized form of the model name. The raw name (e.g. `claude-opus-4.7-1m-internal`) is preserved in JSON, the table's `Models` column, and `modelsUsed`.
90
+
-**Credits** — when present, `totalNanoAiu` is divided by 10⁹ to get the AI Credit count (`1 credit = $0.01`). Always surfaced in JSON; becomes the primary cost basis under `--mode credits`.
91
+
-**Pricing** — for non-credits modes, costs are calculated from LiteLLM pricing data using a normalized form of the model name. The raw name (e.g. `claude-opus-4.7-1m-internal`) is preserved in JSON, the table's `Models` column, and `modelsUsed`.
Copy file name to clipboardExpand all lines: docs/guide/cost-modes.md
+54-1Lines changed: 54 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Cost Modes
2
2
3
-
ccusage supports three different cost calculation modes to handle various scenarios and data sources. Understanding these modes helps you get the most accurate cost estimates for your usage analysis.
3
+
ccusage supports four cost calculation modes to handle various scenarios and data sources. Understanding these modes helps you get the most accurate cost estimates for your usage analysis.
4
4
5
5
## Overview
6
6
@@ -9,6 +9,7 @@ Claude Code stores usage data in JSONL files with both token counts and pre-calc
9
9
-**`auto`** - Smart mode using the best available data
10
10
-**`calculate`** - Always calculate from token counts
11
11
-**`display`** - Only show pre-calculated costs
12
+
-**`credits`** - Bill from GitHub Copilot's AI Credit counts at `1 credit = $0.01` (Copilot-specific; other adapters fall back to `auto`)
0 commit comments