Idempotency and retries
Send an Idempotency-Key header and a retry of the same request returns the same call, with the same passport, instead of making and billing a second one.
Why#
A network error or a timeout leaves your application not knowing whether a call went through. A plain retry makes a second call: a second model call, a second passport, a second charge. With an Idempotency-Key, the retry returns the first call.
How#
Send a header with a value of your choice that is unique per request, for example a UUID you create before the first attempt and reuse for every retry of that request.
import uuid
key = str(uuid.uuid4())
completion = client.chat.completions.create(
model="provider/model-name",
messages=messages,
extra_headers={"Idempotency-Key": key},
)
curl https://www.ospp.it.com/api/v1/openai/chat/completions \
-H "Authorization: Bearer $OSPP_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 3f1c2a9e-5d0b-4c57-9a1e-0c2d8a7b6e44" \
-d '{"model": "provider/model-name", "messages": [{"role": "user", "content": "Hello"}]}'
The key counts together with the conversation#
OSPP does not look at the key alone. It looks at the key together with the conversation: the messages after the system messages.
| What you send again | What happens |
|---|---|
| The same key, the same conversation, the same node's definition | The same call is returned: the same run id (X-OSPP-Run), the same answer, the same passport. No second model call is made and nothing is billed again |
| The same key, another conversation | A new call with its own passport. The key does not tie it to the first call |
| The same key, the same conversation, but another model, system prompt, parameters or tool definitions | Refused with status 409 and the code idempotency_conflict |
The third case is a request that looks like a retry and is not one: the conversation is the same, but what is asked of the model has changed. OSPP refuses it instead of returning an answer the request did not ask for. A different X-OSPP-Node name counts as a change too. So does switching between a streamed and an unstreamed request where the answer is streamed live; see streaming.
The keys of one organisation are separate from those of every other organisation.
If the first attempt is still in progress, the retry waits for it.
Without the header every request is a new call.
Use a key for one request#
Create a new key for every new request, and reuse it only for retries of that request. Because the key counts together with the conversation, reusing one key across the turns of a conversation does not return a wrong answer: each turn has other messages and is a new call. It also gives no protection beyond what one key per request gives, so one key per request is the simple rule.
Retrying after an error#
| What you received | What to do |
|---|---|
| No response (connection lost, client timeout) | Retry with the same Idempotency-Key and the same body |
504 run_pending | The call may still finish. Retry with the same key to receive its answer, or read it with GET /v1/runs/{run_id} |
429 or 503 with Retry-After | Wait that long, then retry; the same key is fine |
502 run_... | The call ended without a passport. A retry with the same key returns that same ended call; use a new key to make a new call |
| 400, 404, 409 | Do not retry unchanged; fix the request |
The official OpenAI clients retry some errors by themselves. Set the key per request, as above, so that their automatic retries carry it too.
How long a retry returns the answer#
A retry returns the answer for as long as OSPP holds it: about an hour by default, or the node's retention period with Model Compare on. After that the same key still makes no second call, but the answer is gone and the retry is answered with 504 run_pending. The call and its passport are still read with GET /v1/runs/{run_id}.
Self-hosted#
Through the local proxy the key also counts together with the conversation, and a retry never makes a second call. The proxy does not return the first answer again: a retry of a request that already started is refused with status 409 (IDEMPOTENCY_CONFLICT). Read the call with GET /v1/runs/{run_id} or find it in the admin UI. See the local proxy.
Outcomes#
Reporting an outcome is a separate request with its own optional idempotency_key field; see outcomes.
