Skip to main content

agent/runner.ts

Source: sdks/typescript/packages/sdk/src/agent/runner.ts. Back to the TypeScript SDK reference index.

note

This page is generated by tools/docs/gen_sdk_reference.py from the SDK sources and the OpenAPI text folders. Do not edit it by hand: change the source and regenerate.

Interface ToolCall​

Tool call requested by a model: identifier, type function, name and arguments.

Fields:

NameTypeRequiredDescription
idstringyesIdentifier of the call.
type"function"yesType of the call, always function.
function{ name: string; arguments: string }yesFunction called: name and arguments as a JSON string.
inputunknownnoAlready parsed input; when set it wins over function.arguments.

Interface AgentMessage​

Chat message of a turn in the OpenAI format: role, content, tool calls and tool call id.

Fields:

NameTypeRequiredDescription
role"system" | "user" | "assistant" | "tool"yessystem, user, assistant or tool.
contentstring | nullyesText of the message; null for an assistant message that only calls tools.
tool_callsToolCall[]noTool calls requested by an assistant message.
tool_call_idstringnoId of the tool call this tool message answers.
namestringnoName of the tool, on a tool message.

Interface ModelResponse​

Answer of a model call: the assistant message, the finish reason, tokens, cost and raw response.

Fields:

NameTypeRequiredDescription
messageAgentMessageyesStatus message (up to 2000 characters).
finishReasonstring | nullyesFinish reason of the model, for example stop, length or tool_calls.
tokensnumbernoTokens used by the call, added to the turn budget.
costnumbernoCost of the call, added to the turn budget.
rawunknownnoRaw gateway response.

Interface ModelRequest​

What the runner gives a model call: messages, tool declarations and step number.

Fields:

NameTypeRequiredDescription
messagesAgentMessage[]yesMessages of the turn so far.
toolsArray<{ type: "function"; function: { name: string; description: string; parameters: Record<string, unknown> } }>yesTools its external agents may invoke (names or patterns such as crm.*).
stepnumberyesCurrent step, free text (up to 500 characters).

Type alias ModelCaller​

Replaces the gateway call (tests, custom routing).

Definition: (req: ModelRequest) => Promise<ModelResponse> \| ModelResponse.

Interface RunnerLimits​

Limits of a turn of AgentRunner: model calls, tool calls, cost, tokens and duration.

Fields:

NameTypeRequiredDescription
maxStepsnumbernoModel calls per turn (default 50).
maxToolCallsnumbernoMaximum tool calls per turn; reaching it stops with max_tool_calls.
maxCostnumbernoCost limit in the currency unit reported by the gateway.
maxTokensnumbernoMaximum output tokens; when set, it is the output assumed for the worst case.
maxDurationMsnumbernoMaximum duration of the turn in milliseconds; going over it stops with timeout.

Type alias StopReason​

Why a turn ended: completed, stopped, aborted, waiting_approval, max_steps, max_tool_calls, budget, timeout or no_progress.

Definition: \| "completed" \| "stopped" \| "aborted" \| "waiting_approval" \| "max_steps" \| "max_tool_calls" \| "budget" \| "timeout" \| "no_progress".

Interface RunnerStep​

One model or tool step of a turn, as passed to onStep and reported to the run.

Fields:

NameTypeRequiredDescription
indexnumberyesPosition of the step in the turn, from 1.
type"model" | "tool"yesmodel or tool.
namestringyesTool name, or the model name for a model step.
inputunknownnoValue that was refused.
outputunknownnoOutput of the operation (transcript, image references...).
errorstringnoError message of a failed tool step.
status"succeeded" | "failed" | "waiting_approval"yessucceeded, failed or waiting_approval.
kindToolKindnoWhat a tool does, read by the continuation rules (exploration, verify_before_done).
tokensnumbernoTokens used by a model step.
costnumbernoCost of a model step.
durationMsnumberyesDuration of the step in milliseconds.
toolCallsnumbernoModel step: number of tool calls requested.
finishReasonstring | nullnoFinish reason of a model step.

Interface TurnResult​

