#1148

usage: record the provider-reported model on usage events when a router resolves an alias

OpenFabioLeitaoopened 1mo ago

Ideas discussion

https://github.com/Twigpine/zero/discussions/1119 (Ideas, opened 2026-10-05, no replies yet). I am filing this tracking issue so the proposal has a place to be reviewed. I will not open a PR before this issue is reviewed and marked issue-approved, as CONTRIBUTING.md asks.

Problem

With OpenAI-compatible routers and aliases (OpenRouter auto, LiteLLM aliases, local gateways), the model Zero requested is often not the model that served the request. The provider says which one did: the chat stream carries model, and Codex Responses carries response.model. Zero drops that value, so zero usage report and the session log can only say "requested auto", never which model actually answered or how usage split across models over time.

Requested behavior

  • Decode the provider-reported model into Usage.ResolvedModel when the stream provides it (OpenAI-compatible chunks and Codex response.model). When usage arrives across several chunks, the last non-empty value wins.
  • Persist it as an optional resolvedModel key on usage events, through the existing single writer usage.EventUsagePayload. It is omitted when empty, so older readers and existing payloads are unaffected.
  • No change to pricing or to the existing model key. Cost keeps coming from the session's configured model through the registry. resolvedModel is attribution, not billing.

Why it belongs in Zero

Zero already persists usage for local accounting. This makes that record truthful when a router is involved, at no cost when it is not. It is provider-agnostic: any router that echoes model benefits, and nothing is provider-specific.

Alternatives or workarounds considered

  • Replacing model with the resolved value would change pricing and break escalation attribution, so it is a separate key.
  • Logging it only in debug output would leave zero usage report unable to use it.

Examples, screenshots, or prior discussion

A working implementation exists on my fork, FabioLeitao/zero: commit 9bfb22f7 (merged into the fork's main through fork PR #5, branch feat/resolved-model-provider-usage). It touches 11 files, +129/-1, most of it tests: internal/providers/openai (codex_responses.go, provider.go, tool_state.go, types.go), internal/zeroruntime (types.go, helpers.go), internal/usage/report.go, plus tests.

Happy to adjust the key name, the merge rule, or the scope to whatever the maintainers prefer before anything is proposed as a PR.

Activity

2
Vasanthdev20041mo ago

This reads well, and keeping it a separate attribution-only key, with model and pricing untouched, is the right split. Approving it, with one condition and two small asks for the PR:

  • Treat the value as untrusted text. It comes straight from the upstream stream, so a gateway can put anything there, and zero usage report prints it to a terminal. Strip control and format characters and cap the length (a couple of hundred bytes is plenty for a model id) before it's stored. Please include a test with an escape sequence and an oversized value in the stream.
  • Omit it when it matches the requested model. Most direct providers echo the requested id back, so this keeps ordinary usage events unchanged and makes resolvedModel mean "a router picked something else".
  • Scope: OpenAI-compatible chunks and Codex response.model as you describe is fine for the first PR. Other providers that report a model can follow later.

Marking this approved.

Vasanthdev20047d ago

A note on scope, so it is recorded here and not only in the review of #1234.

"When usage arrives across several chunks, the last non-empty value wins" means this: each usage chunk is recorded with the last model the provider had reported by the time that chunk arrived, and when a response sends several usage chunks the latest one wins. That is what #1234 does.

It does not mean holding a usage event back in case the model id changes after the last usage chunk. I asked for that to be taken out of #1234, because it changed how usage is delivered for every provider to cover a case no provider is known to produce. If a provider turns up that reports a different model after its final usage chunk, that is a new issue, with a captured stream attached.