Skip to content

feat: Subscription usage observability — 5h/7d usage dashboard, per-agent breakdown, and rate-limit event history #471

Closed

Description

@vybe

Summary

All the data needed to understand subscription pressure already exists in the database — token counts per message, cost per execution, rate-limit events per subscription — but none of it is surfaced in the UI. Operators have no way to see how close a subscription is to its 5-hour rolling limit, which agents are burning the most quota, or how frequently a given subscription has hit the wall. This issue wires the existing data to the front end.

Context

Triggered by subscriptions exhausting their limits significantly faster than expected (#435, SUB-003). Investigation confirmed:

  • subscription_id is already recorded on every chat_messages and schedule_executions row (wired in task_execution_service.py)
  • GET /api/subscriptions/{id}/usage already aggregates 5h and 7d token windows from that data (SUB-004)
  • subscription_rate_limit_events table is populated by SUB-003 on every 429, but is never exposed via API
  • Settings.vue subscription list calls none of these endpoints — all data is dark

Anthropic does not expose remaining budget or proximity-to-limit. The only signal available is the 429 itself and our own token consumption records.

Acceptance Criteria

Tier 1 — Surface existing data in Settings → Subscriptions

  • Each subscription card in Settings.vue shows a usage summary for the last 5 hours: input tokens, output tokens, cost (USD), message count
  • Each subscription card shows a 7-day aggregate for the same fields
  • Rate-limit event count (last 24h) is shown per subscription with a visual warning indicator if > 0
  • Data auto-refreshes on page load (no polling needed)

Tier 2 — Per-agent breakdown endpoint

  • New endpoint GET /api/subscriptions/{id}/usage/breakdown returns token consumption grouped by agent_name for the 5h and 7d windows
  • Settings.vue subscription cards show an expandable per-agent breakdown (collapsed by default)
  • Makes it clear which agent is consuming the most quota on a shared subscription

Tier 3 — Subscription pressure indicator on Agents list

  • Agents.vue shows a small warning badge on any agent card whose subscription had 1+ rate-limit events in the last 24h
  • Hovering the badge shows the subscription name and event count
  • Badge is sourced from a new lightweight endpoint (or piggy-backed on the existing agents list response)

Tier 4 — Auto-assign to available subscription

  • Settings → Subscriptions shows an "Auto-assign agents" button (or equivalent action per subscription card)
  • Clicking it reassigns all agents whose current subscription is rate-limited to the selected subscription (if that subscription has no active rate limit)
  • If no available subscription exists, the button is disabled with a tooltip explaining why
  • The action is scoped to subscriptions the user owns; admin can act across all

Technical Notes

Backend changes needed:

  • db/subscriptions.py: add get_rate_limit_event_count(subscription_id, hours=24) method
  • routers/subscriptions.py: new GET /{id}/usage/breakdown endpoint, new GET /{id}/rate-limit-events endpoint (or add count to existing /usage response)
  • routers/agents.py or agents list response: optionally include subscription_rate_limited: bool per agent
  • routers/subscriptions.py: new POST /{id}/auto-assign endpoint — reassigns agents from rate-limited subscriptions to this one; returns count of agents reassigned

Frontend changes needed:

  • Settings.vue: call /api/subscriptions/{id}/usage per subscription on load; render usage bars + rate-limit badge
  • Agents.vue: call new lightweight endpoint or extend existing agent list response to carry subscription_rate_limited flag
  • Settings.vue: "Auto-assign agents" button per subscription card; calls POST /{id}/auto-assign, shows toast with reassignment count

Related issues:

Constraints:

  • Anthropic does not expose remaining budget in the current 5-hour window; proximity-to-limit must be inferred from token consumption history + rate-limit event frequency
  • All DB queries must use existing indexes (idx_chat_messages_subscription, idx_executions_subscription, idx_rl_events_subscription)

Activity

  1. added
    priority-p2
    type-feature
    on Apr 23, 2026
  2. added
    complexity-medium
    theme-monetization
    on Jun 3, 2026
  3. added
    status-incubating
    on Jun 14, 2026
  4. added
    status-ready
    and removed
    status-incubating
    on Jul 28, 2026
  5. vybe commented on Jul 28, 2026

    @vybe
    ContributorAuthor

    Promoted incubating → ready (operator, 2026-07-28) — greenlit together with its dashboard-tile consumer, trinity-enterprise#259.

    Added scope — estimation-precision research (pre-build). Before the Tier 1 UI work starts, produce a short findings note (as a comment here) answering how precise a "remaining headroom" estimate can actually be, given that Anthropic exposes no remaining-budget API:

    1. Limit model per plan. Map the current limit structure for subscription plans (5-hour rolling window + weekly caps) and how ceilings differ by plan tier. Treat limits as variable, not constants — they are adjusted over time, so the design must not hardcode a limit table that silently rots. Evaluate auto-calibration instead: infer each subscription's effective ceiling from observed consumption at the moments rate-limit events fire.
    2. Model-mix weighting. Consumption burns the limit at different rates depending on which model is loaded (Opus-class vs Sonnet-class vs Haiku-class). A subscription shared by agents running different models needs usage normalized by model weighting — specify the scheme and confirm model identity is captured per message/execution row.
    3. Harvest runtime signals, not just 429s. The Claude Code runtime already emits structured rate-limit telemetry during execution (limit type such as five-hour, reset timestamp, overage status). Inventory exactly what the runtime exposes and persist it — a runtime-reported reset time is ground truth that beats any inference from our side of the wire.
    4. Runtime containment. Keep all estimation logic scoped to the Claude Code runtime behind a narrow adapter interface (runtime reports usage events + limit signals; the estimator consumes only the interface), so other runtimes can implement the same contract later without redesign. No Claude-Code-specific assumptions outside the adapter.

    Research acceptance: achievable precision stated honestly (what can and cannot be known), the weighting scheme, the calibration approach for shifting plan limits, and the adapter boundary — recorded on this issue before build.

  6. vybe commented on Aug 7, 2026

    @vybe
    ContributorAuthor

    Premise correction (2026-08-07): the Technical Notes constraint — "Anthropic does not expose remaining budget or proximity-to-limit; the only signal available is the 429 itself and our own token consumption records" — is no longer accurate, and was already contradicted by evidence in this repo when it was written:

    1. scripts/poc/test-subscription-usage.py (Feb 2026) validated GET https://api.anthropic.com/api/oauth/usage — with a subscription OAuth bearer token and header anthropic-beta: oauth-2025-04-20 it returns actual 5-hour and 7-day utilization percentages, per-model limits, and reset times. See the 2026-02-23 planning entry in docs/archive/memory/changelog.md (the poller + usage MCP tools planned there were never built). Caveats: undocumented endpoint, aggressively rate-limited without a proper User-Agent — treat as best-effort with the DB-derived aggregate as fallback.
    2. Claude Code now officially documents a rate_limits object piped to the statusline command — five_hour/seven_day → used_percentage + resets_at for subscription sessions: https://code.claude.com/docs/en/statusline.md

    Implication for this issue: the 5h/7d usage bars (Tier 1) and the pressure indicator (Tier 3) can show actual utilization sourced from Anthropic (with the existing DB-derived aggregate as honest fallback, source flagged), instead of only inferred consumption. Tier structure stays as specced; the data source upgrades.

  7. vybe commented on Aug 14, 2026

    @vybe
    ContributorAuthor

    Priority raised P2 → P1 (operator reconciliation, 2026-08-14).

    Rationale: this is the data layer under five separate consumers, and it is the only one of them nobody has scheduled — nothing in the v0.9 cut touches it.

    Consumers waiting on it:

    • trinity-enterprise#259 — Subscription-pressure grid tile (ready)
    • trinity-enterprise#351 — agent subscription self-awareness, self + fleet (ready)
    • trinity-enterprise#98 / #101 — fleet cost + token-consumption tiles (incubating)
    • trinity-enterprise#166 — enforceable per-agent spend cap (incubating, and blocked on this: the spend figure shown today is an API-price estimate, not real subscription cost, so a cap built on it would enforce a fiction)

    Operator, on the record 2026-08-14: "these are definitely going in… that's one of the problems I'm running into all the time."

    Sequencing note: the grid chassis (trinity-enterprise#325) and bucketed timeline rollups (#326) ship in this release, which makes #259 a small build immediately after the cut — this data layer is the thing standing in front of it.

    Premise correction from 2026-08-07 stands unchanged and is a prerequisite read before build.

  8. added
    priority-p1
    and removed
    priority-p2
    on Aug 14, 2026
  9. vybe commented on Aug 17, 2026

    @vybe
    ContributorAuthor

    Adding the concrete approach, prompted by #2170, which attempted this and took a wrong turn worth recording so nobody repeats it.

    Don't build on a provider allowance signal. #2170 shipped a discriminated subscription/api/unconfigured contract whose available arm requires an allowance percent, window, and reset from Anthropic. That signal doesn't exist under setup-token auth, so the arm is unreachable and every subscription agent renders usage unavailable permanently. This issue already established that in April. Treat the 429 and our own consumption records as the only inputs.

    Step 0 — verify the cost figure under subscription auth. For Claude Code, cost is the runtime's native total_cost_usd (token counts at API prices), not our estimate. Confirm it's actually populated when auth is an OAuth subscription token rather than an API key:

    SELECT agent_name, cost FROM schedule_executions
    WHERE subscription_id IS NOT NULL AND cost IS NOT NULL LIMIT 20;

    on an instance with real subscription traffic. Everything below assumes non-zero; if it's zeros, drop to token counts only.

    Tier 0 (new) — relabel, don't delete. The live defect is semantic, not numeric: a bare $ on a subscription agent reads as a bill. Keep the sparkline, today's cost, and the trend; present them as API-equivalent (≈$3.12 API-equivalent) with a billing-mode badge naming the funding subscription. Agents on a platform/per-agent API key keep plain metered-cost semantics. This is a labeling change on existing surfaces — no contract change, no aggregation change, and it leaves enterprise #166's spend-cap foundation intact.

    Tiers 1–3 as already specified. Wire the dark data: /usage 5h/7d windows into Settings → Subscriptions, a rate-limit event count with a warning indicator, then the per-agent breakdown and the pressure badge. Proximity-to-limit is inferred from our own consumption plus 429 frequency — stated as inference, never as a provider reading.

    Out of scope here: the fleet roster (get_all_agent_metadata) also serves shared non-owner users, so putting subscription names on it is an access decision to make deliberately, not a side effect of a display change. If a provider-side signal ever appears, #855 is the spike that establishes it and the contract gets designed then, against a real payload.

  10. removed
    status-ready
    on Aug 19, 2026
  11. 9 remaining items

  12. removed
    status-in-progress
    on Aug 19, 2026
  13. added 2 commits that reference this issue on Sep 4, 2026
  14. removed
    status-in-dev
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-mediumpriority-p1theme-monetizationtype-feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions