Skip to content

feat(dashboard): /api/api-cost-stats endpoint (#5995)#134

Merged
manager-identity[bot] merged 1 commit into
mainfrom
feat/dashboard-api-cost-stats
May 13, 2026
Merged

feat(dashboard): /api/api-cost-stats endpoint (#5995)#134
manager-identity[bot] merged 1 commit into
mainfrom
feat/dashboard-api-cost-stats

Conversation

@manager-identity
Copy link
Copy Markdown
Contributor

Summary

Final sub-PR of #5995. Adds the dashboard endpoint that reads api_cost_logs from ai-memory's brain.db and serves per-call cost time-series.

  • GET /api/api-cost-stats?days&provider&model&project&caller returns parallel time-series: by_date, by_model, by_project, by_caller, plus de-duplicated lists and a summary block.
  • Pattern mirrors /api/tool-stats: read-only sqlite via file:…?mode=ro, all filters parameter-bound, zero-fills by_date so the chart x-axis is contiguous, returns empty shape when the table is absent.
  • Smoke-tested against the live 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=30 returns real data with the expected shape
  • Browser smoke after dist rebuild: add a LocalCostPanel.tsx consuming this endpoint (deferred to a follow-up frontend card)

Related

  • Closes #5995
  • Builds on sub-PR 2a: synth-insight-labs#16 (local sink)
  • Builds on sub-PR 2b: ai-memory#87 (canonical schema)
  • Follow-ups: 8 instrumentation cards filed Low for the remaining call sites

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>
@manager-identity manager-identity Bot added the feature New feature label May 13, 2026
@manager-identity manager-identity Bot merged commit 364ebd1 into main May 13, 2026
2 checks passed
@manager-identity manager-identity Bot deleted the feat/dashboard-api-cost-stats branch May 13, 2026 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants