Skip to content

Cost Estimation & Quota Attribution ​

Token accounting in multi-provider agent harnesses is notoriously misleading: tools routinely price Claude Pro or ChatGPT Plus subscription turns at API list prices (inventing fake dollar bills), silently report $0.00 for unrecognized models, or lump cheap cache reads together with expensive cache writes.

Kompile takes a strict accounting stance: separate cash spend from subscription quotas, never fabricate $0 for unknown or unpriced usage, price cache writes by TTL, and fit real quota consumption rates from vendor readings.


1. Three Strict Billing Buckets (UsageCostSummary.Billing) ​

Every journaled model request is classified into exactly one billing bucket before any dollar math runs:

BucketWhen it appliesDollar contributionWhere it appears
PER_TOKENAn API key with no vendor quota window (standard pay-as-you-go API billing)Priced in USD via ModelPriceCatalog (or provider-reported charge)Dollar totals in /cost and kompile usage cost
PLANSubscription logins (Claude Code, ChatGPT/Codex), coding-plan keys (e.g. Z.AI, OpenCode Go), or any key whose vendor reports quota windows$0 to USD totals (counted separately under onPlan)Quota window share in /cost quota and kompile usage cost --quota
UNKNOWNRequests with no recorded account metadata (e.g. legacy logs before account tracking)$0 to USD totals (counted separately under accountUnknown)Reported explicitly alongside why: diagnostics

Within PER_TOKEN, any request whose (provider, model) route matches no pricing rule is counted as unpriced — never silently added as $0.00.


2. Data-Driven Pricing (ModelPriceCatalog) ​

Prices are stored as data, never hardcoded in Java:

  • Shipped catalog: first-party vendor rates bundled in model-prices.json.
  • User overrides: ~/.kompile/config/model-prices.json (same schema). User rules are evaluated first, then shipped rules; first match wins — so you can add custom/private models, enterprise negotiated discounts, or day-one new models without waiting for a Kompile release.

Four-Component Request Breakdown ​

For every priced PER_TOKEN request, Kompile splits cost into four explicit components (rates in USD per million tokens):

ComponentLabel in reportsWhat drives it
Inputuncached inputFresh prompt/tool tokens not served from prompt cache
Cache writenew context writtenPrompt cache creation — priced separately for cacheWrite5m vs cacheWrite1h TTLs. When a vendor reports cache writes without specifying TTL, the catalog's defaultCacheWriteTtl applies and the cost is transparently flagged ttlAssumed: true.
Cache readcarried context re-readCached prefix tokens re-read across turns (typically ~0.1× input rate)
OutputoutputGenerated completion + thinking tokens

Additional pricing accuracy safeguards:

  • Long-context tiers: rules support longContextThreshold and alternate longContext rates when a single request crosses a vendor's high-context pricing cliff (marked longContext: true).
  • Vendor reconciliation: when a harness or provider also reports its own charge (e.g. OpenRouter, OpenCode, or vendor-reported totals), Kompile tracks vendorCompared, vendorUsd, and kompileComparedUsd over the exact same subset of requests so you can audit table accuracy side-by-side.

3. Subscription Quota Window Attribution (UsageQuotaReport & QuotaRates) ​

For PLAN accounts — Claude Code logins, ChatGPT/Codex logins, Z.AI coding-plan keys, OpenCode Go keys, and OpenRouter key budgets — dollars aren't the constraint; rolling quota windows (e.g. 5-hour and 7-day caps) are.

Kompile attributes quota consumption back to specific sessions, models, and subagents in three steps:

  1. Per-message quota sampling (QuotaSampler / QuotaReadingJournal) — while you chat on a quota-bearing route, Kompile records periodic quota utilization readings alongside the token usage journal.
  2. Non-Negative Least Squares fitting (QuotaRates) — given oldest-first quota readings and the token deltas between them, Kompile fits non-negative per-window consumption rates for each account and model so it can estimate what share of each quota window every session, main loop, judge, or subagent consumed.
  3. Plan-allowance fallback (PlanAllowances) — before enough live readings exist for an NNLS fit, windows covered by known vendor formulas fall back cleanly to PlanAllowances rules instead of showing empty data.

Where Claude Code session logs omit an explicit login identifier, ClaudeAccountBackfill infers the account from Claude Code's login history or sibling requests in the same chat — and explicitly discloses how many requests were inferred (why: / inferred: footnotes in the report).


4. Inspecting Costs, Quotas & Judge Overhead ​

text
# Inside kompile chat
/cost                         # last 24h breakdown (requests, tokens, USD, on-plan, unknown)
/cost 7d models               # 7-day spend grouped by model
/cost session subagents       # this session's subagent spend vs. main agent
/cost quota                   # fitted quota-window shares across subscription/plan routes
/usage                        # live vendor account credits, quota windows, and reset times
/judge cost                   # dedicated token & cost breakdown of judge overhead alone
/subagent-breakdown <id>      # token breakdown for one subagent (startup, skills, tools, results)

From the shell CLI (and for automation / dashboards):

bash
kompile usage cost --window 24h
kompile usage cost --quota
kompile usage prices          # inspect active ModelPriceCatalog rules (user + shipped)
kompile usage telemetry       # configure OTLP usage export (UsageOtlpExporter / OtlpUsageReceiver)

Agents can inspect the same cost summaries programmatically through the cost MCP tool.

Released under the Apache-2.0 License.