Skip to content

The agent that grows with you

fix(free-tier): a short 429 cools down for the wait it names, not the hour

alt-glitchd5bbdbe3eb61
diff --git a/agent/nous_rate_guard.py b/agent/nous_rate_guard.py
index d7095a38b3b..0ceaf36654e 100644
--- a/agent/nous_rate_guard.py
+++ b/agent/nous_rate_guard.py
@@ -64,18 +64,18 @@ def record_nous_rate_limit(
) -> None:
"""Record that Nous Portal is rate-limited in the shared state file.
- Reset time comes from headers, then ``error_context["reset_at"]`` (body
- parsing), then ``default_cooldown``.
+ Reset time comes from ``error_context["reset_at"]`` (the 429 body's own wait), then headers, then
+ ``default_cooldown``. The body wins: a welcome-tier refusal says when to retry (410 s), while the
+ hourly bucket's reset header is that window's end (about an hour) even with requests left in it.
"""
now = time.time()
reset_at = None
- header_seconds = _parse_reset_seconds(headers)
- if header_seconds is not None:
- reset_at = now + header_seconds
- if reset_at is None and isinstance(error_context, dict):
+ if isinstance(error_context, dict):
ctx_reset = error_context.get("reset_at")
if isinstance(ctx_reset, (int, float)) and ctx_reset > now:
reset_at = float(ctx_reset)
+ if reset_at is None and (header_seconds := _parse_reset_seconds(headers)) is not None:
+ reset_at = now + header_seconds
if reset_at is None:
reset_at = now + default_cooldown
diff --git a/tests/agent/test_nous_rate_guard.py b/tests/agent/test_nous_rate_guard.py
index f96d4bc6e07..4ecdea87faf 100644
--- a/tests/agent/test_nous_rate_guard.py
+++ b/tests/agent/test_nous_rate_guard.py
@@ -52,6 +52,21 @@ class TestRecordNousRateLimit:
assert state["reset_at"] == pytest.approx(future_reset, abs=1)
+ def test_the_429_bodys_wait_outranks_the_hourly_window_end(self, rate_guard_env):
+ # A welcome-tier fair-share 429 said retry after 410 s; the hourly bucket's reset header (its
+ # window end, ~1 h) turned that into an hour-long block on the next turns.
+ from agent.nous_rate_guard import record_nous_rate_limit, _state_path
+
+ record_nous_rate_limit(
+ headers={"x-ratelimit-remaining-requests-1h": "37", "x-ratelimit-reset-requests-1h": "3594"},
+ error_context={"reset_at": time.time() + 410},
+ anonymous=True,
+ )
+
+ with open(_state_path(anonymous=True), encoding="utf-8-sig") as f:
+ state = json.load(f)
+ assert state["reset_seconds"] == pytest.approx(410, abs=2)
+
def test_custom_default_cooldown(self, rate_guard_env):
from agent.nous_rate_guard import record_nous_rate_limit, _state_path