Problem
The populate workflow consistently fails with an OpenRouter 402 error because @mastra/core@1.36.0's Agent class always sends max_tokens: 65535 (the model's default maximum) to OpenRouter, which exceeds the free-tier credit limit.
Error from Render logs:
[populate-agent] agent.generate failed: This request requires more credits, or fewer max_tokens. You requested up to 65535 tokens, but can only afford 15821.
Root Cause
The @mastra/core@1.36.0 Agent class does not expose any public API to set maxOutputTokens or maxTokens on the underlying LLM call. The token limit sent to the provider is always the model's default maximum.
What Was Tried
Attempt 1: modelSettings on Agent constructor
new Agent({
id: "populate-agent",
model: openrouter("google/gemini-2.5-flash"),
modelSettings: { maxOutputTokens: 8192 }, // ← NOT A VALID PROPERTY
// ...
});
Result: TypeScript error TS2353:
Object literal may only specify known properties, and 'modelSettings' does not exist in type 'AgentConfig'
Attempt 2: maxTokens in agent.generate() options
const result = await agent.generate(prompt, {
abortSignal,
maxSteps: 80,
maxTokens: 8192, // ← NO MATCHING OVERLOAD
});
Result: TypeScript error TS2769:
No overload matches this call.
Both errors are suppressed by RUN npx tsc || true in the Dockerfile, but the runtime still sends max_tokens: undefined to OpenRouter, which defaults to 65535.
Affected Files
backend/src/mastra/agents/populate.ts — orchestrator agent (maxSteps: 80)
backend/src/mastra/agents/investigate.ts — subagent (maxSteps: 10)
backend/src/mastra/agents/refresh.ts — refresh agent (maxSteps: 10)
backend/src/mastra/workflows/populate.ts — workflow that calls populate agent
backend/src/mastra/tools/investigate-tool.ts — spawns investigate subagents
backend/src/mastra/workflows/update.ts — refresh workflow
Environment
@mastra/core: v1.36.0
AI Provider: OpenRouter (@openrouter/ai-sdk-provider)
Model: google/gemini-2.5-flash (free tier)
Hosting: Render (backend), Vercel (frontend)
Convex Cloud: https://flippant-lobster-149.convex.cloud
Possible Fixes
Upgrade @mastra/core to a version that supports token limit configuration (if one exists)
Set limits at the OpenRouter provider level — wrap the model with a custom provider that injects maxTokens into every call
Add credits to OpenRouter — the simplest fix if using paid models; free tier caps are too low for multi-step agent workflows
Patch the provider SDK — create a wrapper around the OpenRouter model that intercepts and overrides max_tokens before it reaches the API
Switch to a provider with higher free-tier limits or use a model with a lower default max_tokens
Problem
The populate workflow consistently fails with an OpenRouter 402 error because @mastra/core@1.36.0's Agent class always sends max_tokens: 65535 (the model's default maximum) to OpenRouter, which exceeds the free-tier credit limit.
Error from Render logs:
[populate-agent] agent.generate failed: This request requires more credits, or fewer max_tokens. You requested up to 65535 tokens, but can only afford 15821.
Root Cause
The @mastra/core@1.36.0 Agent class does not expose any public API to set maxOutputTokens or maxTokens on the underlying LLM call. The token limit sent to the provider is always the model's default maximum.
What Was Tried
Attempt 1: modelSettings on Agent constructor
new Agent({
id: "populate-agent",
model: openrouter("google/gemini-2.5-flash"),
modelSettings: { maxOutputTokens: 8192 }, // ← NOT A VALID PROPERTY
// ...
});
Result: TypeScript error TS2353:
Object literal may only specify known properties, and 'modelSettings' does not exist in type 'AgentConfig'
Attempt 2: maxTokens in agent.generate() options
const result = await agent.generate(prompt, {
abortSignal,
maxSteps: 80,
maxTokens: 8192, // ← NO MATCHING OVERLOAD
});
Result: TypeScript error TS2769:
No overload matches this call.
Both errors are suppressed by RUN npx tsc || true in the Dockerfile, but the runtime still sends max_tokens: undefined to OpenRouter, which defaults to 65535.
Affected Files
Environment
Possible Fixes