feat: per-request cost tracking (C7)#12
Merged
Merged
Conversation
Attributes a USD cost to every trace, so "cost" is a real number instead of an inferred request count. Surfaces spend, cache savings, and cost-over-time on the dashboard. (Closes the cost gap from the v0.1.x audit.) What changed - New pure `cost.ts` (`computeCostUsd`): token usage × per-1K price → USD, or `null` when the model is unpriced or usage is unknown (never a guess). - Config: optional `pricing` map (USD per 1K input/output tokens, per model) → `ResolvedConfig.pricing`; added to `sentinel.config.example.json`. - Trace: `costUsd` on every record + span attribute `sentinel.cost_usd`, set on the real path (routed model) and on a cache hit (the spend the cache avoided). The SQLite store gains a `cost_usd` column with a guarded migration so pre-existing DBs upgrade in place. - Dashboard: a "Cost" stat (with cache savings) and a cost-over-time sparkline; `computeStats` sums spend (non-cache) vs. saved (cache). Tests - cost math (null / free / float-dust rounding), config parse + negative reject, SQLite round-trip + migration, server attributes cost end-to-end, dashboard spend/saved aggregation, and an e2e cost assertion. Dashboard screenshot regenerated to show the new views. How to verify - `pnpm verify` → green (217 tests, coverage ≥90%) - `pnpm build` → green - `pnpm test:e2e` → green No behavior change without a `pricing` block: cost stays `null` and nothing else moves.
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.
Attributes a USD cost to every trace, so "cost" is a real number instead of
an inferred request count. Surfaces spend, cache savings, and cost-over-time
on the dashboard. (Closes the cost gap from the v0.1.x audit.)
What changed
cost.ts(computeCostUsd): token usage × per-1K price → USD, ornullwhen the model is unpriced or usage is unknown (never a guess).pricingmap (USD per 1K input/output tokens, per model)→
ResolvedConfig.pricing; added tosentinel.config.example.json.costUsdon every record + span attributesentinel.cost_usd,set on the real path (routed model) and on a cache hit (the spend the cache
avoided). The SQLite store gains a
cost_usdcolumn with a guardedmigration so pre-existing DBs upgrade in place.
sparkline;
computeStatssums spend (non-cache) vs. saved (cache).Tests
reject, SQLite round-trip + migration, server attributes cost end-to-end,
dashboard spend/saved aggregation, and an e2e cost assertion. Dashboard
screenshot regenerated to show the new views.
How to verify
pnpm verify→ green (217 tests, coverage ≥90%)pnpm build→ greenpnpm test:e2e→ greenNo behavior change without a
pricingblock: cost staysnulland nothingelse moves.