Home » Long-Running Agent Tools: Polling, Callbacks, and Job Handles

Long-Running Agent Tools: Polling, Callbacks, and Job Handles

A retained job handle follows accepted work through running state to a result, with polling observations and a callback signal.

A tool that starts work lasting longer than one call should return a stable job identity and a way to observe that job's state. The agent must distinguish acceptance from completion, preserve the handle across interruptions, and confirm the result before reporting success. Repeating the start call because waiting timed out can create another job instead of continuing the first one.

This is an interface problem even when only one agent is involved. The remote operation may remain alive after the tool call, chat session, or worker that initiated it stops waiting.

Define what the first response establishes

Imagine a report-generation tool with this illustrative response:

{"job_id":"job-demo-7","state":"accepted","status_resource":"jobs/job-demo-7"}

It establishes a reference to accepted work under the proposed contract. It does not establish that the report exists, that processing has begun, or that the caller may access every future artifact.

Microsoft's asynchronous request-reply pattern illustrates an HTTP design with acceptance, a status resource, polling guidance, and a separate completed result. It is one implementation pattern, not a requirement that every tool use identical HTTP responses.

Keep state names and terminal outcomes explicit. accepted, running, succeeded, failed, and canceled can work if the implementation defines their transitions and what each observation proves.

Preserve identity before deciding how to wait

Store the job ID, owning operation, creation observation, authorized caller, and next observation method in the task's durable record. Keep secret-bearing status URLs out of public logs; a reference can be sensitive even when its label looks harmless.

If the start response is lost, reconcile through a supported operation identity or an idempotent start contract. A local variable containing a desired job name is not proof that a remote job with that identity exists.

Our checkpoint-and-recovery article discusses uncertain side effects during restart. Here, the narrower problem is tracking work that is intentionally still running and exposing an observation interface that survives a single call.

Choose polling or callbacks around observable state

Method Useful property Responsibility it introduces
Poll a status resource The caller controls when to inspect Respect intervals, budgets, authorization, and terminal states
Receive a callback Completion can be signaled without continual polling Authenticate delivery and handle duplicates, delays, and reordering
Use a hybrid A callback can trigger a fresh status read Reconcile both signals to the same job identity

A callback is evidence about an event, not automatic permission to trust arbitrary content or follow an unexpected URL. A stale running callback must not overwrite a newer confirmed terminal state.

For polling, separate the total waiting budget from the remote operation's allowed lifetime. A status request timing out says that the observation failed. It does not by itself say that the underlying job failed.

Make cancellation a state transition

“Stop waiting” and “cancel the work” are different requests. A cancellation acknowledgement may mean the server accepted a request to cancel; the worker may still be completing an irreversible step.

Define what happens when cancellation races with success. Before telling the user that work was canceled, read the saved terminal outcome and account for any effect already produced. Do not silently discard a valid result because an earlier local timeout expired.

Verify the result behind a successful state

A success response should identify the actual output and its relevant integrity or version information. Confirm that the authorized caller can retrieve the intended artifact and that it belongs to this job. A page saying “done” is insufficient if the linked output is missing or belongs to an earlier run.

Likewise, a failed job should provide a bounded error category and a recovery path without exposing credentials, raw customer data, or an internal exception dump. Keep retrying the same job distinct from starting a fresh attempt.

No report service, callback receiver, or agent tool was executed for this article. The response and transition table are a proposed contract. Its value is a recoverable chain from accepted work to verified outcome, with uncertainty preserved when the available observation cannot settle it.

You may also like