feat(dashboard): /api/api-cost-stats endpoint (#5995)#134
Merged
Conversation
Final sub-PR of the #5995 cost-instrumentation series. Adds a read-only dashboard endpoint that surfaces the api_cost_logs telemetry now flowing into brain.db (sub-PR 2a: SIL sink #16, sub-PR 2b: ai-memory schema #87). Endpoint: GET /api/api-cost-stats Query params: days (1..365), provider, model, project, caller Response: by_date / by_model / by_project / by_caller time-series + de-duplicated models/projects/callers lists + summary {total_calls, total_cost_usd, window_days} Implementation mirrors the existing /api/tool-stats pattern in app.py: - Read-only sqlite via file:...?mode=ro URI - All filters parameter-bound - Returns the empty response shape when brain.db is absent or api_cost_logs hasn't been created yet (resilient before sub-PR 2b reaches every machine) - Zero-fills by_date over the full window so the chart x-axis is contiguous Tests (tests/test_dashboard_api_cost_stats.py, 8 cases): - Empty response when brain.db missing - Top-level key set is stable - summary totals match the fixture (5 rows = 5 calls, $1.15) - by_model aggregates correctly per (date, model) - provider filter scopes all series (Anthropic-only, GPT excluded) - caller filter scopes all series - days clamps to [1, 365] - by_date is zero-filled across the window Smoke-tested against the live brain.db on this machine: 9 calls / $0.002179 / 4 models (claude-haiku-4-5, custom-model, gemini-2.5-flash, gpt-4.1-mini) across 30 days — confirming the table has real data flowing through the SIL sink. Closes #5995. Follow-up cards for the remaining 8 active call sites (answerer, cortana, trading-copilot, muffinpanrecipes, scaffolding) filed separately at Low priority. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Final sub-PR of #5995. Adds the dashboard endpoint that reads
api_cost_logsfrom ai-memory'sbrain.dband serves per-call cost time-series.GET /api/api-cost-stats?days&provider&model&project&callerreturns parallel time-series:by_date,by_model,by_project,by_caller, plus de-duplicated lists and asummaryblock./api/tool-stats: read-only sqlite viafile:…?mode=ro, all filters parameter-bound, zero-fillsby_dateso the chart x-axis is contiguous, returns empty shape when the table is absent.brain.db: 9 calls / $0.002179 across 30 days, 4 models — confirming the SIL sink (sub-PR 2a) is actually flowing data.Test plan
pytest tests/test_dashboard_api_cost_stats.py -v→ 8/8 pass (empty-DB safety, summary totals, by_model aggregation, provider/caller filters, days clamp, by_date zero-fill)curl http://localhost:8000/api/api-cost-stats?days=30returns real data with the expected shapeLocalCostPanel.tsxconsuming this endpoint (deferred to a follow-up frontend card)Related