Skip to content

feat: add estimated cost to token usage display#114

Merged
simongdavies merged 2 commits intohyperlight-dev:mainfrom
simongdavies:token-cost-estimates
May 7, 2026
Merged

feat: add estimated cost to token usage display#114
simongdavies merged 2 commits intohyperlight-dev:mainfrom
simongdavies:token-cost-estimates

Conversation

@simongdavies
Copy link
Copy Markdown
Member

Adds model-aware cost estimation to both per-request inline stats and the session summary (/tokens command + exit display).

Pricing table:

  • Claude Opus/Sonnet/Haiku (with cache read/write rates)
  • OpenAI o1, o3, GPT-4.1, GPT-4o (with cache read rates)
  • Gemini 2.5 Pro, 2.5 Flash (with cache read rates)

Per-request display now shows: '~$X.XX' at the end of the stats line Session summary now shows:

  • Est. Cost with model tier label
  • Cache savings (how much caching saved vs no-cache pricing)

Also tracks cacheWriteTokens (was available from SDK but not accumulated) for accurate cost calculation.

Implementation:

  • getModelPricing(modelName) — prefix-based pricing lookup
  • estimateCost(pricing, in, out, cacheRead, cacheWrite) — USD calc
  • Both exported for testing/reuse

Adds model-aware cost estimation to both per-request inline stats and
the session summary (/tokens command + exit display).

Pricing table:
- Claude Opus/Sonnet/Haiku (with cache read/write rates)
- OpenAI o1, o3, GPT-4.1, GPT-4o (with cache read rates)
- Gemini 2.5 Pro, 2.5 Flash (with cache read rates)

Per-request display now shows: '~$X.XX' at the end of the stats line
Session summary now shows:
- Est. Cost with model tier label
- Cache savings (how much caching saved vs no-cache pricing)

Also tracks cacheWriteTokens (was available from SDK but not
accumulated) for accurate cost calculation.

Implementation:
- getModelPricing(modelName) — prefix-based pricing lookup
- estimateCost(pricing, in, out, cacheRead, cacheWrite) — USD calc
- Both exported for testing/reuse

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 7, 2026 14:37
@simongdavies simongdavies added the enhancement New feature or request label May 7, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds model-aware USD cost estimation to token usage output for both per-request usage stats and session-level token summaries, including cache read/write accounting.

Changes:

  • Track cumulative cache-write tokens in agent state and usage accumulation.
  • Introduce a prefix-matched model pricing table plus reusable pricing/cost helpers.
  • Display estimated per-request cost and session cost (with cache savings) in token summaries.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/agent/state.ts Adds totalCacheWriteTokens to agent state and initializes it.
src/agent/llm-output.ts Implements model pricing lookup + cost estimation and surfaces cost/caching info in usage displays.
src/agent/event-handler.ts Accumulates cacheWriteTokens into session totals.

Comment thread src/agent/llm-output.ts
Comment thread src/agent/llm-output.ts
Comment thread src/agent/llm-output.ts
Comment thread src/agent/llm-output.ts
- Fix JSDoc on estimateCost: removed incorrect 'returns undefined'
  claim — function always returns a number
- Fix per-request cost double-counting: subtract cacheReadTokens from
  inputTokens before estimating (inputTokens includes cached portion)
- Make currentModel optional in formatTokenSummary — gracefully skips
  pricing when model is unknown
- Tighten prefix matching in getModelPricing: require word boundary
  (end-of-string or '-') after prefix to prevent misclassification
  (e.g. 'o3' won't match 'o3something', only 'o3' or 'o3-mini')

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
@simongdavies simongdavies merged commit 09d6567 into hyperlight-dev:main May 7, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants