Skip to main content
The @mariozechner/pi-ai package provides functions for streaming LLM responses and awaiting completion results.

Import

stream

Stream LLM responses with provider-specific options.
Model<TApi>
required
The model to use for generation
Context
required
Conversation context with system prompt, messages, and tools
ProviderStreamOptions

Return Value

Returns an AssistantMessageEventStream that emits events:
  • text - Text content delta
  • thinking - Reasoning content (for extended thinking models)
  • tool_call - Tool invocation request
  • usage - Token usage information
  • stop - Generation finished
  • error - Error occurred

Example

streamSimple

Stream LLM responses with unified reasoning parameter.
Same as stream() but accepts a reasoning parameter instead of provider-specific thinking options.
ThinkingLevel
Thinking level: "off", "minimal", "low", "medium", "high", or "xhigh"Automatically mapped to provider-specific parameters (e.g., thinking.type for OpenAI, thinking.enabled for Anthropic).
ThinkingBudgets
Custom token budgets for thinking levels (token-based providers only)

Example

complete

Wait for the full response from stream().
Returns the complete AssistantMessage after streaming finishes.

Example

completeSimple

Wait for the full response from streamSimple().

Example

AssistantMessageEventStream

Event stream interface with async iteration and helper methods.

Methods

result

Wait for the complete assistant message.

on

Subscribe to specific event types.
Event Types:
  • text - Text content delta
  • thinking - Thinking content delta
  • tool_call - Tool call request
  • usage - Token usage
  • stop - Stream finished
  • error - Error occurred

abort

Cancel the stream.

Example

Context Type

ThinkingLevel Type

Note: "xhigh" is only supported by GPT-5.2/5.3 models and Anthropic Opus 4.6 models.