Skip to main content
Starts a run in the background and receives the final result on a channel. Uses WithApprovalHandler so tool calls can be approved interactively on stdin — same semantics as Run. Source: examples/agent_with_run_async/

What it demonstrates

  • RunAsync returning <-chan AgentRunAsyncResult
  • WithApprovalHandler for tool approval during async runs
  • Integrating agents into non-blocking application loops

Run

From examples/:
go run ./agent_with_run_async "What is 15 + 27?"
When the calculator tool is invoked, type y or n at the approval prompt.

Key code

a, err := agent.NewAgent(
    agent.WithLLMClient(llmClient),
    agent.WithToolRegistry(reg),
    agent.WithApprovalHandler(approvalHandler),
)

resultCh, err := a.RunAsync(ctx, prompt, nil)
res := <-resultCh
if res.Error != nil {
    log.Fatal(res.Error)
}
fmt.Println(res.Result.Content)
The approval handler receives *agent.ApprovalRequest and calls req.Respond(agent.ApprovalStatusApproved) or Rejected. See Approvals for stream-based approval with Stream.

Expected output

Tool call: calculator({"expression":"15+27"})
Approve? [y/n]: y
15 + 27 = 42
Typing n rejects the tool call and the agent replies without that result.

Learn more

Approvals

Policies, handlers, and stream events

Configuration

Run, RunAsync, and Stream