Agent.generate()
The .generate() method enables non-streaming response generation from an agent with enhanced capabilities. It accepts messages and optional generation options.
Usage examplesDirect link to Usage examples
Basic usageDirect link to Basic usage
Call the agent with a message to generate a response:
const result = await agent.generate('message for agent')
With model settingsDirect link to With model settings
Example of limiting output tokens and setting temperature:
const limitedResult = await agent.generate('Write a short poem about coding', {
modelSettings: {
maxOutputTokens: 50,
temperature: 0.7,
},
})
With memoryDirect link to With memory
Give your agent access to conversation history and persistence by configuring memory options. This allows the agent to remember previous interactions and maintain context across messages.
const memoryResult = await agent.generate('Remember my favorite color is blue', {
memory: {
thread: 'user-123-thread',
resource: 'user-123',
},
})
Accessing response headersDirect link to Accessing response headers
Some model providers return useful information in response headers, such as remaining token counts or rate limit status. You can access these headers from the result object after generation completes.
const result = await agent.generate('Hello!')
const remainingRequests = result.response?.headers?.['anthropic-ratelimit-requests-remaining']
const remainingTokens = result.response?.headers?.['x-ratelimit-remaining-tokens']
console.log(`Remaining requests: ${remainingRequests}, Remaining tokens: ${remainingTokens}`)
Analyzing imagesDirect link to Analyzing images
Agents can analyze and describe images by processing both the visual content and any text within them. To enable image analysis, pass an object with type: 'image' and the image URL in the content array. You can combine image content with text prompts to guide the agent's analysis.
const response = await agent.generate([
{
role: 'user',
content: [
{
type: 'image',
image: 'https://placebear.com/cache/395-205.jpg',
mimeType: 'image/jpeg',
},
{
type: 'text',
text: 'Describe the image in detail, and extract all the text in the image.',
},
],
},
])
console.log(response.text)
Using maxStepsDirect link to using-maxsteps
The maxSteps parameter controls the maximum number of sequential LLM calls an agent can make. Each step includes generating a response, executing any tool calls, and processing the result. Limiting steps helps prevent infinite loops, reduce latency, and control token usage for agents that use tools. The default is 5, but can be increased:
const response = await agent.generate('Help me organize my day', {
maxSteps: 10,
})
console.log(response.text)
Using onStepFinishDirect link to using-onstepfinish
You can monitor the progress of multi-step operations using the onStepFinish callback. This is useful for debugging or providing progress updates to users.
onStepFinish is only available when streaming or generating text without structured output.
const response = await agent.generate('Help me organize my day', {
onStepFinish: ({ text, toolCalls, toolResults, finishReason, usage }) => {
console.log({ text, toolCalls, toolResults, finishReason, usage })
},
})
ParametersDirect link to Parameters
messages:
options?:
maxSteps?:
stopWhen?:
onIterationComplete?:
context.iteration:
context.maxIterations:
context.text:
context.isFinal:
context.finishReason:
context.toolCalls:
context.messages:
return.continue?:
return.feedback?:
isTaskComplete?:
scorers:
strategy?:
onComplete?:
parallel?:
timeout?:
delegation?:
onDelegationStart?:
onDelegationComplete?:
messageFilter?:
scorers?:
scorer:
sampling?:
type:
rate?:
returnScorerData?:
onChunk?:
onError?:
onAbort?:
activeTools?:
abortSignal?:
prepareStep?:
requireToolApproval?:
autoResumeSuspendedTools?:
toolCallConcurrency?:
context?:
structuredOutput?:
schema:
model?:
errorStrategy?:
fallbackValue?:
instructions?:
jsonPromptInjection?:
logger?:
providerOptions?:
outputProcessors?:
maxProcessorRetries?:
inputProcessors?:
instructions?:
system?:
output?:
memory?:
thread:
resource:
options?:
onFinish?:
onStepFinish?:
telemetry?:
isEnabled?:
recordInputs?:
recordOutputs?:
functionId?:
modelSettings?:
temperature?:
maxOutputTokens?:
maxRetries?:
topP?:
topK?:
presencePenalty?:
frequencyPenalty?:
stopSequences?:
toolChoice?:
'auto':
'none':
'required':
{ type: 'tool'; toolName: string }:
toolsets?:
clientTools?:
savePerStep?:
providerOptions?:
openai?:
anthropic?:
google?:
[providerName]?:
runId?:
requestContext?:
tracingContext?:
currentSpan?:
tracingOptions?:
metadata?:
requestContextKeys?:
traceId?:
parentSpanId?:
tags?:
includeRawChunks?:
Response structureDirect link to Response structure
Agent.generate() returns the final data collected during execution. steps is an array of step objects. The tool arrays in the result, including top-level toolCalls and toolResults and the nested step.toolCalls and step.toolResults arrays, use Mastra's chunk format.
That means tool data is wrapped in payload:
const response = await agent.generate('Check the weather in Lagos')
for (const toolCall of response.toolCalls) {
console.log(toolCall.type) // 'tool-call'
console.log(toolCall.runId)
console.log(toolCall.from)
console.log(toolCall.payload.toolName)
console.log(toolCall.payload.args)
}
for (const step of response.steps) {
for (const toolResult of step.toolResults) {
console.log(toolResult.type) // 'tool-result'
console.log(toolResult.payload.toolName)
console.log(toolResult.payload.result)
}
}
For the streaming version of the same chunk shape, see the ChunkType reference.