Skip to content

Add OpenRouter as a billing route and carry metered/subscription on each call: read the endpoint from the provider's own column, not the model id (with a list of routes to follow) #1450

Description

@ulises-c

The problem, in your words

The question behind this series (#1449): with Claude Code and Codex on fixed-fee subscriptions and API usage on top (Bedrock, OpenRouter), how much metered spend am I incurring this month? The route a call took is what decides that — Bedrock and OpenRouter are always metered; a Claude Max or ChatGPT session is covered by the flat fee — so the route has to be a first-class fact about the call, and it has to carry its billing mode.

#1448 makes the billing route visible for one endpoint, Bedrock, by recognising the id shape tools write for it (anthropic.claude-…-v1:0). That covers Bedrock because Bedrock renames the model. Most other billing endpoints do not: a call billed through OpenRouter is recorded as the plain vendor id (claude-sonnet-4-5, or anthropic/claude-sonnet-4.5), so no id-shape rule can tell it from a direct Anthropic call, and its spend merges into the direct row.

The route is on disk, just in a different column than the model:

Tool Where the endpoint is recorded Example
Hermes sessions.billing_provider (+ billing_base_url) in state.db openrouter, anthropic, bedrock, openai-codex, bedrock-mantle — all seen on one machine this week; src/providers/hermes.ts already SELECTs the column and drops it
OpenCode / ZCode session model JSON { providerID, id }; sqlite-session-parser.ts:58 folds it into providerID/id and the known-namespace strip then discards it openrouter/anthropic/claude-sonnet-4-5
Zerostack OpenRouter-prefixed model ids (zerostack.ts:128) deepseek/deepseek-v4-pro
Mux provider:model prefix, stripped at mux.ts:199 openrouter:…
Cline / Roo / Kilo apiProvider in the task's API configuration (not read today) openrouter, bedrock, vertex, anthropic

So today two things are lost: the OpenRouter route entirely, and any Bedrock/Vertex route on a tool that records the endpoint in a column rather than the id.

What you tried

Proposed change

Give the route a second source — the provider's own endpoint column — feeding the same ModelRoute that #1448 introduced, with OpenRouter as the first route that only that source can supply.

  1. Registry of routes, replacing the Bedrock-only branch in getModelRoute: { id, label, billing: 'metered' | 'subscription', detect?(modelId), fromProviderField?(value) }. bedrock keeps its id-shape detector, trimmed to what is on disk — <vendor>.<model> with an optional -vN:M version, vendors anthropic and openai (the two with coding sessions); ARN / bedrock/ / :300k spellings and the wider vendor table from the first cut of feat(models): carry the billing route on the call; key every model row on it #1448 are dropped until a provider is shown to write them; openrouter has none and is recognised from the provider field (openrouter, openrouter/…, openrouter:…, base URL openrouter.ai). Both are metered. Subscription doors get entries too — openai-codex (ChatGPT plan; Hermes writes it as billing_provider), claude-max / anthropic-oauth where a harness can tell OAuth from an API key — as subscription. A provider that already records the mode per session (Hermes cost_status = included, Copilot's rated/unrated credits) overrides the route's default. Display convention: Sonnet 4.5 (OpenRouter), Sonnet 4.5 (Bedrock), Sonnet 4.5 for direct.
  2. Carry the field. ParsedProviderCall.route?: string and billing?: 'metered' | 'subscription' (resolved from the route's default unless the provider states it). Providers set it where they have it: Hermes from billing_provider, OpenCode/ZCode from providerID before the namespace strip, Mux from the colon prefix before it is dropped, Cline-family from apiProvider. Where absent, getModelRoute(model)?.id fills it. parser.ts keys modelBreakdown on route ? "<name> (<label>)" : name, so the column-sourced route and the id-sourced route land in the same row shape. Persist on CachedCall (bump CACHE_VERSION). The daily cache stores the display key, not raw ids (maintainer correction on feat(models): carry the billing route on the call; key every model row on it #1448): day.models is keyed by the string getShortModelName produced at aggregation time, and feat(models): carry the billing route on the call; key every model row on it #1448 needed no bump only because that function was idempotent on the old key. Once the row key becomes (name, route), finalized days must re-derive — DAILY_CACHE_VERSION bumps, and the migration ladder at the top of daily-cache.ts gets its entry. Every surface must key rows the same way: parser.ts modelBreakdown, usage-aggregator mergeDayModelsByDisplayName, menubar-json buildTopModels, model-breakdown aggregateModelTotals, and models-report (which today keys on resolveCanonicalModelId — the mismatch that produced two same-label rows in feat(models): carry the billing route on the call; key every model row on it #1448). One key function, called from all of them. This is the store-at-parse / apply-at-read pattern Cost modes: reconcile displayed cost with the source's own reported cost (auto/calculate/display) #890 lays out for reported-vs-computed cost (both are provenance about a call the caches must carry, not recompute).
  3. Pricing stays on the raw id. LiteLLM already carries openrouter/… (103 rows), bedrock/…, vertex_ai/… (115), azure/… (187). A route never changes what a call costs; it changes which row the cost lands on.

Routes others can add after this lands, each a registry entry plus the provider field that names it (id-shape detector only where the endpoint renames the model):

Route Id shape? Provider field Notes
openrouter no Hermes billing_provider, OpenCode providerID, Cline apiProvider, Mux prefix this issue
vertex yes: claude-…@20251001, vertex_ai/… Cline apiProvider = vertex @date is stripped today and merges with direct — docs/providers/claude.md
azure / azure-ai yes: azure/…, azure_ai/… deployment names Cline apiProvider deployment names are user-chosen; alias support matters
bedrock-mantle partly (bedrock-mantle.<region>.api.aws) Hermes billing_provider = bedrock-mantle Bedrock's OpenAI-compatible endpoint; seen on disk already
openai-codex (ChatGPT subscription) vs openai (API) no Hermes billing_provider (60 sessions on one machine, all cost_status = included), Codex auth mode the subscription side of the split; without it "metered" cannot be computed as a share
claude-max / Anthropic OAuth vs API key no Claude Code transcripts carry no auth marker; Hermes billing_provider = anthropic covers both the biggest gap for the subscription side — may need codeburn plan as the tie-break
copilot, cline-pass, kimi-for-coding, … some (cp/, cline-pass/ prefixes already peeled in models.ts) provider-specific today these peel to the bare model and lose the door
ollama / local yes (:tag) — already handled as "free"; would just gain a label

Related: #1241's rawModels already exposes which raw ids fed a display row; once route exists, a row's routes can be listed the same way for consumers that want the merged view. #1420 (unpriced usage hidden) benefits too — a route-tagged unpriced row says which account is missing a price.

Not in scope: filtering/drill-through and the desktop / menubar / web surfaces that show the split — #1451. Pricing changes — none needed.

Depends on #1448 (ModelRoute, getModelRoute).

Activity

  1. changed the title [-]Filter and drill into spend by billing route (Bedrock first, then OpenRouter via Hermes billing_provider)[/-] [+]Add OpenRouter as a billing route: read the endpoint from the provider's own column, not the model id (and a list of routes to follow)[/+] on Sep 15, 2026
  2. changed the title [-]Add OpenRouter as a billing route: read the endpoint from the provider's own column, not the model id (and a list of routes to follow)[/-] [+]Add OpenRouter as a billing route and carry metered/subscription on each call: read the endpoint from the provider's own column, not the model id (with a list of routes to follow)[/+] on Sep 15, 2026
  3. ulises-c commented on Sep 16, 2026

    @ulises-c
    ContributorAuthor

    Plan before code (over the 300-line first-contributor threshold, per CONTRIBUTING). Findings from the checkout that revise the text above:

    1. Two persisted key shapes, not one. day-aggregator.ts:208 writes day.models[call.model] — the raw id (confirmed in a live daily-cache.v32.json: anthropic.claude-haiku-4-5-20251001-v1:0, openai.gpt-5.6-luna). The display-keyed shape is SessionSummary.modelBreakdown (parser.ts:1789), rolled up by usage-aggregator.ts:137,1026; mergeDayModelsByDisplayName (fix(models): group Trend rows and the models command by the same display name #1241) reconciles the two at read time. A route re-derivable from the raw id needs no daily bump; a route sourced from a provider column (Hermes billing_provider) has no home in day.models and does — so DAILY_CACHE_VERSION bumps for the case that matters, exactly as the feat(models): carry the billing route on the call; key every model row on it #1448 review warned.
    2. Twelve row-key sites, not five: parser.ts:1789 (+ the Devin raw-id exception repeated in session-output.ts:18, model-efficiency.ts:23), usage-aggregator.ts:323,1747, menubar-json.ts:505, model-breakdown.ts:26, models-report.ts:172,190 (canonical id — the odd one), overview.ts:315,384, sessions-report.ts:926, audit-report.ts:119, telemetry-snapshot.ts:146, compare-cohorts.ts:309,399, live-sessions.ts:184. One modelRowKey(model, route?) in models.ts, called from all of them.
    3. Field plumbing follows nanoAiu: ParsedProviderCall → ParsedApiCall → CachedCall (validator at session-cache.ts:749, four spread sites in parser.ts). Cached rows re-parse via a PROVIDER_PARSE_VERSIONS suffix on hermes, not a CACHE_VERSION bump — correcting item 2 above; shard layout is untouched.
    4. Hermes already computes the billing mode as costBasis (hermes.ts:466-497: included / actual / estimated / calculated) and drops it; billing_provider is SELECTed and dropped. Both are one-line forwards. On one machine: openai-codex 60 sessions all included; anthropic 36 all unknown — Claude Max vs Anthropic API is not recoverable from Hermes data, so that split needs codeburn plan as the tie-break, later.
    5. No OpenRouter sessions on disk here (no key in Hermes, OpenCode not installed). The mechanism lands with a Hermes fixture carrying billing_provider = openrouter; the Bedrock route and the bedrock / openai-codex / anthropic column values are what real data proves. Labelled as such in the PR.

    Slice I am implementing now:

    • models.ts: route registry (bedrock id-shape detector trimmed to <vendor>.<model>[-vN:M], vendors anthropic / openai; openrouter, openai-codex, anthropic, bedrock-mantle as column-only entries, each with a billing default) and modelRowKey. getShortModelName unchanged.
    • route? / billing? on the three call types, validator, spread sites, hermes parse-version suffix.
    • hermes.ts: billing_provider → route; costBasis === 'included' → billing: 'subscription'. Id shape fills the route where the column is absent (Claude Code).
    • All twelve sites on modelRowKey; day.models keyed on it too; DAILY_CACHE_VERSION 32 → 33 with a ladder entry.
    • Tests: the three-seeded-Claude-sessions case asserting identical rows across models, menubar payload and overview (the test feat(models): carry the billing route on the call; key every model row on it #1448 lacked); Hermes fixture with billing_provider values; direct ids byte-identical.

    Out of this slice: --route / --billing filters, payload fields, GUI (#1451); Vertex / Azure.

  4. ulises-c commented on Sep 16, 2026

    @ulises-c
    ContributorAuthor

    Implementation: #1448, reworked in place after review. Real-data proof for Bedrock via Claude Code and Hermes; OpenRouter exercised by fixture only — no sessions on this machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions