feat: add estimated cost to token usage display#114
Merged
simongdavies merged 2 commits intohyperlight-dev:mainfrom May 7, 2026
Merged
feat: add estimated cost to token usage display#114simongdavies merged 2 commits intohyperlight-dev:mainfrom
simongdavies merged 2 commits intohyperlight-dev:mainfrom
Conversation
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>
Contributor
There was a problem hiding this comment.
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. |
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds model-aware cost estimation to both per-request inline stats and the session summary (/tokens command + exit display).
Pricing table:
Per-request display now shows: '~$X.XX' at the end of the stats line Session summary now shows:
Also tracks cacheWriteTokens (was available from SDK but not accumulated) for accurate cost calculation.
Implementation: