API Reference

Complete reference for the callModel API, ModelResult class, tool types, and helper functions.

callModel

1function callModel(request: CallModelInput, options?: RequestOptions): ModelResult

Creates a response using the OpenResponses API with multiple consumption patterns.

CallModelInput

ParameterTypeRequiredDescription
modelstring | ((ctx: TurnContext) => string)Yes*Model ID (e.g., “openai/gpt-5-nano”)
modelsstring[]Yes*Model fallback array
inputOpenResponsesInputYesInput messages or string
instructionsstring | ((ctx: TurnContext) => string)NoSystem instructions
toolsTool[]NoTools available to the model
maxToolRoundsMaxToolRoundsNoTool execution limit (deprecated)
stopWhenStopWhenNoStop conditions
temperaturenumber | ((ctx: TurnContext) => number)NoSampling temperature (0-2)
maxOutputTokensnumber | ((ctx: TurnContext) => number)NoMaximum tokens to generate
topPnumberNoTop-p sampling
textResponseTextConfigNoText format configuration
providerProviderPreferencesNoProvider routing and configuration
topKnumberNoTop-k sampling
metadataRecord<string, string>NoRequest metadata
toolChoiceToolChoiceNoTool choice configuration
parallelToolCallsbooleanNoEnable parallel tool calling
reasoningReasoningConfigNoReasoning configuration
promptCacheKeystringNoCache key for prompt caching
previousResponseIdstringNoContext from previous response
includestring[]NoInclude extra fields in response
backgroundbooleanNoRun request in background
safetyIdentifierstringNoUser safety identifier
serviceTierstringNoService tier preference
truncationstringNoTruncation mode
pluginsPlugin[]NoEnabled plugins
userstringNoEnd-user identifier
sessionIdstringNoSession identifier
storebooleanNoStore request data

*Either model or models is required.

ProviderPreferences

Configuration for routing and provider selection.

ParameterTypeDescription
allowFallbacksbooleanAllow backup providers when primary is unavailable (default: true)
requireParametersbooleanOnly use providers that support all requested parameters
dataCollection"allow" | "deny"Data collection policy (allow/deny)
orderstring[]Custom provider routing order
onlystring[]Restrict to specific providers
ignorestring[]Exclude specific providers
quantizationsstring[]Filter by quantization levels
sortstringLoad balancing strategy (e.g., “throughput”)
maxPriceobjectMaximum price limits
preferredMinThroughputnumberMinimum tokens per second preference
preferredMaxLatencynumberMaximum latency preference

RequestOptions

ParameterTypeDescription
timeoutnumberRequest timeout in milliseconds
signalAbortSignalAbort signal for cancellation

ModelResult

Wrapper providing multiple consumption patterns for a response.

Methods

getText()

1getText(): Promise<string>

Get text content after tool execution completes.

getResponse()

1getResponse(): Promise<OpenResponsesNonStreamingResponse>

Get full response with usage data (inputTokens, outputTokens, cachedTokens).

getTextStream()

1getTextStream(): AsyncIterableIterator<string>

Stream text deltas.

getReasoningStream()

1getReasoningStream(): AsyncIterableIterator<string>

Stream reasoning deltas (for reasoning models).

getNewMessagesStream()

1getNewMessagesStream(): AsyncIterableIterator<ResponsesOutputMessage | OpenResponsesFunctionCallOutput>

Stream incremental message updates in OpenResponses format.

getFullResponsesStream()

1getFullResponsesStream(): AsyncIterableIterator<EnhancedResponseStreamEvent>

Stream all events including tool preliminary results.

getToolCalls()

1getToolCalls(): Promise<ParsedToolCall[]>

Get all tool calls from initial response.

getToolCallsStream()

1getToolCallsStream(): AsyncIterableIterator<ParsedToolCall>

Stream tool calls as they complete.

getToolStream()

1getToolStream(): AsyncIterableIterator<ToolStreamEvent>

Stream tool deltas and preliminary results.

cancel()

1cancel(): Promise<void>

Cancel the stream and all consumers.


Tool Types

tool()

1function tool<TInput, TOutput>(config: ToolConfig): Tool

Create a typed tool with Zod schema validation.

ToolConfig

ParameterTypeRequiredDescription
namestringYesTool name
descriptionstringNoTool description
inputSchemaZodObjectYesInput parameter schema
outputSchemaZodTypeNoOutput schema
eventSchemaZodTypeNoEvent schema (triggers generator mode)
executefunction | falseYesExecute function or false for manual
nextTurnParamsNextTurnParamsFunctionsNoParameters to modify next turn

Tool

Union type of all tool types:

1type Tool =
2 | ToolWithExecute<ZodObject, ZodType>
3 | ToolWithGenerator<ZodObject, ZodType, ZodType>
4 | ManualTool<ZodObject, ZodType>;

ToolWithExecute

Regular tool with execute function:

1interface ToolWithExecute<TInput, TOutput> {
2 type: ToolType.Function;
3 function: {
4 name: string;
5 description?: string;
6 inputSchema: TInput;
7 outputSchema?: TOutput;
8 execute: (params: z.infer<TInput>, context?: TurnContext) => Promise<z.infer<TOutput>>;
9 };
10}

ToolWithGenerator

Generator tool with eventSchema:

