Agent.generate()
The generate()
method is used to interact with an agent to produce text or structured responses. This method accepts messages
and an optional options
object as parameters.
Parameters
messages
The messages
parameter can be:
- A single string
- An array of strings
- An array of message objects with
role
andcontent
properties
The message object structure:
interface Message {
role: 'system' | 'user' | 'assistant';
content: string;
}
options
(Optional)
An optional object that can include configuration for output structure, memory management, tool usage, telemetry, and more.
abortSignal?:
AbortSignal
Signal object that allows you to abort the agent's execution. When the signal is aborted, all ongoing operations will be terminated.
context?:
CoreMessage[]
Additional context messages to provide to the agent.
experimental_output?:
Zod schema | JsonSchema7
Enables structured output generation alongside text generation and tool calls. The model will generate responses that conform to the provided schema.
instructions?:
string
Custom instructions that override the agent's default instructions for this specific generation. Useful for dynamically modifying agent behavior without creating a new agent instance.
output?:
Zod schema | JsonSchema7
Defines the expected structure of the output. Can be a JSON Schema object or a Zod schema.
maxSteps?:
number
= 5
Maximum number of execution steps allowed.
memoryOptions?:
MemoryConfig
Configuration options for memory management. See MemoryConfig section below for details.
onStepFinish?:
(step: string) => void
Callback function called after each execution step. Receives step details as a JSON string.
resourceId?:
string
Identifier for the user or resource interacting with the agent. Must be provided if threadId is provided.
telemetry?:
TelemetrySettings
Settings for telemetry collection during generation. See TelemetrySettings section below for details.
temperature?:
number
Controls randomness in the model's output. Higher values (e.g., 0.8) make the output more random, lower values (e.g., 0.2) make it more focused and deterministic.
threadId?:
string
Identifier for the conversation thread. Allows for maintaining context across multiple interactions. Must be provided if resourceId is provided.
toolChoice?:
'auto' | 'none' | 'required' | { type: 'tool'; toolName: string }
= 'auto'
Controls how the agent uses tools during generation.
toolsets?:
ToolsetsInput
Additional toolsets to make available to the agent during generation.
MemoryConfig
Configuration options for memory management:
lastMessages?:
number | false
Number of most recent messages to include in context. Set to false to disable.
semanticRecall?:
boolean | object
Configuration for semantic memory recall. Can be boolean or detailed config.
number
topK?:
number
Number of most semantically similar messages to retrieve.
number | object
messageRange?:
number | { before: number; after: number }
Range of messages to consider for semantic search. Can be a single number or before/after configuration.
workingMemory?:
object
Configuration for working memory.
boolean
enabled?:
boolean
Whether to enable working memory.
string
template?:
string
Template to use for working memory.
'text-stream' | 'tool-call'
type?:
'text-stream' | 'tool-call'
Type of content to use for working memory.
threads?:
object
Thread-specific memory configuration.
boolean
generateTitle?:
boolean
Whether to automatically generate titles for new threads.
TelemetrySettings
Settings for telemetry collection during generation:
isEnabled?:
boolean
= false
Enable or disable telemetry. Disabled by default while experimental.
recordInputs?:
boolean
= true
Enable or disable input recording. You might want to disable this to avoid recording sensitive information, reduce data transfers, or increase performance.
recordOutputs?:
boolean
= true
Enable or disable output recording. You might want to disable this to avoid recording sensitive information, reduce data transfers, or increase performance.
functionId?:
string
Identifier for this function. Used to group telemetry data by function.
metadata?:
Record<string, AttributeValue>
Additional information to include in the telemetry data. AttributeValue can be string, number, boolean, array of these types, or null.
tracer?:
Tracer
A custom OpenTelemetry tracer instance to use for the telemetry data. See OpenTelemetry documentation for details.
Returns
The return value of the generate()
method depends on the options provided, specifically the output
option.
PropertiesTable for Return Values
text?:
string
The generated text response. Present when output is 'text' (no schema provided).
object?:
object
The generated structured response. Present when a schema is provided via `output` or `experimental_output`.
toolCalls?:
Array<ToolCall>
The tool calls made during the generation process. Present in both text and object modes.
ToolCall Structure
toolName:
string
The name of the tool invoked.
args:
any
The arguments passed to the tool.
Related Methods
For real-time streaming responses, see the stream()
method documentation.