Skip to content

feat: Cooperative Rate Limiting & Predictive Circuit Breaker for Multi-Agent Deployments #515

Closed

Description

@tamirdresher

Feature: Cooperative Rate Limiting & Predictive Circuit Breaker for Multi-Agent Deployments

Problem

Issue #450 added a circuit breaker template for model rate limiting — great foundation. But in production with multiple Ralphs across machines (or pods on K8s), single-instance circuit breakers aren't enough. Each Ralph independently hits API limits, causing cascading failures across the fleet.

The core problems at scale:

  1. No coordination — 5 Ralphs each think they have full API quota
  2. Thundering herd — All Ralphs retry simultaneously after rate limit resets
  3. Priority inversion — Low-priority work exhausts quota before critical work runs
  4. Reactive only — Circuit opens AFTER 429, wasting the failed request

Proposed Solution: 6-Pattern Rate Limiting Architecture

1. Traffic Light (RAAS) — GREEN/AMBER/RED throttling from response headers:

  • GREEN (>20% remaining): Normal operation
  • AMBER (5-20%): Only P0 agents proceed
  • RED (<5%): Block all except emergency P0

2. Cooperative Token Pool (CMARP) — Shared ~/.squad/rate-pool.json:

  • P0 agents (Lead) get 40% of quota
  • P1 agents (specialists) get 35%
  • P2 agents (Ralph, Scribe) get 25%
  • Stale leases auto-recovered after 5 minutes

3. Predictive Circuit Breaker (PCB) — Opens BEFORE 429:

  • Tracks last 10 (timestamp, remaining) pairs from response headers
  • Linear regression predicts ETA to quota exhaustion
  • Opens circuit when ETA < 120 seconds
  • Prevents the 429 from ever happening

4. Priority Retry Windows (PWJG) — Non-overlapping jitter:

  • P0: 500ms–5s retry window
  • P1: 2s–30s retry window
  • P2: 5s–60s retry window
  • Higher priority agents recover first

5. Resource Epoch Tracker (RET) — Heartbeat-leased allocations:

  • Each agent registers a 5-minute lease
  • Crashed agent allocations auto-recycle

6. Cascade Dependency Detector (CDD) — Backpressure from downstream failures

Implementation Available

I have all 6 patterns implemented and running in production:

  • rate-limit-manager.ps1 (200+ lines) — Cooperative rate pooling
  • Predictive circuit breaker in ralph-watch.ps1
  • KEDA integration via keda-copilot-scaler (Go, 51 tests) — scales pods to zero when rate limited
  • Running with 7+ concurrent agents on AKS

Builds on top of the existing ralph-circuit-breaker.md template from #450/#451.

References

Activity

  1. tamirdresher commented on Mar 22, 2026

    @tamirdresher
    CollaboratorAuthor

    Hey Brady! 👋

    Submitted 4 PRs from our production Squad deployment — these are patterns we've been running for 3+ months on AKS:

    1. feat: Cooperative Rate Limiting & Predictive Circuit Breaker (#515) #518 — Rate limiting SDK module (PredictiveCircuitBreaker, traffic light, cooperative quota)
    2. feat: Wire rate limiting & circuit breaker into watch command (#515) #522 — Wires feat: Cooperative Rate Limiting & Predictive Circuit Breaker (#515) #518 into the \watch\ command so Ralph backs off before hitting 429s
    3. feat: Machine Capability Discovery & needs:* Label Routing (#514) #520 — Machine capability discovery +
      eeds:*\ label routing (so Ralph only picks issues the machine can handle)
    4. docs: KEDA External Scaler Template for Agent Autoscaling (#516) #519 — KEDA external scaler docs (autoscale agents based on GitHub issue queue depth)

    The circuit breaker in #518/#522 is the big one — it predicts quota exhaustion using linear regression on rate limit headers and opens the circuit before you get a 429. Exponential backoff, half-open recovery, state persisted across restarts. Zero missed rate limits in production.

    Happy to walk through any of it or adjust based on your feedback!

  2. added a commit that references this issue on Mar 23, 2026
  3. added a commit that references this issue on Mar 26, 2026
    9d2e81d
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions