Skip to content

Agent

An agent bundles the configuration that defines how your system should respond: which model to call, which instructions to inject, and which tools to expose. The same agent object can serve many users because it does not capture request-specific state; context always arrives at run time.

AgentParams collect the knobs you use to describe a capability. The most common ones are shown below; every language shares the same concepts even if the syntax differs.

params.ts
interface AgentParams<TContext> {
/**
* A unique name for the agent.
*/
name: string;
/**
* The default language model to use for the agent.
*/
model: LanguageModel;
/**
* Instructions to be added to system messages when executing the agent.
* This can include formatting instructions or other guidance for the agent.
* @default []
*/
instructions?: InstructionParam<TContext>[];
/**
* The tools that the agent can use to perform tasks.
* @default []
*/
tools?: AgentTool<TContext>[];
/**
* The expected format of the response. Either text or json.
* @default { type: "text" }
*/
response_format?: ResponseFormatOption;
/**
* Max number of turns for agent to run to protect against infinite loops.
* @default 10
*/
max_turns?: number;
/**
* Amount of randomness injected into the response. Ranges from 0.0 to 1.0
* @default undefined
*/
temperature?: number;
/**
* An alternative to sampling with temperature, called nucleus sampling,
* where the model considers the results of the tokens with top_p probability mass.
* Ranges from 0.0 to 1.0
* @default undefined
*/
top_p?: number;
/**
* Only sample from the top K options for each subsequent token.
* Used to remove 'long tail' low probability responses.
* Must be a non-negative integer.
* @default undefined
*/
top_k?: number;
/**
* Positive values penalize new tokens based on whether they appear in the text so far,
* increasing the model's likelihood to talk about new topics.
* @default undefined
*/
presence_penalty?: number;
/**
* Positive values penalize new tokens based on their existing frequency in the text so far,
* decreasing the model's likelihood to repeat the same line verbatim.
* @default undefined
*/
frequency_penalty?: number;
/**
* The modalities that the model should support.
* @default undefined
*/
modalities?: Modality[];
/**
* Options for audio generation.
* @default undefined
*/
audio?: AudioOptions;
/**
* Options for reasoning generation.
* @default undefined
*/
reasoning?: ReasoningOptions;
/**
* Optional toolkits that can provide dynamic tools and system prompts for each session.
* @default undefined
*/
toolkits?: Toolkit<TContext>[];
}
  • name shows up in logs and telemetry.
  • model points to the LanguageModel instance from the SDK.
  • instructions enrich the system prompt. They can be static strings or functions that read the per-run context.
  • tools list executable helpers the model may call.
  • toolkits produce additional tools and system prompts at session time.
  • Sampling and output options (temperature, response_format, modalities, etc.) mirror the knobs on the underlying LLMs.
  • max_turns guards against infinite tool-calling loops.

Because these parameters are pure data, you usually construct an agent once at startup and reuse it.

Instructions can depend on the context you provide when running the agent. Returning a string from a function is enough; the library aggregates them into a single system prompt before each run.

Toolkits let you derive tools and extra prompt snippets dynamically. Each toolkit generates a per-session object that stays alive until you close the run session, which is useful for holding onto database connections or scoped caches.

toolkit.ts
interface Toolkit<TContext> {
/**
* Create a new toolkit session for the supplied context value.
* The function should also intialize the ToolkitSession instance with instructions and tools.
*/
createSession(context: TContext): Promise<ToolkitSession<TContext>>;
}
interface ToolkitSession<TContext> {
/**
* Retrieve the current system prompt for the session.
*/
getSystemPrompt(): string | undefined;
/**
* Retrieve the current set of tools that should be available to the session.
*/
getTools(): AgentTool<TContext>[];
/**
* Release any resources that were allocated for the session.
*/
close(): Promise<void> | void;
}

Use toolkits sparingly for capabilities that need per-user initialisation. For simple helper functions that do not keep state, define them directly in tools instead.

Agents remain stateless. When you call agent.run, agent.run_stream, or agent.create_session, the library creates a RunSession that binds the agent parameters to the context you pass in. That session resolves dynamic instructions, opens toolkit sessions, and keeps them around for the duration of the run. When you close the run session, those resources are released.

See Agent vs Run session for a deeper dive into how the pieces fit together and when to choose each entry point.