# AI agent wait for webhook callback: the inbox and wake recipe | agentic402

> AI agent waiting for a webhook callback? Create an inbox, book a wake job, stop, read it when woken. From $0.015, with late and missing callbacks covered.

Source: https://agentic402.dev/recipes/wait-for-a-callback/

Published 3 December 2026 · Updated 3 December 2026

AI agent wait for webhook callback, in four calls: create an [inbox](https://agentic402.dev/tools/inbox/) ($0.01) and give its URL to the service as the callback address, book a [wake](https://agentic402.dev/tools/wake/) job ($0.005) for when the result should be ready, stop running, and when the wake request arrives, read the inbox for free. The callback is waiting there whether it came early or on time.

One wait costs $0.015. Each extra check, if the callback is late, costs $0.005.

## Why waiting is the expensive part

An agent that kicks off a slow job (a build, a payment, a batch job on another API) has three ways to wait for the answer. It can keep running and poll, paying for compute and often for model tokens the whole time. It can block inside one long request, which breaks the moment the process restarts. Or it can stop, and arrange for two things to happen while it's gone: the callback must be caught, and the agent must be started again.

The third way is the only one that costs nothing while nothing is happening, but most agents can't do either half of it alone. They have no public URL for the callback and no clock that runs while they're stopped. An inbox is the public URL and wake is the clock.

## Step 1: create an inbox and register it as the callback

Create the inbox with an empty paid POST. Keep the `read_url` and `read_token` somewhere that survives the agent stopping, such as the job record that will be passed back to it, or a [memory](https://agentic402.dev/tools/memory/) space.

```
const inbox = await (await payFetch("https://agentic402.dev/v1/inbox", { method: "POST" })).json();
// inbox.inbox_url, inbox.read_url, inbox.read_token, inbox.expires_at (7 days)

await startBuild({ callback_url: inbox.inbox_url });   // the service's own API
```

Use one inbox per job. Then everything in it belongs to this job, a leaked read URL and token expose only one job's callbacks, and the 1,000-item window and 60-requests-a-minute limit aren't shared with anything else. If the service sends a test event when you register a webhook, read the inbox once with `wait=0` to confirm it arrived.

## Step 2: book a wake job for when the result should be ready

Pick the time you expect the job to finish, plus a margin. If builds usually take 12 minutes, wake at 15. Point the job at whatever starts your agent, and put what the next run needs in the body: which job, which inbox, what to do.

```
await payFetch("https://agentic402.dev/v1/wake", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    at: new Date(Date.now() + 15 * 60e3).toISOString(),
    url: AGENT_START_URL,
    headers: { "X-Start-Secret": START_SECRET },
    body: { task: "check-build", read_url: inbox.read_url, attempt: 1 },
  }),
});
```

The start endpoint should check the secret header, because anyone can send a POST to it. If your agent has no start endpoint at all (it's run by hand, or on a fixed schedule) point the wake job at the inbox itself. The reminder then lands next to the callback, and the next run finds both in one read.

Booking returns a `cancel_token`. Keep it, so a pending check can be called off for free if the job's result reaches the agent some other way first.

## Choosing when to check first

The first wake time is a guess, so make it an informed one. If the service publishes typical durations, start there. Better, record how long past jobs took: write each job's start and finish time to [memory](https://agentic402.dev/tools/memory/) and schedule the first check near the usual finish. Twenty past builds that all finished between 11 and 14 minutes make 15 an easy choice.

Checking too early costs one extra wake job and one short agent session. Checking too late costs whatever waiting is worth to you: a deploy left unverified, a customer left without an answer. For short jobs, lean early. For jobs that take hours and vary a lot, check at the usual time and then back off.

## Where to keep state between runs

The agent that wakes up is a fresh process. It knows only what the wake request tells it and what it can read. There are two places to put the rest.

The wake body is the simplest: a small JSON object with the task name, the inbox's `read_url` and an attempt counter, delivered with the request. Keep secrets out of it, and out of wake headers where you can, because both are stored with the job until it ends and for 7 days after.

For anything larger or more sensitive, use a memory space. Put the inbox's `read_token`, the job's history and anything the next run needs under a key such as `jobs/build-4812`, and pass only that key in the wake body. The memory token itself then lives with your agent's other secrets, not in a request we store.

## Step 3: stop

Exit. Nothing on agentic402 needs your agent to be running: the inbox accepts the callback at any time in the next 7 days, and the wake job fires on its own. If the callback arrives early, it waits in the inbox. If your start endpoint is briefly down when wake calls, wake tries up to 6 times over about ten minutes.

## Step 4: when woken, read the inbox and decide

```
const r = await fetch(`${job.read_url}?after=0&wait=10`, {
  headers: { Authorization: `Bearer ${READ_TOKEN}` },
});                                                     // free
const { items } = await r.json();
const done = items.find((i) => verifySignature(i) && JSON.parse(i.body).status);

if (done) finish(JSON.parse(done.body));
else if (job.attempt < 6) scheduleWake({ ...job, attempt: job.attempt + 1 }, 10 * 60e3);
else giveUp(job);
```

Reading is free and the `wait` parameter gives a callback that is seconds away a chance to land before the agent decides it's late. Check the signature on any item before trusting it; the inbox keeps the provider's signature headers and the exact body for that.

## What one wait looks like, minute by minute

Here is a build that takes 18 minutes, with a first check at 15 minutes and later checks every 10 minutes:

*Example timeline: build finishes at minute 18. Costs in USDC.*

| Minute | Event | Cost |
|---|---|---|
| 0 | Agent creates inbox, starts build, books wake for minute 15, exits | $0.015 |
| 15 | wake starts the agent. Inbox is empty. Agent books wake for minute 25, exits | $0.005 |
| 18 | CI posts the result to the inbox. Nobody is running | $0 |
| 25 | wake starts the agent. It reads the result and carries on | $0 |

Total: $0.02, and the agent ran for three short sessions instead of 25 minutes. The cost of guessing too early is one extra wake job at half a cent, so err on the early side when the wait is short and on the late side when the job routinely takes hours.

## Callbacks that come late, twice, or never

**Late.** Book another wake job and stop again, as in step 4. Widen the gap each time: 10 minutes, then 30, then a few hours. Set a limit on attempts or on total time, and decide in advance what the agent does when it's reached, such as reporting a timeout to a person.

**Longer than a week.** An inbox lives 7 days and can't be extended. For longer waits, create a fresh inbox before the old one expires, give the service the new URL if it allows changing it, and keep reading the old one until it expires. At $0.01 a week, a 30-day wait needs five inboxes, $0.05.

**Twice.** Webhook providers retry when they don't get a quick success reply, and some send several events per job (queued, started, finished). The inbox stores every request it accepts, up to its 1,000-item window, and a read returns at most 100 of them: to see more, read again with `after` set to the last item's `seq`. Pick out the one you want by its event type and remember the provider's event ID, so a retried delivery isn't acted on twice.

**Never.** Most providers retry a failed delivery, but an inbox answers `200` as soon as it stores a request, so from the provider's side the delivery succeeded. If nothing arrives at all, the problem is upstream: the wrong URL was registered, or the job never finished. Many providers also offer a status API; after a few empty checks, ask it directly.

## The same recipe for a payment that settles later

Builds are the easy case because they finish in minutes. Payments, refunds and identity checks can take hours or days, and the confirmation usually comes by webhook. The recipe doesn't change, only the timings do.

Say an agent arranges a bank transfer that normally confirms the next working day. It creates an inbox, registers it as the webhook endpoint for that one transfer if the provider allows per-request URLs (or for the whole account if it doesn't), and books a wake job for 10:00 the next working day. It saves the transfer reference and the inbox token in memory and stops.

Next morning the agent wakes, reads the inbox, and looks for an event about its transfer reference. Found and signed correctly: it records the payment and moves on. Not found: it books another check for the afternoon, then one for the following morning, and after three working days it flags the transfer for a person to look at. Five checks over three days cost $0.025 in wake jobs plus $0.01 for the inbox.

Two details matter more for money than for builds. Verify every webhook's signature before acting, since anyone who learns the inbox URL can post to it. And act on the provider's event ID only once, because payment providers resend events they think weren't received.

## How this compares with workflow callbacks

Workflow engines solve the same problem inside their own platform. Google Cloud Workflows can pause an execution until a callback endpoint is called: "Once received, the workflow execution resumes, and the callback is processed." Its wait has a timeout that defaults to 43,200 seconds (12 hours), and callers need Google Cloud IAM permissions. Intercom Workflows has a "Wait for Webhook" step, which "will not wait indefinitely and has a built-in 7 day limit."

Both work well when the agent already lives on that platform. The inbox-and-wake recipe is for agents that don't: it needs only an HTTP client and a wallet, works with any service that can send a webhook, and leaves the waiting logic in the agent's own code, where it can change the next check time based on what it found.

The pattern to avoid is the busy wait: an agent that sits in its conversation loop asking "has it arrived yet?" Each turn of that loop is another model call over a growing conversation, so a long wait burns tokens on nothing.

## What the recipe costs at different wait lengths

*Cost of one wait, with one inbox per 7 days and the number of wake checks shown*

| Wait | Inboxes | Wake checks | Total |
|---|---|---|---|
| Minutes, on time | 1 | 1 | $0.015 |
| Hours, 3 checks | 1 | 3 | $0.025 |
| A day, checked every 4 hours | 1 | 6 | $0.04 |
| 3 weeks, checked daily | 3 | 21 | $0.135 |

Reads are free in every case. The other cost is your own: each wake starts a short agent session, so fewer, better-timed checks save more on your side than on ours.

## Testing the whole chain for $0.015

Before wiring a real service in, run the recipe against itself. Create an inbox. Book a wake job 15 seconds ahead with the inbox URL as the target and `{"test": true}` as the body. Then read the inbox with `wait=30`. Usually within that half minute (alarms fire at the set time or shortly after, so read again if it's empty), the wake delivery shows up as an item, with the `X-Agentic402-Wake` header carrying the job ID. The whole test costs $0.015 and proves your payment client, your token handling and your read loop all work.

Then send a request to the inbox URL yourself with curl, shaped like the provider's webhook, and check that your step-4 code picks it out and ignores the test item.

## Running it over MCP

The same recipe works through the [agentic402 MCP server](https://agentic402.dev/docs/mcp/) with `inbox_create`, `wake_schedule` and `inbox_read`, passing the read token as an argument. The paid steps need an MCP client that can pay with x402; reading is free in any client.

For the background on why agents can't take webhooks directly, and the other patterns people use, read [webhooks for AI agents](https://agentic402.dev/guides/webhooks-for-ai-agents/). For the details of each call, the [inbox reference](https://agentic402.dev/tools/inbox/) and the [wake reference](https://agentic402.dev/tools/wake/) list every field and limit.

## Questions about waiting for callbacks

**Can the callback itself wake the agent?**
Not through an inbox. An inbox stores requests and doesn't forward them anywhere, which is why the recipe pairs it with wake. If the service can call your agent's start endpoint directly and your endpoint is always reachable, you don't need an inbox at all.

**Can several jobs share one inbox?**
Yes, if each callback says which job it belongs to. Keep one `after` cursor and route items by job ID. Separate inboxes are simpler and cost $0.01 each.

**What if the service needs the callback URL to answer a verification challenge?**
An inbox replies `{"ok": true}` to every request it accepts and can't compute a reply, so that service needs an endpoint you run yourself.

Cite this page: agentic402, "AI agent waiting for a webhook callback, without staying awake", https://agentic402.dev/recipes/wait-for-a-callback/, published 3 December 2026. Workflow-engine limits are quoted from Google Cloud and Intercom documentation as read that day.

## Related

- [Webhook URL for AI agents: inbox](https://agentic402.dev/tools/inbox/)
- [Schedule an HTTP request: wake](https://agentic402.dev/tools/wake/)
- [Webhooks for AI agents: patterns compared](https://agentic402.dev/guides/webhooks-for-ai-agents/)
- [Recipe: read a page once and keep it](https://agentic402.dev/recipes/read-a-page-once-keep-it/)
