Skip to content

feat(usage): instrument project chat/analysis, add source breakdown and currency display - #505

Merged
Ayinkx merged 2 commits into
AyinkxLab:mainfrom
olaams-tech:fix/phase-10-usage-ledger-currency
Oct 17, 2026
Merged

Ayinkx merged 2 commits into
AyinkxLab:mainfrom
olaams-tech:fix/phase-10-usage-ledger-currency

Conversation

@olaams-tech

Copy link
Copy Markdown
Contributor

Summary

Fixes #429
Fixes #432
Fixes #451
Fixes #441

This PR implements the Phase 10 usage-ledger work for these four issues:

  • Adds the central UsageRecord ledger model plus a reversible Alembic migration (usage_records), including scoped foreign keys and query indexes.
  • Adds app/services/usage_ledger.py with count normalisation, cost estimation, scope filtering, summaries, and source aggregation. Unknown historical sources are grouped safely under unknown.
  • Instruments project chat for both the non-streamed and streamed reply paths with source="project_chat", carrying user, workspace and project scope and the estimated flag.
  • Instruments every project analysis completion with source="analysis", carrying user, workspace and project scope and the estimated flag.
  • Adds GET /api/usage/breakdown (optionally filtered by workspace_id or project_id, always owner-scoped), returning counts/totals per source, overall totals, known sources, and unknown sources.
  • Adds CURRENCY_CODE, CURRENCY_SYMBOL and CURRENCY_DECIMAL_PLACES configuration (default USD) with shared Python/JS formatting. Every cost figure now shows the currency code and is explicitly labelled as an estimate.

Notes

The ledger foundation is a prerequisite for the instrumentation issues; it is included here because /api/usage/breakdown and the project instrumentation need a central store. It is implemented to the schema described in #366.

Verification

  • python -m ruff check . — passed
  • python -m black --check . — passed (268 files unchanged)
  • New ledger/currency tests — passed
  • Project analysis/chat, token usage, project route, chat API and migration regression tests — passed
  • Full pytest — all tests pass except two pre-existing failures on clean origin/main:
    tests/test_notifications.py::TestBudgetAlertDeduplication imports the missing app.services.budget_alerts module. Confirmed the same failure on an untouched origin/main worktree.
  • The currency JS module was exercised directly with Node; its Vitest test is included at tests/js/currency.test.js and runs in CI with the JS toolchain.

…cy display

Add the central UsageRecord ledger and reversible migration, instrument project chat (streamed and non-streamed) and project analysis with project/workspace scope and an estimated flag, expose a per-user breakdown by source API, and format every cost figure in a configurable currency with an explicit estimate label.

Refs AyinkxLab#429, AyinkxLab#432, AyinkxLab#451, AyinkxLab#441. Adds the ledger foundation required by the Phase 10 instrumentation issues.
@drips-wave

drips-wave Bot commented Oct 17, 2026

Copy link
Copy Markdown

@olaams-tech Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Keeps the PostgreSQL chat coverage gate above 85% by exercising the new ledger breakdown endpoint and the SSE reply branch of the messages API.
@olaams-tech

Copy link
Copy Markdown
Contributor Author

CI note: the Tests job fails only on TestBudgetAlertDeduplication importing the missing app.services.budget_alerts module. I reproduced those two failures on a clean origin/main worktree at 4d1d3be, so they are pre-existing and unrelated to this PR. All other checks pass, including Tests (PostgreSQL), Lint & format, Frontend tests, Build Docker image, and Asset & VS Code guard.

@Ayinkx
Ayinkx merged commit 559932b into AyinkxLab:main Oct 17, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants