feat(audit): spend rollup across runs (audit summary --by) - #139
Merged
Conversation
Per-run 'audit export' had no cross-run companion, so spend-by-team/user/feature was DIY SQL. Add 'riskkernel audit summary --by <provider|model|day|name|metadata.<key>>' on top of the existing store aggregation: roll cost-ledger spend up by a dimension, with --since/--until windows and --json output (table otherwise). Grouping by metadata.<key> turns the tags you put on runs into a spend report — 'audit summary --by metadata.team'. Deterministic SQL over the ledger you own; no LLM in the path. Also attribute spend on the OTel side: the model-call span now carries riskkernel.run.name and one riskkernel.run.meta.<key> per run tag, so the same grouping works in Datadog/Grafana/SigNoz without a separate run->team map. Both new attributes are pinned in api/v1/otel-genai.md. Tests: audit summary by metadata against a seeded store (grouped totals + grand total), the --by guard, parseTimeFlag, summaryHeader; and span assertions that run.name + meta.* are emitted when set and omitted when not.
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.
Closes the honest gap behind the "spend/usage visibility" pitch: the foundation (every model call is a ledger row tied to a run with a
name+metadata) was there, but there was no built-in rollup across runs — per-team/user spend was DIY SQL.The store-level aggregation (
SummarizeLedgerby provider/model/day/name/metadata.key) already shipped with the Postgres work; this adds the CLI surface and the OTel attribution on top.CLI
Rolls cost-ledger spend up by a dimension —
provider|model|day|name|metadata.<key>— with--since/--untilwindows and--json(table by default). Grouping bymetadata.<key>turns the tags you put on runs into a spend-by-team/user/feature report. Deterministic SQL over the ledger you own; no LLM in the path.OTel attribution
The model-call span now also carries
riskkernel.run.nameand oneriskkernel.run.meta.<key>per run tag, so the same grouping works in Datadog/Grafana/SigNoz without a separate run→team map. Both are pinned inapi/v1/otel-genai.md(public contract). Cardinality is the user's own (their tags).Still single-tenant — this is attribution via user-supplied tags, not auth/RBAC (that stays deferred per the architecture).
Tests
audit summary --by metadata.team --jsonagainst a seeded SQLite store → asserts per-team dollars (alpha=0.01, beta=0.03) and the grand total; the--byguard;parseTimeFlag(RFC3339 + date + invalid);summaryHeader.run.name+run.meta.*when set, and omits them on an unnamed/untagged run.go test -racegreen on otel + cmd;go vet/gofmtclean; binary smoke confirms the command is wired into help, the usage guard, and table output.Help text and shell completions updated for the new subcommand.
Closes #37