Retries, timeouts, and idempotency
Disable automatic SDK retries first
Section titled “Disable automatic SDK retries first”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
Section titled “When a retry is safe”| Condition | Retry? | Meaning |
|---|---|---|
| 429 per-source / Project limit | Yes | Read Retry-After when present, then bounded backoff |
429 provider_rate_limit | Yes, with a bound | For synchronous Chat, retry the same body and key after the indicated wait when all dispatched attempts definitively refused execution |
| 429 Provider concurrency | Yes | Honor parseable Retry-After; otherwise application backoff |
| 429 Deployment concurrency | Yes | No Retry-After; use bounded application backoff |
503 provider_unavailable | Yes | No healthy Deployment currently exists |
503 all_candidates_suspended | Depends on reason | Back off for timed windows; invalid credentials have no Retry-After and need an operator |
503 accounting_capacity_exhausted | Yes | Honor Retry-After while admitted work settles |
| Upstream 5xx | Cautiously | May have reached Provider; treat as ambiguous and do not assume gateway fallback |
403 token_guard_blocked | Not immediately | Cost threshold or temporary block; change request or wait/administer |
403 model_not_allowed / 404 model_not_found | No | Configuration problem; contact administrator |
| Rejected 400 field | No | Request is invalid; change it |
| Ambiguous result | Cautiously | See 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:
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"}]}'| Duplicate state | Response | Handling |
|---|---|---|
| Same key, different request body | 409 idempotency_conflict | Inspect the business key; do not hide conflicts by replacing it |
| Still running | 409 idempotency_in_progress | Wait and retry the same key under a bound |
| Outcome unknown | 409 idempotency_outcome_unknown | Reconcile the operation before deciding whether to risk a separate execution and charge |
| Completed | 409 idempotency_completed | The original answer is not replayed; save the first response on the client |
| Key cannot be checked or stored safely | 503, such as resource_store_unavailable | Repair 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.
Resource endpoints use Idempotency-Key
Section titled “Resource endpoints use Idempotency-Key”Use a stable key for resource creates: Async (POST /v1/async/invocations), Files
(POST /v1/files), and Batches (POST /v1/batches).
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.jsonlSynchronous 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.
Retry-After is not always present
Section titled “Retry-After is not always present”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.
Timeouts
Section titled “Timeouts”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.