1interface ToolWithGenerator<TInput, TEvent, TOutput> {
2 type: ToolType.Function;
3 function: {
4 name: string;
5 description?: string;
6 inputSchema: TInput;
7 eventSchema: TEvent;
8 outputSchema: TOutput;
9 execute: (params: z.infer<TInput>, context?: TurnContext) => AsyncGenerator<z.infer<TEvent>>;
10 };
11}

ManualTool

Tool without execute function:

1interface ManualTool<TInput, TOutput> {
2 type: ToolType.Function;
3 function: {
4 name: string;
5 description?: string;
6 inputSchema: TInput;
7 outputSchema?: TOutput;
8 };
9}

Context Types

TurnContext

1interface TurnContext {
2 toolCall?: OpenResponsesFunctionToolCall;
3 numberOfTurns: number;
4 turnRequest?: OpenResponsesRequest;
5}

NextTurnParamsContext

1interface NextTurnParamsContext {
2 input: OpenResponsesInput;
3 model: string;
4 models: string[];
5 temperature: number | null;
6 maxOutputTokens: number | null;
7 topP: number | null;
8 topK?: number | undefined;
9 instructions: string | null;
10}

Stream Event Types

EnhancedResponseStreamEvent

1type EnhancedResponseStreamEvent =
2 | OpenResponsesStreamEvent
3 | ToolPreliminaryResultEvent;

ToolStreamEvent

1type ToolStreamEvent =
2 | { type: 'delta'; content: string }
3 | { type: 'preliminary_result'; toolCallId: string; result: unknown };

ParsedToolCall

1interface ParsedToolCall {
2 id: string;
3 name: string;
4 arguments: unknown;
5}

ToolExecutionResult

1interface ToolExecutionResult {
2 toolCallId: string;
3 toolName: string;
4 result: unknown;
5 preliminaryResults?: unknown[];
6 error?: Error;
7}

Stop Conditions

StopWhen

1type StopWhen =
2 | StopCondition
3 | StopCondition[];

StopCondition

1type StopCondition = (context: StopConditionContext) => boolean | Promise<boolean>;

StopConditionContext

1interface StopConditionContext {
2 steps: StepResult[];
3}

StepResult

1interface StepResult {
2 stepType: 'initial' | 'continue';
3 text: string;
4 toolCalls: TypedToolCallUnion[];
5 toolResults: ToolExecutionResultUnion[];
6 response: OpenResponsesNonStreamingResponse;
7 usage?: OpenResponsesUsage;
8 finishReason?: string;
9 warnings?: Warning[];
10 experimental_providerMetadata?: Record<string, unknown>;
11}

Warning

1interface Warning {
2 type: string;
3 message: string;
4}

Built-in Helpers

FunctionSignatureDescription
stepCountIs(n: number) => StopConditionStop after n steps
hasToolCall(name: string) => StopConditionStop when tool is called
maxTokensUsed(n: number) => StopConditionStop after n tokens
maxCost(amount: number) => StopConditionStop after cost limit
finishReasonIs(reason: string) => StopConditionStop on finish reason

Format Helpers

fromChatMessages

1function fromChatMessages(messages: Message[]): OpenResponsesInput

Convert OpenAI chat format to OpenResponses input.

toChatMessage

1function toChatMessage(response: OpenResponsesNonStreamingResponse): AssistantMessage

Convert response to chat message format.

fromClaudeMessages

1function fromClaudeMessages(messages: ClaudeMessageParam[]): OpenResponsesInput

Convert Anthropic Claude format to OpenResponses input.

toClaudeMessage

1function toClaudeMessage(response: OpenResponsesNonStreamingResponse): ClaudeMessage

Convert response to Claude message format.


Type Utilities

InferToolInput

1type InferToolInput<T> = T extends { function: { inputSchema: infer S } }
2 ? S extends ZodType ? z.infer<S> : unknown
3 : unknown;

InferToolOutput

1type InferToolOutput<T> = T extends { function: { outputSchema: infer S } }
2 ? S extends ZodType ? z.infer<S> : unknown
3 : unknown;

InferToolEvent

1type InferToolEvent<T> = T extends { function: { eventSchema: infer S } }
2 ? S extends ZodType ? z.infer<S> : never
3 : never;

TypedToolCall

1type TypedToolCall<T extends Tool> = {
2 id: string;
3 name: T extends { function: { name: infer N } } ? N : string;
4 arguments: InferToolInput<T>;
5};

Exports

1// Main SDK
2export { OpenRouter } from '@openrouter/sdk';
3
4// Tool helpers
5export { tool, ToolType } from '@openrouter/sdk';
6
7// Format helpers
8export { fromChatMessages, toChatMessage, fromClaudeMessages, toClaudeMessage } from '@openrouter/sdk';
9
10// Stop condition helpers
11export { stepCountIs, hasToolCall, maxTokensUsed, maxCost, finishReasonIs } from '@openrouter/sdk';
12
13// Types
14export type {
15 CallModelInput,
16 Tool,
17 ToolWithExecute,
18 ToolWithGenerator,
19 ManualTool,
20 TurnContext,
21 ParsedToolCall,
22 ToolExecutionResult,
23 StopCondition,
24 StopWhen,
25 MaxToolRounds,
26 InferToolInput,
27 InferToolOutput,
28 InferToolEvent,
29} from '@openrouter/sdk';