Run and stream

Run an agent once or read its events as they arrive.

Use run() when you only need the final result. Use stream() when your app should show text or tool activity as it happens.

Both methods need a prompt, a JSON context object, and a sessionId.

Get the final result

const result = await agent.run(
  "Summarize my latest order.",
  { userId: "user-123" },
  { sessionId: "session-123" },
);

The result includes a status, content, messages, tool calls, tool results, and usage when the provider reports it.

Stream events

const { stream, result } = await agent.stream(
  "Summarize my latest order.",
  { userId: "user-123" },
  { sessionId: "session-123" },
);

for await (const event of stream) {
  if (event.type === "message.part.delta" && event.part.type === "text") {
    process.stdout.write(event.part.text);
  }
}

const finalResult = await result;

The stream can include text, tool calls, tool progress, approvals, usage, and the final run status. See events for the event list.

Set run limits

Pass these options in the third argument:

  • maxSteps: maximum number of model calls.
  • toolTimeout: maximum time a tool can run, in milliseconds.
  • authorizationTimeout: maximum time an authorization check can run, in milliseconds.
  • abortSignal: stop the run when your app cancels the signal.
  • runId: set your own run ID. Agentdock creates one if you omit it.
  • sessionNamespace: separate sessions that use the same session ID.

You can set systemPrompt and maxSteps as defaults when you create the agent. Per-run values override defaults.

On this page