Retries
Both retry layers — the HTTP client's automatic policy with its idempotent-only verb list, and the wire tier's with_retry combinator.
HTTP tier
BrainHttpClient applies an HttpRetryPolicy automatically.
max_attemptsintoptional3; 1 disables retry.base_delayfloatoptional0.1.max_delayfloatoptionalRetry-After. Defaults to 2.0.Two helpers on the class: HttpRetryPolicy.none() for a single attempt, and
HttpRetryPolicy.is_retryable_status(status) for the status test on its own.
from brain_db_sdk.http.retry import HttpRetryPolicy
brain = BrainHttpClient(api_key=key, retry=HttpRetryPolicy(max_attempts=5, max_delay=10.0))What is retried
| Condition | Retried |
|---|---|
status == 0 — transport failure or timeout, never reached the edge | Yes |
503 — service unavailable | Yes |
429 — rate limited | Yes |
| Every other status | No, terminal |
A 4xx is a client verdict. 429 is the exception, because it is a "slow
down" rather than a refusal.
Only idempotent verbs retry — encode (it carries a stable server-side
request id), whoami and capabilities. recall, forget, link,
unlink, plan and reason stay single-shot.
putLlmCredential is excluded
because a PUT that may have landed is not safe to repeat blind against a
live credential, and
subscribe because a retry would reopen the
feed at the original position and re-deliver everything already seen.
A server Retry-After header overrides the computed backoff, capped at
max_delay.
Wire tier
The wire client does not retry. brain_db_sdk.retry provides the pieces:
with_retry(op, policy=None)TrequiredCall op until it succeeds or the policy is exhausted. Retries only what
is_retryable approves.
RetryPolicydataclassoptionalmax_attempts, base_delay, max_delay — in seconds.
sleep_before_retry(policy, attempt, exc)float | NoneoptionalThe delay for an attempt, or None when there is nothing to wait for.
Use it if you are writing your own loop.
from brain_db_sdk import EncodeBuilder
from brain_db_sdk.retry import RetryPolicy, with_retry
request = EncodeBuilder("...").build() # build ONCE
res = with_retry(lambda: client.encode(request), RetryPolicy(max_attempts=5))Two different with_retry functions
brain_db_sdk.retry.with_retry is synchronous; brain_db_sdk.aio.with_retry
is a coroutine. The synchronous one is re-exported from the package root,
so from brain_db_sdk import with_retry gives you that one. The asyncio
twin is reachable only through brain_db_sdk.aio, precisely so the two
cannot be confused — await a value the sync helper already resolved and you
get a TypeError, not a hang, but the import is the place to get it right.
Jitter
The server's retry hint is a floor, not a target: the combinator waits a random interval between that floor and the computed backoff, so a fleet told to retry after the same hint does not return as one wave. If you write your own loop, add the same jitter — synchronised retries are how a recovering shard is knocked back over.
Idempotency on the wire
Retrying a wire write is only safe because the request carries a request_id.
The builders generate a fresh one on every .build(), so re-building is not
retrying — it produces a new id and the server sees a second, distinct write.
Build once, outside the retry loop, exactly as above.
Replaying the same request_id with different parameters is
IdempotencyConflict.
Was this page helpful?