Skip to content

feat: populate UsageUpdate.cost with estimated session cost#216

Open
simonrosenberg wants to merge 1 commit intozed-industries:mainfrom
simonrosenberg:feat/usage-update-cost-estimation
Open

feat: populate UsageUpdate.cost with estimated session cost#216
simonrosenberg wants to merge 1 commit intozed-industries:mainfrom
simonrosenberg:feat/usage-update-cost-estimation

Conversation

@simonrosenberg
Copy link
Copy Markdown

Summary

Populate UsageUpdate.cost with an estimated cumulative session cost derived from per-turn token counts and a model pricing table.

  • Add pricing.rs with per-model pricing for gpt-4.1, gpt-4o, o3, o4-mini families and prefix-matching for date-suffixed model slugs
  • Track cumulative cost across the session via Rc<Cell<f64>> shared between ThreadActor and PromptState
  • Compute incremental cost from last_token_usage on each TokenCountEvent
  • Update model tracking on ModelReroute events so mid-session model switches are priced correctly
  • For unknown models, cost is omitted (None)

This is an interim solution until the upstream Codex event stream exposes authoritative cost (openai/codex#16258).

Caveats (documented in #209):

  • Service tier / fast mode pricing is not accounted for (no upstream signal — see openai/codex#13794)
  • Pricing table needs periodic updates as OpenAI changes rates
  • ChatGPT subscription users see estimated API-equivalent cost (not their actual billing)

Closes #209

Test plan

  • CI build passes
  • Verify UsageUpdate notifications include cost with amount > 0 and currency: "USD" for known models
  • Verify cost is omitted for unknown models
  • Verify cost accumulates correctly across multiple turns
  • Verify model reroute updates pricing for subsequent turns
  • Unit tests in pricing.rs pass

🤖 Generated with Claude Code

Derive cumulative session cost from per-turn token counts and a model
pricing table, so ACP clients can display meaningful cost instead of
$0.00 or null.

- Add `pricing.rs` with per-model pricing table (gpt-4.1, gpt-4o, o3,
  o4-mini, etc.) and prefix-matching for date-suffixed model slugs
- Track cumulative cost across the session via `Rc<Cell<f64>>` shared
  between `ThreadActor` and `PromptState`
- Compute incremental cost from `last_token_usage` on each
  `TokenCountEvent`, using the active model's pricing
- Update model tracking on `ModelReroute` events so mid-session model
  switches are priced correctly
- For unknown models, cost is omitted (remains `None`)

This is an interim solution until the upstream Codex event stream
exposes authoritative cost (openai/codex#16258).

Closes zed-industries#209

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UsageUpdate.cost is still not populated for codex-acp

1 participant