Skip to content
Rapid Router
Guides · Errors and limits

Idempotency

Retry a request whose response was lost without paying twice, with the Idempotency-Key header.

The retry a caller cannot avoid: the request completed, the response was lost on the wire, and sending it again bills a second completion. Send an Idempotency-Key header on a non-streaming POST /chat/completions and the retry returns the stored first answer instead of running, and charging, again. The replay carries the response header idempotency-replayed: true and bills nothing.

With no header, one case is still covered. If your client disconnects from a non-streaming POST /chat/completions before the answer arrives, the provider keeps generating and that completion is billed. It shows in your logs as status 499. For 10 minutes, the same body from the same key gets that answer at no charge, even if it is still being generated, with the response header X-RR-Cache-Status: hit-hangup. The same holds when the provider never answers and the request ends as a billed 504: the retry gets that answer, not a second bill. To get a fresh answer instead, change anything in the request. Stream if your replies can outlast your client's timeout: a stream that is cancelled stops being generated and is billed only for what was produced.

The exact semantics:

  • Same key, same body: the stored 200 replays, with idempotency-replayed: true.
  • Same key, different body: 422 with code idempotency_mismatch. Silently answering with a response to a different question would be worse than failing, so use a fresh key per logical request.
  • Same key while the first send is still running: 409 with code idempotency_in_flight. Retry after it finishes.
  • A 200 is stored, and so is a timeout that was billed. If the provider never answers, the request ends as 504 with code upstream_timeout and is billed for what the provider was generating. A retry with the same key gets that same answer and is not billed again; use a new key to run the request again. Any other failed call stays retryable with the same key.

Keys are scoped to the API key that sent them, so they can never collide with another tenant's or with another key on your own account, and they live for 24 hours. On a streaming request the header is ignored: replaying a stream as a stored blob would change its shape mid-integration.

curl
curl https://rapidrouter.ai/api/v1/chat/completions \
  -H "Authorization: Bearer $RAPIDROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-8842-attempt-1" \
  -d '{
    "model": "google/gemini-3.6-flash",
    "messages": [{ "role": "user", "content": "Hi" }]
  }'

# send it again with the same key and body:
# same 200 response, header idempotency-replayed: true, nothing billed