Answer the first round: one clock read, the limit's message and time zone on stderr, plainer help and comments

This commit is contained in:
2026-10-02 22:49:39 +02:00
parent e7ff869398
commit 5a481d3ebf
9 changed files with 36 additions and 27 deletions
+8 -7
View File
@@ -17,9 +17,10 @@ def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effec
while True:
result = attempt()
if isinstance(result, Limited):
due = retry_due(result, effects.now())
print(f"the {who} hit a usage limit; retrying at {time.strftime('%Y-%m-%d %H:%M', time.localtime(due))}", file=sys.stderr, flush=True)
effects.sleep(due - effects.now())
now = effects.now()
wait = limit_wait(result, now)
print(f"the {who} hit a usage limit ({result.said}); retrying at {time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(now + wait))}", file=sys.stderr, flush=True)
effects.sleep(wait)
continue
if not isinstance(result, Failure):
return result
@@ -30,8 +31,8 @@ def retried(who: str, attempt: typing.Callable[[], T | Failure | Limited], effec
effects.sleep(60)
def retry_due(limited: Limited, now: float) -> float:
# A retry before the reset fails (https://platform.claude.com/docs/en/api/rate-limits), so polling sooner only spends requests.
def limit_wait(limited: Limited, now: float) -> float:
# The limit rejects every request until it resets, so wait for a known reset and poll rarely for an unknown one.
if limited.resets_at is not None and limited.resets_at + PAST_RESET > now:
return limited.resets_at + PAST_RESET
return now + UNKNOWN_RESET_POLL
return limited.resets_at + PAST_RESET - now
return UNKNOWN_RESET_POLL