Brain Docs

Reference

Python SDKInstallationHTTP clientWire clientSync vs async
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
Python SDKRetries

Retries

Both retry layers — the HTTP client's automatic policy with its idempotent-only verb list, and the wire tier's with_retry combinator.

PURPOSE

The HTTP client retries for you, on a list of verbs it considers safe to repeat. The wire client does not retry at all — it ships a combinator you wrap a call in. This page documents both, and the server hint that overrides your backoff.

HTTP tier

BrainHttpClient applies an HttpRetryPolicy automatically.

ParameterTypeRequired
max_attemptsintoptional
Total attempts including the first. Defaults to 3; 1 disables retry.
base_delayfloatoptional
First backoff in seconds; doubles each attempt. Defaults to 0.1.
max_delayfloatoptional
Upper bound on any single backoff — and it also caps a server Retry-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

ConditionRetried
status == 0 — transport failure or timeout, never reached the edgeYes
503 — service unavailableYes
429 — rate limitedYes
Every other statusNo, terminal

A 4xx is a client verdict. 429 is the exception, because it is a "slow down" rather than a refusal.

Not every verb is retried, and the choices are deliberate

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:

ParameterTypeRequired
with_retry(op, policy=None)Trequired

Call op until it succeeds or the policy is exhausted. Retries only what is_retryable approves.

RetryPolicydataclassoptional

max_attempts, base_delay, max_delay — in seconds.

sleep_before_retry(policy, attempt, exc)float | Noneoptional

The 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.

See also
ErrorsSync vs asyncHTTP client

Was this page helpful?

Errors

Both taxonomies — the wire BrainError hierarchy with its typed server subclasses, and the HTTP tier's BrainHttpError.

Pagination

Cursor-based enumeration on the HTTP tier, frame-based streaming on the wire, and the terminator each one uses.

On this page

HTTP tierWhat is retriedWire tierJitterIdempotency on the wire
Suggest an editOpen in Playground