feat: show per-message token usage and estimated cost (USD) in chat - #13102
feat: show per-message token usage and estimated cost (USD) in chat#13102Meinianda-L wants to merge 2 commits into
Conversation
- core: PromptLog now carries the provider-reported usage and the raw model id, populated by BaseLLM.streamChat - core: calculateRequestCost refactored into a shared helper with per-provider pricing tables; prices added for DeepSeek, Gemini and Mistral plus cache-read pricing for GPT-4o family, sourced from models.dev (same pricing DB used by OpenCode), display-only - gui: new UsageCost footer under assistant messages showing model, tokens and estimated USD cost; omitted when usage is unavailable or the model price is unknown - tests for cost calculation and the footer component Closes continuedev#1703
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
|
Heads-up on the failing checks — neither is related to this PR:
All other checks pass: lint, prettier, core-checks, gui-checks, vscode-checks, binary-checks, packages, e2e, and tests on 18/20/22/24 across all three OSes. |
…urce as OpenCode) - new core/llm/utils/modelPricing.json: pricing extracted from the models.dev API (https://models.opencode.ai/api.json, the data source used by OpenCode), retrieved 2026-08-08, with source attribution in the file - calculateRequestCost reads pricing from the JSON instead of inline tables; behavior unchanged - align gpt-3.5-turbo pricing with models.dev (0.5/1.5)
Closes #1703
Type of change
What does this PR do?
Shows per-message token usage and estimated cost (USD) under each assistant
message in the chat, similar to the cost display in Cline/OpenCode. Issue
#1703 requested per-message token usage; its comments also asked for an
estimated cost.
Changes:
PromptLognow carriesusage(and the rawmodelid) from theLLM request.
llm/streamChatalready passes thePromptLogthrough, so noprotocol change is needed.
calculateRequestCostrefactored into a shared cost helper;pricing lives in
core/llm/utils/modelPricing.json, extracted from themodels.dev API (
https://models.opencode.ai/api.json— the same datasource OpenCode uses at runtime), retrieved 2026-08-08, with the source
attributed in the file. Prices extended to DeepSeek, Gemini and Mistral,
plus cache-read pricing for GPT-4o/gpt-4o-mini, and
gpt-3.5-turboaligned with models.dev (0.5/1.5). Display-only, not
billing-authoritative.
UsageCostfooter component. The line is omitted entirelywhen no usage was reported; the cost part is omitted when the model price
is unknown — we never show a made-up figure.
Not changed: the console/stats page and CLI (they already show cost), the
llm-infopackage, no runtime dependency on models.dev.How did you verify your code works?
New tests cover known-model pricing (Anthropic/OpenAI/DeepSeek/Gemini/Mistral),
unknown model/provider -> null, zero tokens, prefix/case matching, cache
write/read branches, and the GUI footer's render/omit behavior.
Screenshots / recordings
N/A — formatting is plain text (
Model · 16,000 tokens · $0.0960).Checklist