Skip to content

bug(subscriptions): auto-switch (SUB-003) does not fire on monthly usage cap exhaustion #435

Closed as not planned

Description

@vybe

Summary

When a Claude Max subscription hits its monthly usage cap, the SUB-003 auto-switch mechanism does not activate — even when alternative subscriptions are registered and have remaining capacity. Agents assigned to the exhausted subscription keep failing with 429s on every scheduled task until the cap resets, with no automatic remediation.

Component

Backend / Subscription Management (SUB-003)

Priority

P2 — feature impaired, manual workaround available (reassign agents to a different subscription via Settings)

Error

WARNING: Claude Code error detected: type=rate_limit,
  message=You're out of extra usage · resets Apr 23, 3pm (America/New_York)
ERROR: [Headless Task] Rate limit: Subscription usage limit:
  You're out of extra usage · resets Apr 23, 3pm (America/New_York).
  To resolve: (1) wait for the usage reset, (2) set an ANTHROPIC_API_KEY
  on this agent for pay-per-use billing, or (3) assign a different
  subscription token in Settings → Subscriptions.

Agent container returns 429 Too Many Requests to backend on POST /api/task.

Observed Behavior

  • Subscription A hits monthly usage cap
  • All agents on Subscription A begin failing in ~2s on every execution
  • Two other subscriptions (B and C) are registered with remaining capacity
  • No auto-switch fires; agents remain stuck on exhausted Subscription A
  • No subscription_rate_limit_events activity visible — backend appears to not detect the 429s returned from agent containers as subscription rate-limit events

Expected Behavior

SUB-003 should detect repeated type=rate_limit failures from an agent's task endpoint, recognize the "out of extra usage" pattern as a usage-cap exhaustion, and either:

  1. Automatically reassign the agent to another registered subscription with capacity, or
  2. Surface the situation prominently (operator queue / notification) so it can be acted on immediately

Root Cause (Hypothesis)

The 429 response travels from the agent container back to the backend, not from the Anthropic API directly to the backend:

Scheduler → backend (/api/internal/execute-task)
         → agent (/api/task)
         → Claude Code subprocess → [rate_limit detected inside agent]
         ← agent returns 429 ←
         ← backend marks execution failed ←

SUB-003's rate-limit event tracking likely monitors Anthropic API errors at the point where the backend calls the Claude API directly. In the subscription-auth flow, Claude is invoked inside the agent container via CLAUDE_CODE_OAUTH_TOKEN. The backend never sees a direct Anthropic 429 — it only sees the agent container's 429, which may not be parsed to extract the subscription rate-limit signal and write to subscription_rate_limit_events.

Additionally, "out of extra usage" (monthly hard cap) is semantically distinct from "too many requests" (transient throttle). Even if the event IS detected, the auto-switch logic may only be designed to handle transient throttling, not hard-cap exhaustion.

Reproduction Steps

  1. Register two or more Claude Max subscriptions (Subscription A and Subscription B)
  2. Assign several agents to Subscription A
  3. Run heavy workloads on those agents until Subscription A exhausts its monthly usage cap
  4. Observe: all agents on Subscription A begin failing immediately on every execution
  5. Observe: SUB-003 does not reassign agents to Subscription B
  6. Check subscription_rate_limit_events table — likely empty or not updated for these failures

Suggested Fix

Two complementary changes:

1. Detect agent-container 429s as subscription rate-limit events

In the backend's task execution handler, when an agent returns 429, parse the response body for type=rate_limit and write to subscription_rate_limit_events with the subscription ID of the agent's current subscription.

2. Distinguish usage exhaustion from transient throttling

The "out of extra usage · resets [date]" message is a hard cap, not a transient throttle. For this case, auto-switch should trigger immediately (after 1–2 failures) rather than waiting for the repeated-429 threshold used for transient throttles. A transient throttle may self-resolve; a monthly cap will not.

# Rough sketch — in task execution error handler:
if response.status_code == 429:
    body = response.json()
    if body.get("type") == "rate_limit":
        message = body.get("message", "")
        is_hard_cap = "out of extra usage" in message or "resets" in message
        record_subscription_rate_limit_event(
            subscription_id=agent.subscription_id,
            error_type="usage_exhaustion" if is_hard_cap else "transient",
        )
        if is_hard_cap:
            trigger_auto_switch(agent)  # don't wait for threshold

