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:
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 — text deltas, tool calls, and lifecycle events — so you can render output as it is produced:
event.type to handle only the chunks you care about.
Request input
Both methods accept aChatInputType. Every field is optional, so chat.run() with no argument is valid.
| Field | Type | Description |
|---|---|---|
prompt | string | The user message, appended as the trailing turn. |
messages | MessageType[] | Prior conversation turns prepended before prompt. |
systemPrompts | string[] | Extra system prompts, appended after the agent’s base prompts. |
tools | AiToolClassType[] | Extra tools for this call only, added to the agent’s own. |
middlewares | AiMiddlewareClassType[] | Extra middleware for this call only. |
temperature | number | Output randomness, range [0.0, 2.0]. |
topP | number | Nucleus sampling threshold. |
maxTokens | number | Maximum tokens to generate. |
outputSchema | AssertType | A schema for structured output. |
metadata | Record<string, unknown> | Request-scoped data carried through middleware — never sent to the model. |
context | unknown | Runtime dependencies for tools and middleware — never sent to the model. |
conversationId | string | Identifier for tracking a conversation. |
abortController | AbortController | Cancels the in-flight request. |
agentLoopStrategy | AgentLoopStrategy | Controls how the agent loop iterates (TanStack AI). |
Conversation history
Pass prior turns inmessages; prompt is always appended last:
Sampling options
temperature, topP, and maxTokens map straight onto the model call:
Cancellation
Pass anAbortController and abort it to cancel an in-flight run:
Metadata vs. context
Bothmetadata and context ride along with a request and neither is ever sent to the model — but they serve different roles:
metadatais part of the request config. It is shallow-merged across the run and can be read or transformed by middleware (e.g. tracing tags, feature flags, a session id).contextcarries live dependencies — the authenticated user, a database client, an audit logger — that tools and middleware read viactx.context.