Claude API Webhook Retries & Failure Handling Guide
If you're building an async integration around Claude — queueing requests, processing them in the background, and notifying your system (or a customer's system) when a response is ready — you need a retry and failure-handling strategy for two separate layers: calls to the Claude API, and webhook deliveries from your service to whoever's listening. Getting either one wrong causes duplicate charges, lost responses, or silent failures that look fine in your logs but break production for someone else.
This guide covers both layers: how to retry Claude API calls that fail transiently, and how to design webhook delivery so retries don't turn into duplicate side effects downstream.
Why Claude API calls need retry logic in the first place
Claude's API (and any gateway sitting in front of it, including SubToAPI) can fail for reasons that have nothing to do with your request being wrong:
- 429 rate limit — you've hit a token-per-minute or request-per-minute ceiling.
- 500/502/503 — transient upstream issues.
- Timeouts — long generations or network hiccups, especially on non-streaming requests.
- 529 overloaded — the model is temporarily at capacity.
None of these mean "give up." They mean "wait and try again, with limits." A naive while (true) retry() loop will make things worse during an outage by hammering the API right when it's already struggling.
A correct retry loop
async function callClaude(payload, attempt = 1) {
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (res.ok) return res.json();
const retryable = [429, 500, 502, 503, 529].includes(res.status);
if (retryable && attempt < 5) {
const retryAfter = res.headers.get("retry-after");
const delay = retryAfter
? Number(retryAfter) * 1000
: Math.min(2 ** attempt * 500, 20000) + Math.random() * 500;
await new Promise((r) => setTimeout(r, delay));
return callClaude(payload, attempt + 1);
}
throw new Error(`Claude request failed: ${res.status}`);
}
Key points:
- Respect
Retry-Afterif the response includes it — don't guess when the server already told you. - Exponential backoff with jitter — fixed delays cause synchronized retry storms across many clients.
- Cap attempts — 4–5 retries is enough; beyond that, surface the failure instead of looping silently.
- Only retry idempotent-safe operations — a single
messagescall is safe to retry since it's stateless, but if your retry triggers a side effect elsewhere (billing a user, writing to a database), guard that separately.
See /docs/messages for the exact error codes and response shape to match against.
Retrying webhook deliveries without duplicating work
Once Claude's response comes back, many integrations fire a webhook to notify another system — "generation complete," "summary ready," "agent step finished." This is where most failure-handling bugs actually live, because now you're retrying a delivery whose receiver might have already processed it.
Design for duplicate delivery, not against it
You cannot guarantee exactly-once delivery over HTTP. Aim for at-least-once delivery with idempotent handling on the receiving end instead:
- Include a unique
event_idin every webhook payload. - Ask (or require) receivers to dedupe on
event_idbefore acting on it. - Keep payloads deterministic — if you retry the same event, send the exact same body, not a regenerated one.
{
"event_id": "evt_8f2a1c",
"type": "completion.finished",
"created_at": "2024-05-02T14:22:01Z",
"data": {
"request_id": "req_93ab",
"status": "succeeded",
"output": "..."
}
}
Retry schedule for webhook delivery
Unlike API calls, webhook retries should span longer because the receiving endpoint might be down for minutes, not milliseconds:
| Attempt | Delay | |---|---| | 1 | immediate | | 2 | 1 min | | 3 | 5 min | | 4 | 30 min | | 5 | 2 hours | | 6 | 12 hours (final) |
After the final attempt, move the event to a dead-letter queue instead of dropping it. A dashboard or log table where failed deliveries land — with the ability to manually replay them — saves you from "the webhook just never arrived and nobody noticed for a week."
Verify what you send and what you receive
If you're the one sending webhooks, sign the payload (HMAC over the raw body with a shared secret) so receivers can confirm authenticity. If you're the one receiving webhooks from a Claude-adjacent service, verify that signature before processing — otherwise retries combined with no verification is an open invitation for replay or forged events.
const crypto = require("crypto");
function verifySignature(rawBody, signature, secret) {
const expected = crypto
.createHmac("sha256", secret)
.update(rawBody)
.digest("hex");
return crypto.timingSafeEqual(
Buffer.from(expected),
Buffer.from(signature)
);
}
Respond fast, process later
A common cause of unnecessary retries: the receiving endpoint does slow work (database writes, downstream API calls) before returning 200, times out, and the sender retries a request that actually succeeded. Acknowledge receipt immediately (return 200 as soon as the payload is validated and queued), then process asynchronously.
Where SubToAPI fits
If your Claude integration involves streaming responses, tool-use calls, or background jobs across a team, you still need clean failure handling at the API-call layer regardless of what you build on top. SubToAPI gives you per-application API keys (sub_live_...), usage metadata on every response, and consistent error codes so your retry logic doesn't have to special-case different failure shapes. Check /docs/streaming and /docs/tools for how retries interact with streamed and tool-use requests specifically, and /docs/quickstart to get a key provisioned. Plans start at /pricing, with a free trial at /signup.
Questions
Does Claude's API send webhooks directly to my app? No. Anthropic's API is request/response (including streaming). Webhooks come from whatever layer you build on top — your own job queue or gateway — to notify downstream systems when a Claude request completes.
What's the difference between retrying an API call and retrying a webhook? API call retries should be fast and short-lived (seconds, capped attempts) because you're waiting on a live request. Webhook retries should span minutes to hours, since the receiving endpoint could be down for a longer stretch, and must be designed for duplicate delivery.
How do I avoid processing the same event twice after a retry? Attach a unique event ID to every webhook payload and require the receiver to check it against previously processed IDs before acting — this makes at-least-once delivery behave like effectively-once processing.