Skip to main content
An agent exposes two ways to execute: run returns a single result once the agent loop finishes, and stream yields events as they arrive. Both take the same per-request input.

One-shot with run

run drives the agent loop to completion, calling the model, executing any tools it requests, and feeding the results back, then returns the final result:
Without an output schema, run resolves to the assistant’s collected text as a string. With one, it resolves to a validated, typed object, so type the call with the matching T.

Streaming

stream yields the raw run as an AG-UI event stream of text deltas, tool calls, and lifecycle events, so you can render output as it is produced:
Narrow on event.type to handle only the chunks you care about.

Request input

Both methods accept a ChatInputType. Every field is optional, so chat.run() with no argument is valid.

Conversation history

Pass prior turns in messages; prompt is always appended last:

Sampling options

temperature, topP, and maxTokens map straight onto the model call:

Cancellation

Pass an AbortController and abort it to cancel an in-flight run:

Metadata vs. context

Both metadata and context ride along with a request, and neither is ever sent to the model, but they serve different roles. metadata is part of the request config: it is shallow-merged across the run and can be read or transformed by middleware (tracing tags, feature flags, a session id, and the like). context carries live dependencies such as the authenticated user, a database client, or an audit logger, which tools and middleware read via ctx.context.
See Middleware for how hooks read both.