Result of AgentRunner.runTurn.

Fields:

NameTypeRequiredDescription
finalTextstringyesLast text answer of the model.
messagesAgentMessage[]yesAll messages of the turn, input, nudges and tool results included.
stepsRunnerStep[]yesModel and tool steps of the turn, in order.
stopReasonStopReasonyesWhy the turn ended.
stopDetailstringnoHuman readable cause for a non completed turn (limit, heartbeat reason).
nudgesRecord<stringyesNudges sent in the turn, per rule code.
usage{ tokens: number; cost: number; durationMs: number; modelCalls: number; toolCalls: number }yesUsage counters added to the budget consumption, for example input_tokens, output_tokens.
approvalDocnoSet when stopReason is waiting_approval.

Interface AgentRunnerOptions​

Options of AgentRunner: model or callModel, tools, run ids, limits, rules, heartbeat, kill switch and reporting.

Fields:

NameTypeRequiredDescription
clientEnterpriseAInoEnterpriseAI client used for the gateway, the tools, MCP and the run reports.
modelstringnoGateway model or route alias.
effortstringnoReasoning effort sent as reasoning_effort.
callModelModelCallernoReplaces the gateway call (tests, custom routing).
toolsTool<anynoTools its external agents may invoke (names or patterns such as crm.*).
remoteToolsRemoteToolSpec[]noTools run server side by the Tool Gateway (POST /v1/tools/{name}/invoke).
mcpToolsMcpToolSpec[]noTools of registered MCP servers, called through the MCP gateway (tools/call).
agentIdstringnoAgent id sent as agent_id to the gateway and the Tool Gateway.
sessionIdstringnoSession id sent as session_id to the gateway.
runIdstringnoRun id: sent as run_id and, with a client, enables step and checkpoint reports.
taskIdstringnoTask id sent as task_id to the gateway.
dataClassificationstringnoData classification sent as data_classification with model and remote tool calls.
limitsRunnerLimitsnoStep, tool call, cost, token and duration limits of a turn.
rulesContinuationRule[]noContinuation rules (default defaultRules()). Pass [] to disable them.
deliveryRequiredbooleannoThe workflow needs a tool to run before the answer (delivery rule).
auditModebooleannoAudit mode: exploration cap 8 instead of 4.
heartbeatHeartbeatHandlenoHeartbeat handle; beaten before every model call, stopRequested checked before every call.
shouldStop() => Promise<boolean> | booleannoExtra kill switch, checked before every model and tool call.
signalAbortSignalnoAbortSignal passed to every call; an abort ends the turn with aborted.
localeLocalenoLanguage of the messages produced by the runner, en (default) or fr.
reportStepsbooleannoPOST /v1/runs/{id}/steps for each call (default true when client and runId are set).
checkpointsbooleannoPOST /v1/runs/{id}/checkpoints after tool steps and on stop (default true when client and runId are set).
onStep(s: RunnerStep) => Promise<void> | voidnoCalled with each model and tool step.
onError(err: unknown) => voidnoReporting errors (steps, checkpoints) never fail the turn; they land here.

Class AgentRunner​

Model then tools, repeated until the final answer, then the Navin continuation rules.

Attributes:

NameTypeRequiredDescription
optionsAgentRunnerOptionsyesOptions given to the constructor.

Constructor AgentRunner​

new AgentRunner(options: AgentRunnerOptions)

Indexes the local, remote and MCP tools and takes defaultRules() when no rules are given; throws invalid_request without a client or a callModel.

NameTypeDefaultRequiredDescription
optionsAgentRunnerOptionsyesPer call options (idempotency key, traceparent, headers, abort signal).

AgentRunner.runTurn​

async runTurn(input: AgentMessage[]): Promise<TurnResult>

Runs one user turn until the final answer or a stop.

NameTypeDefaultRequiredDescription
inputAgentMessage[]yesMessages of the conversation so far; the last user message is read by the go-ahead rule.

Returns: Promise<TurnResult>. Final text, messages, steps, stop reason, nudges sent and usage of the turn.