Environment

  • Trinity version: 0027b8b
  • OS: Ubuntu 22.04

Related

  • SUB-003 feature: subscription auto-switch on rate limits
  • subscription_rate_limit_events table
  • src/backend/services/task_execution_service.py — where agent 429s are handled
  • Related feature: SUB-002 subscription assignment

Activity

  1. vybe commented on Apr 21, 2026

    @vybe
    ContributorAuthor

    Validation — symptoms real, hypothesis partially wrong

    Confirmed the bug via code walkthrough. The observed behavior (empty subscription_rate_limit_events, no auto-switch) is accurate, but the root-cause hypothesis needs correction.

    What the issue gets wrong

    The backend never sees a direct Anthropic 429 — it only sees the agent container's 429, which may not be parsed...

    This path is already wired:

    • Agent detects rate-limit patterns (including "out of extra usage" and "resets ") and raises HTTPException(status_code=429) — docker/base-image/agent_server/services/claude_code.py:716-729 (_is_rate_limit_message), used at lines 659-665 and 1179-1186.
    • Backend catches the agent 429 and calls the SUB-003 handler — src/backend/services/task_execution_service.py:530-537.

    So the suggested fix sketch (parse response body for type=rate_limit) is unnecessary — that detection already works.

    Actual root cause (two real bugs)

    1. Events aren't recorded when auto-switch is disabled. services/subscription_auto_switch.py:36-38 early-returns on the auto_switch_subscriptions setting before record_rate_limit_event() runs. Setting defaults to "false":

    enabled = db.get_setting_value("auto_switch_subscriptions", default="false") == "true"
    if not enabled:
        return None  # ← event never recorded

    This explains the empty subscription_rate_limit_events table exactly: the feature is off, so nothing gets recorded — not even for visibility or operator notifications.

    2. No hard-cap fast path. handle_rate_limit_error requires 2+ events in 2 hours before switching (subscription_auto_switch.py:52-57). "Out of extra usage · resets [date]" is a monthly hard cap — it will not self-resolve. Current code treats it like a transient throttle. Issue's suggestion to fast-path this case is correct.

    Bonus issue (not in report)

    select_best_alternative_subscription (db/subscriptions.py:572-606) only excludes alternatives that have been rate-limited in the last 2h — it doesn't consult get_subscription_usage (SUB-004). A switch could land on another near-exhausted subscription.

    Refined scope for the fix

    Change Where
    Move record_rate_limit_event() before the enabled check subscription_auto_switch.py:36
    Detect hard-cap pattern (out of extra usage, resets ) and fast-path switch after 1 event subscription_auto_switch.py:52
    Always emit high-priority notification on hard-cap, regardless of switch outcome subscription_auto_switch.py
    (Optional) Consult get_subscription_usage when picking alternative db/subscriptions.py:572

    Proceeding with this scope.

  2. added
    status-in-progress
    and removed on Apr 21, 2026
  3. vybe commented on Apr 21, 2026

    @vybe
    ContributorAuthor

    Closing as invalid — feature flag was disabled

    After the code walkthrough in the prior comment, confirmed with the reporter that the auto_switch_subscriptions platform setting was off. With the flag disabled, handle_rate_limit_error returns at services/subscription_auto_switch.py:36-38 before any event is recorded or switch is attempted — which exactly matches the symptoms (empty subscription_rate_limit_events, no auto-switch, agents stuck on exhausted subscription).

    Enabling the setting via Settings → Subscriptions (or the admin endpoint PUT /api/subscriptions/auto-switch) restores the intended behavior.

    The two observations that could still be valid product improvements (record events for visibility even when flag is off; fast-path hard-cap exhaustion without waiting for 2 events) are intentional scope for a future refinement if desired — not a bug. Closing this issue as invalid.

  4. added
    invalid
    and removed
    type-bug
    status-in-progress
    on Apr 21, 2026
  5. added
    complexity-low
    on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowinvalidpriority-p2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions