Repository navigation
feat(cost): per-basis cost rollup with per-model breakdown (#1136) - #1166
Conversation
Cost rollups today expose a single ``total_cost_usd`` per session. #803 explicitly required separating cost bases and #870 AC required API-equivalent and subscription quota/burn as separate fields. PR #987 closed #870 with lint/type cleanup only; the basis split never landed. Extend the typed cost read model with a ``CostBasisPayload`` carrying ``provider_reported_usd``, ``api_equivalent_usd``, ``subscription_equivalent_usd``, ``catalog_priced_usd``, and ``tool_surcharge_usd`` as independent axes. Same usage may be expressed on more than one axis (e.g. a provider-reported total alongside a parallel catalog-priced reconciliation), so values do not sum to ``total_usd``; ``total_usd`` remains the legacy summary draw for backwards compatibility and is documented as drawing from ``provider_reported`` when an exact total is present, otherwise from ``catalog_priced``. Add ``CostModelBreakdown`` and surface a per-model breakdown on both ``CostEstimatePayload.per_model_breakdown`` (session scope) and ``CostRollupInsight.per_model_breakdown`` (cross-session scope). Mixed- model sessions return one row per model; nothing is collapsed. ``CostEstimatePayload.unavailable_reason`` enumerates ``no_model``, ``no_price``, ``no_tokens``, ``provider_zero``, ``subscription_unconfigured``, and ``no_messages`` so consumers can render "why" instead of an opaque zero. Wire ``list_cost_rollup_insights`` to aggregate the basis axes, accumulate ``unavailable_reason_counts``, and merge per-model rows across sessions (incrementing ``session_count`` per merged model). Expose the new basis fields in the ``cost_rollups`` insight registry so they appear in CLI and MCP envelopes alongside the existing total. Closes #1136. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Move the per-group basis + per-model breakdown reduction out of ``operations/archive.py:list_cost_rollup_insights`` and into a typed helper ``aggregate_cost_rollup_insights`` in ``insights/archive_rollups.py``. Keeps ``operations/archive.py`` under its 1200-line budget and colocates the rollup logic with the other archive insight aggregators. No behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (6)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
polylogue/polylogue/archive/semantic/pricing.py
Lines 658 to 661 in 40f78d8
When a session has provider-level usage (conversation_estimate.status == "priced") and also message-level model data, this early return bypasses the newly computed per_model_breakdown and returns the conversation-level estimate as-is. In that case mixed-model sessions are collapsed to a single dominant model row, so downstream rollups lose the per-model split introduced in this change. This is most visible for exports that include both aggregate session usage and per-message model/tokens.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Summary
Splits cost rollups across the five basis axes (#803/#870 carry-over) and adds per-model breakdowns to both session-cost and cost-rollup insights. Every
unavailablerow now carries a discrete reason so consumers can render "why" instead of an opaque zero.Closes #1136.Problem
Cost rollups today expose a single
total_cost_usdper session.provider_reported_usd,api_equivalent_usd,subscription_equivalent_usd,catalog_priced_usd, andtool_surcharge_usd.Consumers querying
CostRollupInsight.total_usdcannot distinguish "this session billed $1.25 against the API" from "this session would cost $1.25 against the catalog if billed via API" from "this session burned X subscription credits". Mixed-model sessions collapse into one row keyed by the dominant model.Solution
Typed basis split.
CostBasisPayloadcarries the five basis axes as independent fields. Same usage may be expressed on more than one axis (e.g. a provider-reported total alongside a parallel catalog-priced reconciliation), so basis values do not sum tototal_usd.total_usdremains the legacy single-number summary for backwards compatibility — documented as drawing fromprovider_reported_usdwhen present, otherwise fromcatalog_priced_usd.Per-model breakdown.
CostModelBreakdownrows hang off both:CostEstimatePayload.per_model_breakdown(session scope) — built inestimate_conversation_costby grouping message estimates by(model_name, normalized_model).CostRollupInsight.per_model_breakdown(cross-session scope) — aggregated byaggregate_cost_rollup_insightsininsights/archive_rollups.py. Sessions with no breakdown contribute a single synthesized row from the dominant model so provider-reported-only sessions still surface per-model context.Mixed-model sessions return one row per model, ranked by
total_usddesc. Nothing is collapsed.Unavailable reasons.
CostEstimatePayload.unavailable_reason(CostUnavailableReasonliteral) enumeratesno_model,no_price,no_tokens,provider_zero,subscription_unconfigured,no_messages. Rollups carryunavailable_reason_countsso a consumer can see "12 sessions unavailable: 8no_tokens, 4no_model".Surfaces. Existing CLI/MCP envelopes update automatically:
cost_rollupsregistry addsprovider_usd/api_usd/sub_usd/catalog_usdcolumns alongside the existing total;CostRollupInsight.model_dumpcarries the fullbasisandper_model_breakdownshapes through JSON.Refactor.
operations/archive.py:list_cost_rollup_insightsshrinks to a thin orchestrator over the newaggregate_cost_rollup_insightshelper, keepingoperations/archive.pyunder its 1200-line budget.Files touched:
polylogue/archive/semantic/pricing.py—CostBasisPayload,CostModelBreakdown,CostUnavailableReason,CostBasis;_exact_estimate/_estimate_from_usage/estimate_conversation_costpopulate basis and breakdownpolylogue/insights/archive.py—CostRollupInsightgainsbasis,unavailable_reason_counts,per_model_breakdownpolylogue/insights/archive_rollups.py— newaggregate_cost_rollup_insights+ helperspolylogue/insights/registry.py— basis columns incost_rollupsfield listpolylogue/operations/archive.py— call the new aggregatortests/unit/insights/test_cost_basis_split.py— 7 new contractsNon-goals: no new pricing catalog sources, no tokenizer changes, no CLI/MCP surface changes beyond the auto-generated registry columns. Surface-facing children land separately.
Verification
nix develop -c pytest tests/unit/insights/test_cost_basis_split.py tests/unit/core/test_pricing.py tests/unit/insights/ tests/unit/mcp/ tests/unit/cli/test_insights.py -q→288 passednix develop -c devtools verify --quick→exit_code: 0(mypy --strict, ruff, render-all --check, file budgets all green)nix develop -c mypy polylogue tests devtools→Success: no issues found in 1395 source filesCloses #1136.
🤖 Generated with Claude Code