@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 anAssistantMessageEventStream that emits events:
text- Text content deltathinking- Reasoning content (for extended thinking models)tool_call- Tool invocation requestusage- Token usage informationstop- Generation finishederror- Error occurred
Example
streamSimple
Stream LLM responses with unified reasoning parameter.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 fromstream().
AssistantMessage after streaming finishes.
Example
completeSimple
Wait for the full response fromstreamSimple().
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.text- Text content deltathinking- Thinking content deltatool_call- Tool call requestusage- Token usagestop- Stream finishederror- Error occurred
abort
Cancel the stream.Example
Context Type
ThinkingLevel Type
"xhigh" is only supported by GPT-5.2/5.3 models and Anthropic Opus 4.6 models.