Retry Without Paying Twice: Idempotency Keys for Safer AI API Calls
Your app sends a request to an AI model. Ten seconds pass. The connection drops, your code times out, and it retries. A moment later, the user taps "Send" again for good measure. Was that one AI call, two, or three? And how many did you pay for?
Retries are a normal part of running AI features in production. The trick is making them safe. This post explains, in plain terms, how idempotency keys help, and how OROO Blue uses them so a retried request isn't charged twice.
Why retries quietly cost you money
A timeout on your side does not always mean the request failed on the other side. The model may have finished the work, but the response never reached you. If your code simply sends the same request again, the provider sees a brand-new call. You get billed again for tokens, and any side effects in your own system (saving a reply, sending a notification, deducting a user's credits) can happen twice too.
At small scale this is a rounding error. At production scale, with flaky mobile networks and impatient users, it adds up to wasted spend and confusing bugs.
What an idempotency key actually does
An idempotency key is a unique ID you attach to a request. It tells the server: "If you've already seen this exact operation, don't do it again; treat this as a repeat." The same key sent twice means one operation, not two.
The important rule is simple: one key per user action, reused on every retry of that action. If a chat feature, a support bot or an internal tool triggers one AI request, generate one key for it. If you retry because of a timeout, send the same key. If the user starts a genuinely new action, generate a new key.
Here is a generic, vendor-neutral sketch of the pattern (not any specific API):
key = new_unique_id() // once per user action
for attempt in 1..max_attempts:
result = call_ai(request, idempotency_key = key, timeout = T)
if result.ok: return result
if not retryable(result.error): raise result.error
sleep(backoff_with_jitter(attempt))
raise "gave up after retries"
Retry smart, not just often
Idempotency makes retries safe; good retry habits make them sensible. A few practical tips:
- Set clear timeouts. Long AI responses need generous timeouts, but not infinite ones.
- Use exponential backoff with jitter. Wait a little longer after each failure, with some randomness, so many clients don't retry at the same instant.
- Retry only what's worth retrying. Network errors, timeouts and temporary server or rate-limit errors are usually retryable. Bad requests or authentication errors won't fix themselves, so fail fast.
- Cap the attempts. Three to five tries is a common starting point. After that, show the user a friendly message.
- Log the key. Record the idempotency key with each attempt so you can trace exactly what happened later.
How OROO Blue fits in
OROO Blue is a secure AI gateway for Indian teams and developers. With one API and one OROO key, you can reach OpenAI, Google and Anthropic models using the model names oroo-chatgpt, oroo-gemini and oroo-claude. Send an idempotency key with your request, and a retried request won't be charged twice.
Billing is prepaid in rupees through OROO Mint, a wallet you top up via Razorpay, and you pay only for tokens used. That makes double charges easy to spot and, with idempotency keys, easy to avoid.
A few other controls help keep retry logic from turning into a runaway loop:
- Per-key model, token and rate limits, so one buggy retry loop can't burn through your wallet.
- Freeze, rotate or revoke keys when something looks wrong.
- IP allowlists, so a key only works from the places you expect.
- Key secrets shown once and stored hashed.
- No storage of prompts or completions by OROO.
Build retry-safe AI features with OROO Blue
Retries will happen. Paying twice for them doesn't have to. Generate one idempotency key per user action, retry with backoff, and let your gateway do the rest. Explore OROO Blue and start building at orooblue.com.

Comments
Post a Comment