Your agent’s report tool posts a request, gets 202 Accepted and a job id, and then waits for the job inside the tool. The job takes four minutes, the framework cancels the tool call at one, and the job id dies with the cancelled call. The model sees a timeout, calls the tool again, and starts a second job.
What you get
You will end up with three tool shapes for an asynchronous job, measured in model turns and uncollected jobs, and a run of the timeout pitfall under a real agent SDK. This is for you if a tool your agent calls starts work that outlives one request.
Short answer
Have the start tool return the job id at once, or wait inside the tool only for a budget below the framework’s tool timeout and then return the id. A timeout that cancels a tool holding the id in a local variable leaves the job running uncollected. For jobs that run for minutes, register a webhook, save the conversation under the job id, and resume the agent when the signed delivery arrives.
You will need
Node 22 or later for the TypeScript samples, which have no dependencies, and Python 3.12 or later
for the timeout run. The job API is a stand-in on 127.0.0.1 that answers
202 Accepted with a Location header
and Retry-After. Its jobs finish
only when a controlled clock is advanced. The model is scripted as data, so turn counts below are
the result of a scripted policy, standing in for tokens.
Approaches compared
| Approach | When it fits | What it costs you | When to pick something else |
|---|---|---|---|
| Blocking tool with an internal timeout | Jobs that usually finish inside one tool call | Holds a turn open, and a budget over the tool timeout can lose the job id | Jobs that routinely run for minutes |
| Polling tool | Short jobs, and no endpoint the API could call back | A model turn for every status check, so tokens grow with the job’s duration | The job runs long enough that the turn count dominates the bill |
| Webhook completion with resumed state | Long jobs from an API that can call a URL you host | A public receiver, signature checks, and durable state keyed by job id | You cannot expose an endpoint, or the user is waiting on the answer |
Polling is the simplest to build and the most expensive to run, because every check is a model turn. A bounded wait spends a few turns. It is only safe while its budget sits below the framework’s timeout. The webhook spends no turns while the job runs, and buys a public endpoint and a store to look after.
Measure the three shapes
The demo runs each shape against a fresh stand-in. The job takes 120 seconds on the stand-in’s clock, and each scripted model turn costs 10.
node demo.mjs
job takes 120 s, Retry-After 30 s, one model turn 10 s, tool timeout 60 s
shape model turns answer at jobs started collected orphaned
polling tool 14 140 s 1 1 0
blocking tool, 50 s budget 5 140 s 1 1 0
blocking tool, no budget 3 150 s 2 0 2
webhook, resumed from state 3 140 s 1 1 0
polling tool: The report is ready: 1200 rows at /v1/reports/files/job-1.csv.
blocking tool, 50 s budget: The report is ready: 1200 rows at /v1/reports/files/job-1.csv.
blocking tool, no budget: The report did not finish: Tool timed out after 60 seconds.
webhook, resumed from state: The report is ready: 1200 rows at /v1/reports/files/job-1.csv.
no budget, on the server afterwards: job-1 succeeded, job-2 succeeded
webhook, first turn ended with: Report job-1 is running. The answer follows when it is done.
webhook deliveries: job-1 204
Polling spent 14 turns on a job the webhook finished in three, and both answered at 140 seconds. The blocking tool without a budget never answered. Both of its jobs finished on the server with their results unread, because the only copy of each job id was lost in a timeout.
Bound the wait below the timeout
The budgeted tool waits as long as Retry-After suggests, and stops before its budget runs out. It
reads the header as whole seconds only, and waits at least a second between checks. It
then returns the job id with a note, so the next call resumes the same job instead of starting
another. A status check that fails returns the id the same way, whether the answer was an error
page, an error in JSON, or nothing at all.
One budget covers the whole call: the start request, the sleeps, and every check. Each request
gets what is left of the budget as its time limit. A request with no limit of its own can stall
past the budget and into the framework’s timeout, which is the loss the budget was there to
prevent. requestJson ends a request with a timer, so an answer that arrives a byte at a time is
cut off as well.
// The shortest wait between two checks. Without it, an answer with no
// Retry-After would be asked for again at once, until the budget ran out.
const MIN_WAIT_S = 1
// Shape 2: wait inside the tool, but only for budgetS. When the budget runs
// out, hand the job id back so the next call can pick the same job up again.
export function blockingTools(api, { budgetS, sleep, now }) {
async function waitFor(jobId, firstWait, deadline) {
let wait = firstWait
while (now() + wait <= deadline) {
await sleep(wait)
// The check gets what is left of the budget as its time limit. A request
// that stalls would otherwise run past the budget and into the
// framework's timeout, which takes the job id.
const left = deadline - now()
if (left <= 0) break
let res
try {
res = await api.status(jobId, left)
} catch (err) {
return { job_id: jobId, status: 'running', note: `status check failed (${err.message}); call wait_for_report with this job_id` }
}
const { retryAfter, ...job } = res
if (job.status !== 'running') return job
wait = Math.max(MIN_WAIT_S, retryAfter || wait)
}
return { job_id: jobId, status: 'running', note: 'still running; call wait_for_report with this job_id' }
}
return {
// One budget covers the whole call: the start, the sleeps, and the checks.
async run_report() {
const deadline = now() + budgetS
const { jobId, retryAfter } = await api.start({}, budgetS)
return waitFor(jobId, retryAfter, deadline)
},
async wait_for_report({ job_id }) {
return waitFor(job_id, 0, now() + budgetS)
},
}
}
The job id is the one thing the model must get back. It comes from the model on the next call, so
jobPath puts it into the URL as one escaped segment and refuses an empty id, ., and ...
A start that fails has no job id to return, so run_report throws, still inside the budget.
Watch a real framework drop the job id
The OpenAI Agents SDK takes a per-call timeout on @function_tool. When it fires, the default
timeout_behavior="error_as_result" sends the model a message instead of the tool’s result,
as the SDK’s tool timeout docs
describe. This run uses the SDK’s ScriptedModel, and a stand-in whose job never finishes inside
the timeout.
python3 timeout_pitfall.py
openai-agents 0.26.0
timeout=0.5, no budget inside the tool
first tool result the model saw: Tool 'run_report' timed out after 0.5 seconds.
final answer: The report did not finish.
jobs started 2, results collected 0
timeout=2.0, 0.2 s budget inside the tool
first tool result the model saw: {"job_id": "job-1", "status": "running", "note": "still running; call get_report_status with this job_id"}
final answer: Done.
jobs started 1, results collected 1
The timeout message names the tool and the limit. It carries no job id, because the id lived in a local variable of the cancelled call. The scripted policy retried, as the message invites, and started job 2. The budgeted version returned the id when its 0.2-second budget ran out, well inside its 2-second timeout.1
Resume from a signed webhook
The webhook shape saves the conversation under the job id and ends the turn. The receiver checks the HMAC signature over a timestamp and the raw body in constant time, and refuses a timestamp more than five minutes from its own clock. It accepts only a job it has saved state for, and queues the event. The run resumes off the request path, so a slow model turn never holds the API’s connection open.
export async function resume(event, { store, ...agent }) {
const saved = store.get(event.id)
if (!saved) return null
store.delete(event.id)
const history = saved.conversation
history.push({ role: 'tool', name: 'report_webhook', result: { job_id: event.id, status: event.status, result: event.result } })
return runAgent({ ...agent, history })
}
Deleting the saved state before resuming makes a second delivery a no-op, and once the run has resumed the receiver answers it 404. A delivery replayed with a fresh timestamp fails the signature. The saved conversation comes from the receiver’s own store, so the sender picks only which saved job to resume. The Standard Webhooks specification also signs a message id, for receivers that cannot delete state on first use.
Check it worked
The Node suite pins every number in the demo. It also covers a budget set over the timeout and a
status check that fails inside the budget. More tests answer the status check with a stall, a
byte at a time, a broken connection, an HTML page, JSON that names no job status, and a body past
one megabyte. The job id comes back from each. A 404 reads as an unknown job only when its body is
the API’s own, saying the job does not exist. A 404 page or JSON from something in between is a
failed check, because it says nothing about the job. Two tests give the start
the same treatment, where the call fails inside the budget. The receiver answers 401 to a bad signature or a stale timestamp and 404 to an unknown
job, and the suite tries a job id of ../admin and a run with NODE_USE_ENV_PROXY=1.
Node 22 sends node:http requests on the default agent to HTTP_PROXY under that variable, so the
samples give loopback calls an agent of their own.
node --test long-running.test.mjs
1..28
# tests 28
# suites 0
# pass 28
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 949.599661
The test that matters most sets budgetS to 100 against the 60-second timeout. It asserts that
the answer says timed out and that nothing is collected, which is the bug the budget exists to
prevent.
When it goes wrong
A job finishes with its result unread. A blocking tool timed out with the job id in a local variable. Keep the budget below the framework’s timeout, and return the id when the budget runs out.
The tool call times out with its budget set below the timeout. A request stalled, and the budget bounded only the sleeps between checks. Give each request the time left in the budget as its limit, and end the request when that runs out.
After the start request times out, you cannot tell whether a job exists. The API may have created the job before its answer stalled, so a second start can make a second job. Send an idempotency key with the start, as in add an Idempotency-Key header to every POST from a client, so that a retry returns the first job.
Polling burns the token budget on a long job. Every status check is a model turn. Make the status
tool wait up to Retry-After before answering, which turns polling into the bounded wait, or move
to the webhook.
The webhook arrives and resumes nothing. The saved state lived in process memory and the process
restarted, or a second instance received the delivery. Keep the state in a store every instance can
read, keyed by job id. In the OpenAI Agents SDK, RunResult.to_input_list() gives the history to
carry into the resumed run, and the human-in-the-loop guide persists a paused run with
result.to_state().
When not to do this
Do not wait inside a tool for a job measured in minutes. Even with a budget, each wait holds a turn open, and the user sees nothing until it ends. Return the id, tell the user, and collect the result later.
Do not build the webhook shape for a job that finishes in seconds. A public receiver, signature checks, and a durable store are a lot of machinery to save three polling turns.
Do not let the model decide when to give up. The scripted policy here retries once after a timeout, as the SDK’s message invites a model to recover, and each retry starts a job. Put the retry rule in the tool, where a job id can be reused rather than replaced.
Related how-tos
Last verified
Verified 2026-11-30 against Node 22.23.5 and openai-agents 0.26.0, on Python 3.12, 3.13, and 3.14. Every output block is what the command preceding it printed. The model is scripted in both languages, and the job API is a stand-in on 127.0.0.1.
Footnotes
-
MCP grew its own answer to the same problem. Revision 2025-11-25 of the specification adds tasks, which it describes as durable state machines intended for polling by the sender and deferred result retrieval. The receiver generates each task’s ID. The page introduces them as experimental, so the design may change in later revisions. The job id in this page plays the same part, minus the protocol. ↩︎ Back to text