Skip to main content

agent.ts

Source: sdks/typescript/packages/sdk/src/agent.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.

Type alias PlanResult​

What plan returns: a tool call (tool, input) or the final answer (final), with optional tokens and cost.

Definition: { tool: string; input: unknown; tokens?: number; cost?: number } \| { final: unknown; tokens?: number; cost?: number }.

Interface VerifyResult​

Result of verify for one tool step: ok and an optional feedback.

Fields:

NameTypeRequiredDescription
okbooleanyesWhether the exercise succeeded.
feedbackstringnoOptional feedback kept in the history for the next plan call.

Interface LoopStep​

One tool step of runAgentLoop: the tool, its input, its output or error and the verification result.

Fields:

NameTypeRequiredDescription
stepnumberyesCurrent step, free text (up to 500 characters).
toolstringyesName of the tool called.
inputunknownyesValue that was refused.
outputunknownnoOutput of the operation (transcript, image references...).
errorstringnoError message when the tool call failed.
verifiedbooleanyesTrue when the call succeeded and verify accepted it.
feedbackstringnoFeedback of verify, or the error message when the tool failed.

Interface LoopBudget​

Limits of runAgentLoop on tokens, cost and duration.

Fields:

NameTypeRequiredDescription
maxTokensnumbernoMaximum output tokens; when set, it is the output assumed for the worst case.
maxCostnumbernoCost limit in the currency unit used by plan results.
maxDurationMsnumbernoMaximum duration of the loop in milliseconds.

Interface AgentLoopOptions​

Options of runAgentLoop: the tools, the plan and verify callbacks, the step limit, the budget and the kill switch.

Fields:

NameTypeRequiredDescription
toolsTool<anyyesTools its external agents may invoke (names or patterns such as crm.*).
plan(history: LoopStep[], step: number) => Promise<PlanResult> | PlanResultyesPlan: chooses the next tool call or returns the final answer.
verify(s: LoopStep) => Promise<VerifyResult> | VerifyResultnoVerify: checks each tool result (default accepts every successful call).
maxStepsnumbernoMaximum number of steps (default 10); reaching it throws agent_max_steps.
budgetLoopBudgetnoToken, cost and duration limits merged into the runner limits; going over them throws agent_budget.
killSwitch() => Promise<boolean> | booleannoReturns true when the agent must stop (kill switch). Checked before every model and tool call.
onStep(s: LoopStep) => Promise<void> | voidnoCalled after each tool step, for example to POST a heartbeat.
signalAbortSignalnoAbortSignal that cancels the loop; an abort throws agent_aborted.
localeLocalenoLanguage of the error messages, en (default) or fr.
agentIdstringnoAgent id passed to the runner and named in the agent_killed error message.

Interface AgentLoopResult​

Result of runAgentLoop: the final answer, the tool steps and the usage (tokens, cost, duration in milliseconds).

Fields:

NameTypeRequiredDescription
finalunknownyesTrue when this is the last step of the run. Default false.
stepsLoopStep[]yesEvery tool step run by the loop, in order.
usage{ tokens: number; cost: number; durationMs: number }yesUsage counters added to the budget consumption, for example input_tokens, output_tokens.

Functions​

runAgentLoop​

async runAgentLoop(o: AgentLoopOptions): Promise<AgentLoopResult>

plan / act / verify loop with a step limit, a budget and a kill switch. Delegates to AgentRunner: plan plays the model, continuation rules are off.

NameTypeDefaultRequiredDescription
oAgentLoopOptionsyesPer call options (idempotency key, traceparent, headers).

Returns: Promise<AgentLoopResult>. Final answer, tool steps and usage; throws AgentLoopError on step limit, budget, abort or kill switch.

Raises: AgentLoopError.