Install
consolepip install backoff-headers
API
pythonfrom backoff_headers import (
format_retry_after, parse_rate_limit, parse_retry_after, wait_time,
)
parse_retry_after("120") # 120.0
parse_retry_after("Wed, 21 Oct 2015 07:28:00 GMT") # seconds until then, 0.0 if past
limit = parse_rate_limit(response.headers)
limit.limit, limit.remaining, limit.window, limit.policy
limit.exhausted # remaining == 0
limit.seconds_until_reset()
wait_time(headers, cap=60, jitter=2.0) # never longer than 60s, spread by up to 2s
format_retry_after(1.2) # "2", rounded up so you never retry early
reset_style on parse_rate_limit is "auto" (an implausibly large reset is treated as a timestamp), "delta" or "epoch" when you know what your server sends. now is accepted everywhere a clock is needed, which is what makes the behaviour testable.
Both header families are read case-insensitively, and RFC 9331 wins when a server sends both.
Design notes
- Precedence:
Retry-After, then an exhausted quota's reset, thendefault. jitteraddsuniform(0, jitter), so a fleet of clients does not retry in lockstep.capis applied after the jitter, so a cap is never exceeded.- Nothing sleeps, nothing retries: the caller owns the loop and the clock.
Development
consolepython -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest
The suite covers every line of the package (pytest --cov --cov-branch), and the examples in the docstrings run as tests through --doctest-modules.
License
MIT.
Other libraries here
One problem each, standard library only, no runtime dependencies.
- argrepair — turn a bad tool call into a message the model can act on
- columncheck — find the CSV defect before the pipeline does
- dir-digest — one hash for a directory tree, identical on every OS
- docanchor — catch every broken heading anchor in your docs
- envfile-diff — diff two .env files without printing the secrets
- fsync-replace — replace a file so a crash cannot leave it half-written
- header-date — HTTP dates parsed strictly, formatted correctly
- junit-merge — merge JUnit XML from every job into one honest report
- npm-semver — npm semver ranges, parsed the way npm parses them
- outputcap — keep a huge tool result out of the context window
- parse-size — byte sizes in and out, MB and MiB never confused
- path-containment — keep untrusted paths inside their directory
- redactscope — stop secrets in tool results before they reach the model
- runbudget — a per-run cost cap that warns before it stops
- testflakes — rank your flaky tests from the history you already have
- tool-loop-guard — catch an agent repeating itself while it is still cheap
- toolcall-ledger — a hash-chained record of every tool call
- toolcircuit — stop retrying a tool that is down
- toolquota — client-side rate limits your agent loop can wait on
- wheeldeps — read a wheel's dependencies offline, markers included
- zip-deterministic — byte-identical archives, build after build