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
contextContextInput<ToolContextMap>NoTool context keyed by tool name

*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 cumulative message snapshots 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.

getContextUpdates()

1getContextUpdates(): AsyncGenerator<ToolContextMap<TTools>>

Stream context snapshots whenever a tool calls setContext(). Completes when tool execution finishes.

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)
contextSchemaZodObjectNoContext data this tool needs
executefunction | falseYes*Execute function, or false for manual
onToolCalledfunctionYes*HITL hook — return value to auto-respond, null to pause
onResponseReceivedfunctionNoHITL hook — post-process caller-supplied result (HITL only)
nextTurnParamsNextTurnParamsFunctionsNoParameters to modify next turn

* Provide exactly one of execute or onToolCalled. Omitting both (with execute: false) makes the tool a manual tool.

Tool

Union type of all tool types:

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

ToolWithExecute

Regular tool with execute function:

1interface ToolWithExecute<
2 TInput, TOutput, TContext, TName
3> {
4 type: ToolType.Function;
5 function: {
6 name: TName;
7 description?: string;
8 inputSchema: TInput;
9 outputSchema?: TOutput;
10 contextSchema?: ZodObject;
11 execute: (
12 params: z.infer<TInput>,
13 context: ToolExecuteContext<TName, TContext>,
14 ) => Promise<z.infer<TOutput>>;
15 };
16}

ToolWithGenerator

Generator tool with eventSchema:

1interface ToolWithGenerator<
2 TInput, TEvent, TOutput, TContext, TName
3> {
4 type: ToolType.Function;
5 function: {
6 name: TName;
7 description?: string;
8 inputSchema: TInput;
9 eventSchema: TEvent;
10 outputSchema: TOutput;
11 contextSchema?: ZodObject;
12 execute: (
13 params: z.infer<TInput>,
14 context: ToolExecuteContext<TName, TContext>,
15 ) => AsyncGenerator<z.infer<TEvent>>;
16 };
17}

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}

HITLTool

Human-in-the-loop tool with onToolCalled 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.

1interface HITLToolFunction<
2 TInput, TOutput, TContext, TName
3> {
4 name: TName;
5 description?: string;
6 inputSchema: TInput;
7 outputSchema: TOutput;
8 contextSchema?: ZodObject;
9 onToolCalled: (
10 params: z.infer<TInput>,
11 context?: ToolExecuteContext<TName, TContext>,
12 ) => Promise<z.infer<TOutput> | null> | z.infer<TOutput> | null;
13 onResponseReceived?: (
14 rawResult: unknown,
15 context?: ToolExecuteContext<TName, TContext>,
16 ) => Promise<z.infer<TOutput>> | z.infer<TOutput>;
17 toModelOutput?: ToModelOutputFunction<
18 z.infer<TInput>,
19 z.infer<TOutput>
20 >;
21}
22
23type HITLTool<TInput, TOutput, TContext> = {
24 type: ToolType.Function;
25 function: HITLToolFunction<TInput, TOutput, TContext>;
26};

Returning 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

1function isManualTool(tool: Tool): tool is ManualTool;
2function isHITLTool(tool: Tool): tool is HITLTool;
3function isAutoResolvableTool(
4 tool: Tool,
5): tool is ToolWithExecute | ToolWithGenerator | HITLTool;
  • isManualTool — no execute and no onToolCalled. Always pauses the loop.
  • isHITLTool — has an onToolCalled function.
  • isAutoResolvableTool — either has an execute function (regular/generator) or is a HITL tool. Returns false for manual and server tools.

Context Types

TurnContext

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

ToolExecuteContext

Flat context passed to tool execute functions. Merges TurnContext fields with tool-specific context:

1type ToolExecuteContext<TName, TContext> =
2 TurnContext & {
3 tools: {
4 readonly [K in TName]: Readonly<TContext>;
5 };
6 setContext(partial: Partial<TContext>): void;
7 };

ToolContextMap

Context map for callModel’s context option, keyed by tool name:

1type ToolContextMap<T extends readonly Tool[]> = {
2 [K in T[number] as K['function']['name']]:
3 InferToolContext<K>;
4};

ContextInput

Context can be static, a sync function, or an async function:

1type ContextInput<T> =
2 | T
3 | ((turn: TurnContext) => T)
4 | ((turn: TurnContext) => Promise<T>);

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// Agent client
2export { OpenRouter } from '@openrouter/agent';
3
4// Tool helpers
5export {
6 tool,
7 ToolType,
8 isManualTool,
9 isHITLTool,
10 isAutoResolvableTool,
11} from '@openrouter/agent';
12
13// Format helpers
14export { fromChatMessages, toChatMessage, fromClaudeMessages, toClaudeMessage } from '@openrouter/agent';
15
16// Stop condition helpers
17export { stepCountIs, hasToolCall, maxTokensUsed, maxCost, finishReasonIs } from '@openrouter/agent';
18
19// Context helpers
20export {
21 buildToolExecuteContext,
22 ToolContextStore,
23} from '@openrouter/agent';
24
25// Types
26export type {
27 CallModelInput,
28 ContextInput,
29 Tool,
30 ToolWithExecute,
31 ToolWithGenerator,
32 ManualTool,
33 HITLTool,
34 HITLToolFunction,
35 ToolExecuteContext,
36 ToolContextMap,
37 TurnContext,
38 ParsedToolCall,
39 ToolExecutionResult,
40 StopCondition,
41 StopWhen,
42 InferToolInput,
43 InferToolOutput,
44 InferToolEvent,
45} from '@openrouter/agent';