feat: track per-message LLM cost in chat metrics - #6102
Open
angelplusultra wants to merge 13 commits into
Open
Conversation
…nal-completion-call-not-the-full-run' into feat/cost-metric-field # Conflicts: # server/__tests__/utils/agents/aibitat/providers/ai-provider.test.js
shatfield4
requested changes
Aug 8, 2026
shatfield4
left a comment
Collaborator
There was a problem hiding this comment.
Small nit here with deduping 2 functions with the same logic in the quick review that I did.
The only other thing that I would investigate is if adding the new fields to the metrics/usage object causes any side effects (specifically in the openai compat responses). I'm not exactly sure if this can cause errors across other providers but lets be sure to test this.
Comment on lines
+120
to
+124
| static #toSafeMetric(value) { | ||
| const number = Number(value); | ||
| if (!Number.isFinite(number) || number < 0) return 0; | ||
| return number; | ||
| } |
Collaborator
There was a problem hiding this comment.
See previous comment on #toSafeMetric to dedupe and form into a util
| const number = Number(value); | ||
| if (!Number.isFinite(number) || number < 0) return 0; | ||
| return number; | ||
| } |
Collaborator
There was a problem hiding this comment.
This looks like the same logic as the #toSafeMetric function in ai-provider.js. Let's move this to a util and reuse it in both place.
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.
Pull Request Type
Translations #6103
Description
Adds per-message USD cost tracking to chat and agent metrics. This is plumbing for upcoming cost features — costs are recorded in the metrics of every chat and agent run, but intentionally not displayed anywhere in the UI yet, since individual chat/run costs are usually too small to be useful on their own.
Server
ModelPricinghelper (server/utils/helpers/modelPricing/) that sources pricing from models.dev, cached on disk for 3 days with ETag revalidation. Falls back to the pricing snapshot bundled in@opencode-ai/modelswhen offline/air-gapped.inputCost/outputCost/totalCost) is added to metrics in all chat flows (workspace stream, developer API, embed, OpenAI-compatible endpoint, Telegram bot) and agent runs. Agent runs price each completion call individually, so totals stay correct even if the model changes mid-run.display_currencysystem setting (admin/manager editable, validated against supported currencies) and aGET /system/exchange-ratesendpoint. Exchange rates come from Frankfurter, cached on disk server-side for 30 days with a 10s fetch timeout — air-gapped installs degrade to USD.Display currency (built, not yet wired in)
The display currency setting and cost conversion pipeline are fully built and ready to go — they just need to be wired in once we have a feature we want to build around cost:
CurrencyPreferencedropdown (Settings → Interface) is complete but commented out of the page with a note.useCurrencyhook fetches the instance currency + rates and formats stored USD costs for display; currently unused, documented as the entry point for future cost surfaces.Includes #6085 (cumulative agent token usage), which this builds on — agent cost is derived from the accumulated per-call usage that PR introduces.
Visuals (if applicable)
N/A — no user-facing changes. Cost lives only in stored metrics for now.
Additional Information
Pricing data adds no hard runtime dependency: if models.dev is unreachable and no cache exists, the bundled snapshot is used; if that also fails, chats simply record no cost.
Developer Validations
yarn lintfrom the root of the repo & committed changes