feat(storage): aggregate cost-ledger spend by dimension - #40
Merged
Conversation
Add Store.SummarizeLedger — group spend (calls/tokens/dollars) across runs by provider, model, day, run name, or a run-metadata key (e.g. metadata.team), with optional since/until time bounds. Sums from the cost_ledger (the auditable source), not the run rollup. The grouping expression is chosen from a fixed whitelist (it's structural, not a bindable parameter); the metadata JSON path is bound as a value and its key is validated, so nothing is built unsafely from caller input. This is the storage foundation for the usage-rollup CLI/API (#37) — no public surface yet.
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.
First PR of the v0.2.0 observability work (#37) — the storage foundation. No public surface changes; the CLI (
audit summary) and API (GET /v1/usage/summary) land in follow-ups on top of this.What
Store.SummarizeLedger(ctx, SummarizeOptions) (UsageSummary, error)— groups cost-ledger spend (calls / prompt+completion tokens / dollars) across runs by one dimension:provider,model,day(UTC date),name(run name), ormetadata.<key>(e.g.metadata.team)Since/Untiltime bounds (inclusive / exclusive)TotalSpend is summed from
cost_ledger(the auditable source of truth), not the run rollup.Safety
metadata.<key>, the key is validated (identifier-like only) and the JSON path is bound as a parameter tojson_extract. No SQL injection surface.Tests
TestSummarizeLedger: by team (metadata), provider, name, and day (with a time bound), grand total, spend-desc ordering, and rejection of an unsupported dimension / unsafe metadata key.go test ./internal/storage,go vet, gofmt all clean. (json_extractconfirmed working under the pure-Gomodernc.org/sqlitedriver.)Part of the v0.2.0 series: storage → API → CLI → OTel tagging → tool_calls read-back (#38) → example dashboard.