Skip to content

feat: show per-message token usage and estimated cost (USD) in chat - #13102

Open
Meinianda-L wants to merge 2 commits into
continuedev:mainfrom
Meinianda-L:feat/show-request-cost
Open

feat: show per-message token usage and estimated cost (USD) in chat#13102
Meinianda-L wants to merge 2 commits into
continuedev:mainfrom
Meinianda-L:feat/show-request-cost

Conversation

@Meinianda-L

@Meinianda-L Meinianda-L commented Aug 8, 2026

Copy link
Copy Markdown

Closes #1703

Type of change

  • New feature

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.

Claude Sonnet · 16,000 tokens · $0.0960

Changes:

  1. core: PromptLog now carries usage (and the raw model id) from the
    LLM request. llm/streamChat already passes the PromptLog through, so no
    protocol change is needed.
  2. core: calculateRequestCost refactored into a shared cost helper;
    pricing lives in core/llm/utils/modelPricing.json, extracted from the
    models.dev API (https://models.opencode.ai/api.json — the same data
    source 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-turbo
    aligned with models.dev (0.5/1.5). Display-only, not
    billing-authoritative.
  3. gui: new UsageCost footer component. The line is omitted entirely
    when 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-info package, no runtime dependency on models.dev.

How did you verify your code works?

# core (node 20.20.1, jest)
NODE_OPTIONS=--experimental-vm-modules npx jest core/llm/utils/calculateRequestCost.test.ts
  17 pass, 0 fail

# core full suite: 856 pass (19 pre-existing failures in llm.test.ts require API keys)

# gui (vitest)
npm test
  377 pass, 0 fail

# repo
npx tsc --noEmit   # core + gui clean
npx eslint ...     # clean on changed files
npx prettier --check ...  # clean

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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

- 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
@Meinianda-L
Meinianda-L requested a review from a team as a code owner August 8, 2026 09:09
@Meinianda-L
Meinianda-L requested review from sestinj and removed request for a team August 8, 2026 09:09
@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@Meinianda-L

Meinianda-L commented Aug 8, 2026

Copy link
Copy Markdown
Author

Friendly ping for review: @sestinj and @bdougie。 The PR is self-contained and extends the existing cost calculation in core/llm/utils/calculateRequestCost.ts, so it can be reviewed and merged independently.

@Meinianda-L

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@Meinianda-L

Copy link
Copy Markdown
Author

Heads-up on the failing checks — neither is related to this PR:

  1. test (windows-latest, 22): infra flake, npm registry returned 502 Bad Gateway - GET https://registry.npmjs.org/npm/-/npm-10.9.3.tgz during install. Could a maintainer re-run this job?

  2. jetbrains-tests: pre-existing failure — Autocomplete > testAutocomplete() fails on main too (see merged commit badf887, same DriverWithContextError). This PR doesn't touch autocomplete or the IntelliJ extension (the only core change adds two optional fields to the chat PromptLog return value).

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)
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.

[CON-252] Show token usage per message

1 participant