callModel
CallModelInput
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | ((ctx: TurnContext) => string) | Yes* | Model ID (e.g., “openai/gpt-5-nano”) |
models | string[] | Yes* | Model fallback array |
input | OpenResponsesInput | Yes | Input messages or string |
instructions | string | ((ctx: TurnContext) => string) | No | System instructions |
tools | Tool[] | No | Tools available to the model |
maxToolRounds | MaxToolRounds | No | Tool execution limit (deprecated) |
stopWhen | StopWhen | No | Stop conditions |
temperature | number | ((ctx: TurnContext) => number) | No | Sampling temperature (0-2) |
maxOutputTokens | number | ((ctx: TurnContext) => number) | No | Maximum tokens to generate |
topP | number | No | Top-p sampling |
text | ResponseTextConfig | No | Text format configuration |
provider | ProviderPreferences | No | Provider routing and configuration |
topK | number | No | Top-k sampling |
metadata | Record<string, string> | No | Request metadata |
toolChoice | ToolChoice | No | Tool choice configuration |
parallelToolCalls | boolean | No | Enable parallel tool calling |
reasoning | ReasoningConfig | No | Reasoning configuration |
promptCacheKey | string | No | Cache key for prompt caching |
previousResponseId | string | No | Context from previous response |
include | string[] | No | Include extra fields in response |
background | boolean | No | Run request in background |
safetyIdentifier | string | No | User safety identifier |
serviceTier | string | No | Service tier preference |
truncation | string | No | Truncation mode |
plugins | Plugin[] | No | Enabled plugins |
user | string | No | End-user identifier |
sessionId | string | No | Session identifier |
store | boolean | No | Store request data |
context | ContextInput<ToolContextMap> | No | Tool context keyed by tool name |
model or models is required.
ProviderPreferences
Configuration for routing and provider selection.| Parameter | Type | Description |
|---|---|---|
allowFallbacks | boolean | Allow backup providers when primary is unavailable (default: true) |
requireParameters | boolean | Only use providers that support all requested parameters |
dataCollection | "allow" | "deny" | Data collection policy (allow/deny) |
order | string[] | Custom provider routing order |
only | string[] | Restrict to specific providers |
ignore | string[] | Exclude specific providers |
quantizations | string[] | Filter by quantization levels |
sort | string | Load balancing strategy (e.g., “throughput”) |
maxPrice | object | Maximum price limits |
preferredMinThroughput | number | Minimum tokens per second preference |
preferredMaxLatency | number | Maximum latency preference |
RequestOptions
| Parameter | Type | Description |
|---|---|---|
timeout | number | Request timeout in milliseconds |
signal | AbortSignal | Abort signal for cancellation |
ModelResult
Wrapper providing multiple consumption patterns for a response.Methods
getText()
getResponse()
getTextStream()
getReasoningStream()
getNewMessagesStream()
getFullResponsesStream()
getToolCalls()
getToolCallsStream()
getToolStream()
getContextUpdates()
setContext(). Completes when tool execution finishes.
cancel()
Tool Types
tool()
ToolConfig
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Tool name |
description | string | No | Tool description |
inputSchema | ZodObject | Yes | Input parameter schema |
outputSchema | ZodType | No | Output schema |
eventSchema | ZodType | No | Event schema (triggers generator mode) |
contextSchema | ZodObject | No | Context data this tool needs |
execute | function | false | Yes* | Execute function, or false for manual |
onToolCalled | function | Yes* | HITL hook — return value to auto-respond, null to pause |
onResponseReceived | function | No | HITL hook — post-process caller-supplied result (HITL only) |
nextTurnParams | NextTurnParamsFunctions | No | Parameters to modify next turn |
execute or onToolCalled. Omitting both (with execute: false) makes the tool a manual tool.
Tool
Union type of all tool types:ToolWithExecute
Regular tool with execute function:ToolWithGenerator
Generator tool with eventSchema:ManualTool
Tool without execute function:HITLTool
Human-in-the-loop tool withonToolCalled and optional onResponseReceived hooks. outputSchema is required — it validates both the hook’s non-null return value and the caller-supplied response delivered via function_call_output.
null from onToolCalled pauses the loop and sets the conversation status to 'awaiting_hitl'. Throwing from onToolCalled is surfaced as a tool error of the form { error: ... }. Throwing from onResponseReceived is surfaced as an error payload that includes the caller’s original output of the form { error: ..., originalOutput: ... }.
Tool Type Guards
isManualTool— noexecuteand noonToolCalled. Always pauses the loop.isHITLTool— has anonToolCalledfunction.isAutoResolvableTool— either has anexecutefunction (regular/generator) or is a HITL tool. Returnsfalsefor manual and server tools.
Context Types
TurnContext
ToolExecuteContext
Flat context passed to tool execute functions. MergesTurnContext fields with tool-specific context:
ToolContextMap
Context map forcallModel’s context option,
keyed by tool name:
ContextInput
Context can be static, a sync function, or an async function:NextTurnParamsContext
Stream Event Types
EnhancedResponseStreamEvent
ToolStreamEvent
ParsedToolCall
ToolExecutionResult
Stop Conditions
StopWhen
StopCondition
StopConditionContext
StepResult
Warning
Built-in Helpers
| Function | Signature | Description |
|---|---|---|
stepCountIs | (n: number) => StopCondition | Stop after n steps |
hasToolCall | (name: string) => StopCondition | Stop when tool is called |
maxTokensUsed | (n: number) => StopCondition | Stop after n tokens |
maxCost | (amount: number) => StopCondition | Stop after cost limit |
finishReasonIs | (reason: string) => StopCondition | Stop on finish reason |