agent.generate() with maxTokens set is ignored and defaults to 4096

It seems that maxTokens is totally ignored and defaulted to 4096 when I tried to increase the maxTokens. The code below doesn't respect the maxTokens:
const result = await agent.generate('Write a short poem about coding', {
maxTokens: 50,
});
const result = await agent.generate('Write a short poem about coding', {
maxTokens: 50,
});
The code below will respect the max tokens but setting modelSettings.maxOutputTokens is undocumented on https://mastra.ai/en/reference/agents/generate#options:
const result = await agent.generate('Write a short poem about coding', {
modelSettings: {
maxOutputTokens: 50,
},
});
const result = await agent.generate('Write a short poem about coding', {
modelSettings: {
maxOutputTokens: 50,
},
});
I can reproduce this with Vertex AI Anthropic and OpenAI.
Reference: Agent.generate() | Agents | Mastra Docs
Documentation for the Agent.generate() method in Mastra agents, which enables non-streaming generation of responses with enhanced capabilities.
1 Reply
Mastra Triager
GitHub
[DISCORD:1433094638071185591] agent.generate() with maxTokens set i...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1433094638071185591 It seems that maxTokens is totally ignored and defaulted to 4096 when I tried to incre...

Did you find this page helpful?