Skip to content

feat(subscriptions): track per-subscription token usage across agents - #240

Merged
vybe merged 1 commit into
mainfrom
feature/subscription-usage-tracking
Apr 1, 2026
Merged

vybe merged 1 commit into
mainfrom
feature/subscription-usage-tracking

Conversation

@vybe

@vybe vybe commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Snapshots subscription_id at message-record time in chat_messages, chat_sessions, and schedule_executions — survives SUB-003 auto-switch reassignments
  • Adds output_tokens column to chat_messages for complete token accounting
  • New GET /api/subscriptions/{id}/usage endpoint returns rolling 5-hour and 7-day usage windows (tokens, cost, message count, active agents, rate-limit events)
  • Migration System Manifest UI #31 adds columns with safe ALTER TABLE ADD COLUMN guards

Motivation

Multiple agents share a single Claude Max/Pro subscription token. Previously there was no way to see aggregate consumption per subscription — only per-agent. With rolling-window visibility, operators can see how close a subscription is to its daily/weekly limit before hitting a 429.

Changes

Backend:

  • src/backend/db/schema.py — new columns + indexes
  • src/backend/db/migrations.py — migration System Manifest UI #31
  • src/backend/db/chat.py — add_chat_message + get_or_create_chat_session accept subscription_id, output_tokens
  • src/backend/db/schedules.py — execution records accept subscription_id
  • src/backend/db/subscriptions.py — get_subscription_usage() dual-window query
  • src/backend/db_models.py — SubscriptionUsageWindow, SubscriptionUsage models
  • src/backend/routers/chat.py — snapshots subscription at record time
  • src/backend/routers/subscriptions.py — new /usage endpoint
  • src/backend/services/task_execution_service.py — passes subscription_id through
  • src/backend/database.py — delegation method

Tests:

  • tests/test_subscription_usage.py — 8 tests (all passing)

Docs:

  • docs/memory/feature-flows/subscription-usage-tracking.md — new flow doc
  • docs/memory/feature-flows/subscription-management.md — updated with SUB-004
  • docs/memory/feature-flows/persistent-chat-tracking.md — schema updates

Test plan

  • GET /api/subscriptions/{id}/usage returns correct JSON shape
  • Rolling windows sum correctly across agents sharing a subscription
  • Returns zeros for a subscription with no chat history
  • Returns 404 for unknown subscription ID
  • Supports lookup by name as well as UUID
  • Existing chat functionality unaffected (subscription_id is nullable, no breaking changes)

🤖 Generated with Claude Code

Snapshots subscription_id at message-record time in chat_messages,
chat_sessions, and schedule_executions so usage can be aggregated
per subscription even after agents switch subscriptions (SUB-003
auto-switch). Adds GET /api/subscriptions/{id}/usage returning
rolling 5-hour and 7-day token/cost windows across all agents
sharing the subscription.

- Migration #31: adds subscription_id + output_tokens columns
- chat.py, schedules.py: accept subscription_id at record time
- subscriptions.py: get_subscription_usage() with dual rolling windows
- New endpoint: GET /api/subscriptions/{id}/usage (admin-only)
- 8 new tests in tests/test_subscription_usage.py

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant