Repository navigation
feat(subscriptions): track per-subscription token usage across agents - #240
Merged
Merged
Conversation
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]>
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
subscription_idat message-record time inchat_messages,chat_sessions, andschedule_executions— survives SUB-003 auto-switch reassignmentsoutput_tokenscolumn tochat_messagesfor complete token accountingGET /api/subscriptions/{id}/usageendpoint returns rolling 5-hour and 7-day usage windows (tokens, cost, message count, active agents, rate-limit events)ALTER TABLE ADD COLUMNguardsMotivation
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 + indexessrc/backend/db/migrations.py— migration System Manifest UI #31src/backend/db/chat.py—add_chat_message+get_or_create_chat_sessionacceptsubscription_id,output_tokenssrc/backend/db/schedules.py— execution records acceptsubscription_idsrc/backend/db/subscriptions.py—get_subscription_usage()dual-window querysrc/backend/db_models.py—SubscriptionUsageWindow,SubscriptionUsagemodelssrc/backend/routers/chat.py— snapshots subscription at record timesrc/backend/routers/subscriptions.py— new/usageendpointsrc/backend/services/task_execution_service.py— passes subscription_id throughsrc/backend/database.py— delegation methodTests:
tests/test_subscription_usage.py— 8 tests (all passing)Docs:
docs/memory/feature-flows/subscription-usage-tracking.md— new flow docdocs/memory/feature-flows/subscription-management.md— updated with SUB-004docs/memory/feature-flows/persistent-chat-tracking.md— schema updatesTest plan
GET /api/subscriptions/{id}/usagereturns correct JSON shape🤖 Generated with Claude Code