Skip to content
v0.18.0stable

Retries, timeouts, and idempotency

The official OpenAI and Anthropic SDKs retry some failures automatically. Behind a gateway, one visible application call can already contain multiple Provider Attempts, so an additional SDK retry can multiply cost, concurrency, and audit records. The examples use max_retries=0 or maxRetries: 0; add an explicit application policy only after classifying Halro errors.

client = OpenAI(base_url="...", api_key=..., timeout=60.0, max_retries=0)
const client = new OpenAI({ baseURL: "...", apiKey: ..., timeout: 60_000, maxRetries: 0 });
When a retry is safe
ConditionRetry?Meaning
429 per-source / Project limitYesRead Retry-After when present, then bounded backoff
429 provider_rate_limitYes, with a boundFor synchronous Chat, retry the same body and key after the indicated wait when all dispatched attempts definitively refused execution
429 Provider concurrencyYesHonor parseable Retry-After; otherwise application backoff
429 Deployment concurrencyYesNo Retry-After; use bounded application backoff
503 provider_unavailableYesNo healthy Deployment currently exists
503 all_candidates_suspendedDepends on reasonBack off for timed windows; invalid credentials have no Retry-After and need an operator
503 accounting_capacity_exhaustedYesHonor Retry-After while admitted work settles
Upstream 5xxCautiouslyMay have reached Provider; treat as ambiguous and do not assume gateway fallback
403 token_guard_blockedNot immediatelyCost threshold or temporary block; change request or wait/administer
403 model_not_allowed / 404 model_not_foundNoConfiguration problem; contact administrator
Rejected 400 fieldNoRequest is invalid; change it
Ambiguous resultCautiouslySee next section

Indeterminate failures are not retried by the gateway

Section titled “Indeterminate failures are not retried by the gateway”

If the Provider may have accepted work but Halro cannot prove the result, a second automatic call could double-charge or duplicate side effects. Halro settles conservatively and returns an explicit failure. Record the request ID, inspect the Attempt, and let the application decide whether a new business operation is acceptable.

Chat and Embeddings: duplicate prevention without answer replay

Section titled “Chat and Embeddings: duplicate prevention without answer replay”

Since v0.9.0, POST /v1/chat/completions (including streaming) and POST /v1/embeddings accept Idempotency-Key. Save and reuse the same key, body, and Run attribution for one business operation. This example creates the first request; do not generate a new key when retrying it:

Terminal window
IDEMPOTENCY_KEY="$(uuidgen)"
curl https://halro.example.com/v1/chat/completions \
-H "Authorization: Bearer $HALRO_GATEWAY_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $IDEMPOTENCY_KEY" \
-d '{"model":"chat","max_tokens":256,"messages":[{"role":"user","content":"Hello"}]}'
Chat and Embeddings: duplicate prevention without answer replay
Duplicate stateResponseHandling
Same key, different request body409 idempotency_conflictInspect the business key; do not hide conflicts by replacing it
Still running409 idempotency_in_progressWait and retry the same key under a bound
Outcome unknown409 idempotency_outcome_unknownReconcile the operation before deciding whether to risk a separate execution and charge
Completed409 idempotency_completedThe original answer is not replayed; save the first response on the client
Key cannot be checked or stored safely503, such as resource_store_unavailableRepair storage and keep the same key; do not treat it as free

A streaming duplicate is refused as an HTTP error before opening the stream. This provides at-most-once upstream execution while the record remains valid, not answer replay. Records store no request/response body and expire after 24 hours in every state; reuse after expiry can cause a new call. Keys are scoped by Project; Chat and Embeddings share the inference_call namespace. Keys must contain 1–128 visible ASCII characters without whitespace; invalid values return 400 invalid_idempotency_key. Requests without a key behave as before.

An uncertain result after upstream dispatch keeps the key protected. Starting in v0.16.0, synchronous Chat can reuse the same key, body, and Run attribution after a definitive rate-limit refusal once settlement and the refusal are recorded. A successful reclaim starts a new 24-hour protection window. An initial request rejected before dispatch for capability, token, budget, or attempt reservation releases its key. Synchronous Responses and Messages do not provide this inference idempotency contract. To retrieve an answer later, use explicitly enabled background: true Responses.

Use a stable key for resource creates: Async (POST /v1/async/invocations), Files (POST /v1/files), and Batches (POST /v1/batches).

Terminal window
curl https://halro.example.com/v1/files \
-H "Authorization: Bearer $HALRO_GATEWAY_KEY" \
-H "Halro-Route: batch-files" \
-H "Idempotency-Key: $(uuidgen)" \
-F purpose=batch -F file=@input.jsonl

Synchronous Responses and Messages do not use this resource-create contract. Chat and Embeddings use the duplicate-prevention contract above. background: true Responses are the exception: Idempotency-Key is optional but strongly recommended. The same Key, body, and X-Halro-Run-ID attribution return the same record; changing any of them while reusing the Key returns 409.

Idempotency does not make an operation free: the original accepted Provider Attempt can still be billed. It prevents duplicate Halro resources and gives the caller a stable result to retrieve.

Source and Project rate limits can calculate a wait window and commonly return Retry-After. Deployment concurrency may not. When compatible synchronous generation targets are temporarily rate-limited, Halro uses the earliest known recovery time. A fresh refusal without a usable hint can omit the header. Clients must handle the header being absent.

Set the client timeout longer than the expected route timeout so Halro can return its classified result. A client disconnect does not prove the Provider stopped. For streaming, apply the timeout to the interval between events rather than the complete response, or every long answer eventually times out. For long work, prefer bounded deferred Responses where supported, monitor their 24-hour TTL, and retain the final request and accounting evidence.

See Scenario 3 for combining Project/Run budgets, client retries, and Halro Attempt limits.