Idempotency
Give a write one stable identity, retry safely, and know when Inference starts a new computation.
Networks fail mid-request. Give each intended write a stable identity so a
retry cannot dial twice or create a second resource. Most mutating Voice
API-key POST requests require one Idempotency-Key header. Some operations
make it optional; follow the compact retry requirement on each endpoint.
curl -X POST https://api.kejue.co/v1/outbound-jobs \
-H "Authorization: Bearer $KEJUE_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 018f4d7a-7d30-7d97-9a9b-34c291b99618" \
--data '{
"agent_id": "AGENT_ID",
"to_number": "+12025550100",
"phone_number_id": "PHONE_NUMBER_ID"
}'One key = one logical intent. Generate a high-entropy value (UUID/ULID),
persist it with the exact request bytes, and reuse it only to retry that
same request. A retry with the same key returns the original result with
Idempotency-Replayed: true — it never executes twice.
Inference requests
Inference API-key creation uses the write-replay rules on this page. Audio
generation and file transcription instead use Idempotency-Key as a
computation identity: a repeated identity returns
409 inference_request_exists, rather than replaying audio or starting the
computation again. Inspect the existing request with
GET /v1/inference/requests/{id} before deciding whether to create
a new computation with a new key. A disconnected request may already have
consumed credit; see Inference usage.
Realtime token creation is single-use and does not replay credentials. Mint a fresh token for each connection attempt; see WebSocket authentication. Each endpoint’s compact requirements strip identifies its retry policy.
The remaining replay rules apply to replay-protected writes; Inference computation identities follow the behavior above.
Outcomes
| Situation | Response |
|---|---|
| No header on a required POST | 400 idempotency_key_required; nothing executes |
| First request | Executes normally |
| Retry of a completed identical request | Original status/body + Idempotency-Replayed: true |
| Identical request still running | 409 idempotency_in_progress + Retry-After: 1 — wait and retry same key |
| Same key, different content | 409 idempotency_conflict — recover the original bytes, or use a new key for a genuinely new intent |
| Same key after the actor's role/permissions changed | 409 idempotency_security_context_changed — no replay, no re-execution; read business state |
| Empty, duplicate, or invalid key value | 400 invalid_idempotency_key |
| Protection store unreachable before execution | 503 idempotency_unavailable + Retry-After: 1; nothing executed — fail closed |
| Completed, but replay store temporarily unreachable | 503 idempotency_result_unavailable + Retry-After: 1 — retry same key later; never switch keys |
| Completed, but replay payload permanently lost | 409 idempotency_result_lost — terminal; inspect the resource to find the committed result |
Both replay-failure codes prove the original request completed — a new
key is the one forbidden move, because it would run the mutation again. For
idempotency_result_lost, follow details.remediation; if the lost
response held a one-time credential, revoke and recreate it.
What counts as "the same request"
A key is scoped to workspace + authenticated actor + HTTP method + path +
canonical query string; within that scope, the fingerprint covers the exact
body bytes, media type, and any If-Match values. So:
- Reusing a key on a different endpoint is harmless — different scope.
- Byte-different JSON is a different request, even if semantically equal. Retry your stored bytes; don't re-serialize a mutable object.
- A different API key or user is a different scope — the same key text does not deduplicate across actors. Keep the original credential for retries.
- Reordering distinct query parameter names is fine; repeated values keep their order.
Key format
Voice replay keys and Inference API-key creation keys accept 1–255 ASCII
characters. Inference audio-generation and file-transcription computation
identities accept at most 128 bytes. Use the RFC HTTP token set:
letters, digits, and
! # $ % & ' * + - . ^ _ ` | ~Use no spaces, slashes, quotes, or non-ASCII. UUIDs, UUIDv7, and ULIDs fit both limits. Don't derive the key purely from a resource ID that can legitimately receive the same action twice.
Replay retention and availability
A completed mutation does not run again when its stored response expires
or cannot be recovered. Replay responses are retained for 24 hours by
default; if a completed result is no longer available, follow the
idempotency_result_lost guidance above instead of creating a new intent.
If replay protection is unavailable before a protected write can start, we
return 503 idempotency_unavailable without applying the mutation.
Replays return the original status/body plus a safe header allowlist
(Cache-Control, Content-Type, Content-Disposition, Location, ETag,
Last-Modified, Retry-After). X-Request-Id always identifies the
current attempt, and replayed error envelopes get their request_id
rewritten to match. A legacy Idempotent-Replayed header is still emitted;
use the canonical Idempotency-Replayed.
Which results are replayable
A completed result below 500, except 429, can be replayed. A 429 or
5xx can be retried with the same key. Ordinary validation and authorization
failures are checked again on retry; a recorded final 4xx outcome can be
replayed. Don't predict which: retry unchanged only for
transport ambiguity or retryable codes, and check Idempotency-Replayed on
what comes back.
Scope of the mechanism
Idempotency-Key applies to POST only — GET is read-only, PUT is a
full replacement, and conditional PATCH/DELETE use
If-Match where declared. It dedupes
retries of one intent, not two independently-generated requests for the
same business action — use resource uniqueness and point reads for that.