feat(insights): surface included/estimated/unknown cost buckets (#77223) - #77247
Open
webtecnica wants to merge 1 commit into
Open
feat(insights): surface included/estimated/unknown cost buckets (#77223)#77247webtecnica wants to merge 1 commit into
webtecnica wants to merge 1 commit into
Conversation
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.
Summary
InsightsEngine._compute_overviewsumsestimated_cost_usd+actual_cost_usdinto a singleestimated_costnumber. Sessions withcost_status = 'included'(subscription-included providers, e.g.openai-codex) contribute zero per-session — correct — but the overview never surfaced the bucket, so an aggregate "$X total" silently understated usage. Theincluded_cost_sessionscounter existed in the overview dict but was rendered in neither formatter and carried no dollar figure.This PR splits the aggregate cost into three buckets — estimated / included / unknown — with dollar figures where computable, and renders them in both
format_terminalandformat_gateway.Root Cause
_compute_overviewonly accumulatedincluded_cost_sessions/unknown_cost_sessionscounts, never dollar values for the included bucket (subscription-included routes price at zero) and never rendered any of it. Real DB evidence (Aug 2026): 473 sessions → 315included, 134estimated, 3unknown, 21NULL; the ledger collapsed to "$0" for the included majority.Change
agent/insights.py_estimate_included_market_cost(session): prices a subscription-included session at market rates using the same price table asestimate_usage_cost(remaps theopenai-codexroute toopenaiso the official docs snapshot applies); returns0.0when no market price is computable._compute_overviewnow emitsincluded_cost(hypothetical at-market $),included_cost_tokens,estimated_cost_sessions, andunknown_cost(always0.0— no pricing signal exists, the count is the signal; formatters render it as "no pricing signal", never "$0.00").format_terminal: new💰 Costsection listing Estimated / Included (tokens + sessions + "~$X at market rates (hypothetical)", or "market price unavailable") / Unknown (count + "no pricing signal").format_gateway: new**Cost:**line with the same three buckets (compact, still labeled hypothetical).tests/agent/test_insights.pyTestCostBucketsclass: bucket separation in the overview, at-market pricing of included sessions with known models, and terminal/gateway rendering.Verification
python3 -m pytest tests/agent/test_insights.py -q→ 31 passedpython3 -m pytest tests/ -k insights -p no:xdist -q→ 45 passed, 3 skippedpython3 -m pytest tests/agent/test_usage_pricing.py tests/cli/test_cli_insights_command.py tests/gateway/test_insights_unicode_flags.py tests/hermes_state/test_aux_usage_accounting.py -q→ 31 passedIncluded: 4,500,000 tokens (2 sessions, subscription) — ~$12.50 at market rates (hypothetical)**Cost:** estimated $0.75 | 2 included / 4,500,000 tokens (~$12.50 at market (hypothetical)) | 1 unknown (no pricing signal)Closes #77223