Skip to content

feat(insights): surface included/estimated/unknown cost buckets (#77223) - #77247

Open
webtecnica wants to merge 1 commit into
NousResearch:mainfrom
webtecnica:fix/77223-cost-buckets-upstream
Open

feat(insights): surface included/estimated/unknown cost buckets (#77223)#77247
webtecnica wants to merge 1 commit into
NousResearch:mainfrom
webtecnica:fix/77223-cost-buckets-upstream

Conversation

@webtecnica

Copy link
Copy Markdown
Contributor

Summary

InsightsEngine._compute_overview sums estimated_cost_usd + actual_cost_usd into a single estimated_cost number. Sessions with cost_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. The included_cost_sessions counter 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_terminal and format_gateway.

Root Cause

_compute_overview only accumulated included_cost_sessions / unknown_cost_sessions counts, 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 → 315 included, 134 estimated, 3 unknown, 21 NULL; the ledger collapsed to "$0" for the included majority.

Change

  • agent/insights.py
    • New module helper _estimate_included_market_cost(session): prices a subscription-included session at market rates using the same price table as estimate_usage_cost (remaps the openai-codex route to openai so the official docs snapshot applies); returns 0.0 when no market price is computable.
    • _compute_overview now emits included_cost (hypothetical at-market $), included_cost_tokens, estimated_cost_sessions, and unknown_cost (always 0.0 — no pricing signal exists, the count is the signal; formatters render it as "no pricing signal", never "$0.00").
    • format_terminal: new 💰 Cost section 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.py
    • New TestCostBuckets class: bucket separation in the overview, at-market pricing of included sessions with known models, and terminal/gateway rendering.
    • Updated the two formatter tests that asserted cost display was hidden (reverses fix(insights): hide cache read/write and cost metrics from display #11477's display trim for these surfaces, per this issue's request).

Verification

  • python3 -m pytest tests/agent/test_insights.py -q → 31 passed
  • python3 -m pytest tests/ -k insights -p no:xdist -q → 45 passed, 3 skipped
  • python3 -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 passed
  • Manual smoke test: 2 included (1 priceable gpt-4o + 1 unpriceable gpt-5.2-codex), 1 estimated, 1 unknown →
    • terminal: Included: 4,500,000 tokens (2 sessions, subscription) — ~$12.50 at market rates (hypothetical)
    • gateway: **Cost:** estimated $0.75 | 2 included / 4,500,000 tokens (~$12.50 at market (hypothetical)) | 1 unknown (no pricing signal)

Closes #77223

@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint area/billing Account usage, credit usage, billing (cross-cutting) labels Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing Account usage, credit usage, billing (cross-cutting) comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(usage): surface included/estimated/unknown cost buckets in aggregate views

2 participants