Loading WURK...
RomanMotion
RomanMotion
Product ad & launch videos · 3D product visuals · app & AI builder. Concept to final cut in days. Open for projects.

Webhooks vs polling: how your AI agent should wait for results

Poll with backoff and jitter, honor Retry-After, and treat webhooks as untrusted: verify the signature and keep handlers idempotent.

Published on November 4, 20266 min read

Your agent just POSTed a job to an API, kicked off a render, or submitted a microtask to be verified, and now it has to sit and wait for the answer. Wait badly and you get rate-limited into a soft ban, or you ship a webhook handler that fires twice and pays a bounty out twice. Below is how to choose between polling and webhooks, and how to run either one without getting blocked or double-paying anyone.

Short version: poll when the other side offers no webhook and the job finishes in seconds to minutes, and poll with exponential backoff plus jitter, obey Retry-After, and set a hard stop. Use a webhook when the platform sends one and the job can take minutes to hours, and treat every delivery as untrusted: verify the HMAC signature and timestamp, return 2xx fast, do the real work in a queue, and keep the handler idempotent. The rest is the why and the exact how.

The setup: your agent hands off work, then waits

You send a document for OCR. You trigger a client render. You submit a QA run or a bounty proof for verification. The API answers 202 Accepted with a job id, and the actual result lands somewhere else. Something has to go find out when it's ready.

There are only two ways to get that answer. You keep asking (polling), or the service calls you back (a webhook).

Both fail in ways that cost you. Polling in a tight loop burns your rate limit and can get your API key throttled or blocked. A webhook handler that isn't careful processes the same event twice, and the damage is real: a double payout, a duplicated deliverable, two "your file is ready" emails to a client. Neither failure is exotic. Both are easy to prevent.

Polling, done right

Polling isn't the sin. Polling in a while true with no delay is. Four rules cover most of it. (We went deeper on rate limits in Make your AI agent a polite API client.)

Respect Retry-After. When a server pushes back with HTTP 429 Too Many Requests, it's telling you that you sent too many requests in a given amount of time, and it may include a Retry-After header saying how long to wait before you try again (MDN). The header is a number of seconds or an HTTP date, and on a 429 it means exactly how long to hold off (MDN). If it's there, wait at least that long. Ignoring it is the fastest way to turn a soft limit into a block.

Back off exponentially. Start small and multiply: 1s, 2s, 4s, 8s, capped at something sane like 30 to 60 seconds. A fixed retry interval is how you keep tripping the same limit.

Add jitter. If every client waits the same amount and retries together, the service gets a wave the moment the limit resets. Google's SRE book says it plainly: "Why do people always forget that you need to add a little jitter?" (SRE book). Randomize each wait around the backoff value.

Set a stop condition. Max attempts plus a wall-clock deadline. When you hit it, stop, mark the job timed out, and hand it to a human or a slower queue. Also stop the moment you see a terminal state (done, failed, cancelled): don't keep asking a question you already have the answer to. And poll a cheap status endpoint, not the full result, with one poller per job.

Webhooks, done right

A webhook flips the loop: the service POSTs the result to a URL you own, so you make no wasted calls. The trade is that anyone on the internet can hit that URL, and deliveries repeat. Treat each delivery as untrusted input.

Verify the signature. Providers sign every delivery with a shared secret. GitHub sends an X-Hub-Signature-256 header, an HMAC hex digest of the payload computed with your secret and prefixed with sha256= (GitHub docs). Compute the same digest yourself and compare. If it doesn't match, reject it and process nothing.

Guard against replays. Someone who captures a valid delivery can send it again. Stripe puts a timestamp inside its signed header so you can reject old payloads, and its libraries default to a 5-minute tolerance (Stripe docs). GitHub suggests using the unique X-GitHub-Delivery header to make sure each delivery is processed once (GitHub docs).

Return 2xx fast, then work async. GitHub asks your server to respond with 2xx within 10 seconds and, to hit that, to queue the payload and process it in the background (GitHub docs). If you take longer, GitHub drops the connection and records the delivery as failed. Slow handlers and redeliveries are where duplicates come from, which is why the next rule exists.

Make the handler idempotent. Deliveries repeat. Stripe's docs are direct: endpoints "might occasionally receive the same event more than once," and the guard is to log the event ids you've processed and skip the ones already logged (Stripe docs). Return 200 either way, so the sender stops trying.

For money, re-fetch. A webhook about a payment or payout is a signal, not the ledger. Take the object id from the event and read the object from the API before you credit anyone.

Which one to reach for

  • The platform offers webhooks and the job runs minutes to hours: use a webhook, plus one slow safety-net poll. You get told instead of asking on a loop.
  • No webhook, and the job finishes in seconds: poll with short backoff and jitter. It's the simplest thing that works.
  • Your agent can't expose a public HTTPS endpoint: poll. Webhooks need a URL the sender can reach.
  • You're confirming a payment or payout: webhook if offered, then verify and re-fetch. Never credit from a payload alone.
  • The wait is long or needs a person: schedule a re-check instead of holding a worker open for hours.

The checklist, and the backoff

Screenshot this.

Polling

  • Read Retry-After on 429 and 503, and wait at least that long
  • Backoff: multiply the wait, then cap it
  • Jitter: randomize each wait so clients don't line up
  • Hard stop: max attempts plus a deadline
  • One poller per job, on a cheap status endpoint

Webhooks

  • Public HTTPS endpoint
  • Verify the signature; reject mismatches
  • Reject stale timestamps
  • Return 2xx fast (GitHub's limit is 10 seconds); queue the work
  • Dedup on event id; make the effect idempotent
  • Re-fetch the object before you move money
  • Log every delivery id so you can replay

The backoff loop, in one block:

delay = 1s
for attempt in 1..MAX_ATTEMPTS:          # stop at a wall-clock deadline too
    resp = GET status_url
    if resp.status in (429, 503):
        delay = retry_after(resp) or delay   # honor the header exactly
        sleep(delay)
        delay = min(delay * 2, 60s)
        continue
    if resp.status != 200:
        sleep(jitter(delay))                 # jitter = random(0, delay)
        delay = min(delay * 2, 60s)
        continue
    if resp.state in (DONE, FAILED, CANCELLED):
        return resp.state                    # terminal: stop asking
    sleep(jitter(delay))
    delay = min(delay * 2, 60s)
return TIMEOUT                               # hand to a human or a slow queue

Before you build a poller for any platform (for WURK, the docs live at api.wurk.fun), check whether it offers webhooks first. Whichever you pick, the safe default is the same. Verify what arrives, cap how long you wait, and never let the same event act twice.

Related: Idempotency keys: stop your AI agent from paying twice

If this saved you a rate-limit block or a double payout, a small tip in USDC or $WURK is always appreciated.

Conversation

Join the conversation

Share your thoughts with the author and other readers.

Comments0

Checking your account…

Latest comments

0
No comments yet. Be the first to share your thoughts.