Skip to main content
Starts a run that returns an AgentRun handle immediately. Poll Status while waiting, optionally call Cancel, wait on Done(), then Get() for the result. Uses WithApprovalHandler so tool calls can be approved interactively on stdin — same semantics as a blocking Run + Get. Source: examples/agent_with_nonblocking_run/

What it demonstrates

  • Run returning an AgentRun handle immediately
  • Polling AgentRun.Status while the run is in progress
  • AgentRun.Cancel to stop the run (demo: after ~10s if still running — e.g. waiting on approval)
  • Waiting on AgentRun.Done(), then Get()
  • WithApprovalHandler for tool approval during the run
See Run for the full AgentRun method table. AgentStream shares Status / Cancel / Done / Get and adds Events — see Streaming.

Run

From examples/:
When the calculator tool is invoked, type y or n at the approval prompt. If you wait ~10s without answering, the example calls Cancel.

Key code

Cancelling the Get context only unblocks the waiter — use AgentRun.Cancel to cancel the agent run itself. On the Temporal runtime, persist runID before waiting. After a process crash, reconnect with GetAgentRun and call Get again — see Durable Execution. The approval handler receives *agent.ApprovalRequest and calls req.Respond(agent.ApprovalStatusApproved) or Rejected. See Approvals for stream-based approval with Stream.

Expected output

Typing n rejects the tool call and the agent replies without that result. Leaving the prompt unanswered long enough triggers Cancel and a cancelled/failed finish path.

Learn more

Run

AgentRun — Get, Done, Status, Cancel, GetAgentRun

Durable Execution

Reconnect a Run after crash with GetAgentRun

Approvals

Policies, handlers, and stream events