Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AMAP ML SkillClaw Retry After Header Handling

From Leeroopedia
Knowledge Sources
Domains Optimization, Debugging, LLMs
Last Updated 2026-10-22 00:00 GMT

Overview

Respects the HTTP `Retry-After` header for rate-limited (429) responses, supporting both delta-seconds and HTTP-date formats, with quota-detection to skip retries on account-level limits.

Description

When an upstream LLM returns a 429 (rate limited) or other retryable status, `forward_chat` checks the `Retry-After` header. The `retry_after_seconds` function parses both numeric seconds (`float(raw)`) and HTTP-date format (`parsedate_to_datetime`). If the retry delay exceeds 30 seconds or the response body contains "usage limit" or error code `"1308"`, the system treats it as a quota limit rather than a transient rate limit — it skips retrying the same account and immediately attempts a fallback endpoint instead. This avoids wasting time waiting for a quota window that may be hours long.

Usage

Use this heuristic when LLM API endpoints return 429 rate-limit responses with a `Retry-After` header. It is applied automatically in `forward_chat` during the failover loop.

The Insight (Rule of Thumb)

  • Action: Parse `Retry-After` header; if delay > 30s or body indicates quota limit, switch to fallback endpoint immediately instead of waiting.
  • Value: Quota detection: `delay > 30 OR "usage limit" in response.text.lower() OR '"1308"' in response.text`.
  • Trade-off: May miss a legitimately short retry window if the header overestimates, but avoids multi-minute waits on account-level quotas.
  • Compatibility: Supports both numeric seconds and HTTP-date `Retry-After` formats per RFC 7231.

Reasoning

Rate-limit responses come in two flavors: (1) transient throttling (wait a few seconds and retry) and (2) account-level quota exhaustion (wait minutes to hours). The `Retry-After` header value distinguishes them: short delays are transient, long delays indicate quota. Waiting 30+ seconds for a quota reset wastes the user's time when a fallback endpoint is available. The `"1308"` error code and "usage limit" text are provider-specific signals that the limit is account-wide, not a burst throttle. By detecting these signals, the system fails over to a different endpoint immediately.

Code Evidence

Retry-After parsing from `skillclaw/chat_upstream.py:88-99`:

def retry_after_seconds(response):
    raw = response.headers.get("retry-after", "")
    try:
        return max(0.0, float(raw))
    except ValueError:
        try:
            stamp = parsedate_to_datetime(raw)
            if stamp.tzinfo is None:
                stamp = stamp.replace(tzinfo=timezone.utc)
            return max(0.0, (stamp - datetime.now(timezone.utc)).total_seconds())
        except (TypeError, ValueError, OverflowError):
            return None

Quota detection from `skillclaw/chat_upstream.py:138-147`:

delay = retry_after_seconds(response)
quota = last_status == 429 and (
    (delay is not None and delay > 30)
    or "usage limit" in response.text.lower()
    or '"1308"' in response.text
)
if index + 1 >= len(endpoints) and quota:
    break

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment