[GH-ISSUE #2685] [Issue]: Batched agent maxTokens is summed with no model-cap clamp → 400s the entire batch #1330

Closed
opened 2026-10-21 05:31:40 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2685

Originally assigned to: @SpicyMarinara on GitHub.

Summary

When multiple agents are batched into one LLM call, the request max_tokens is the sum of every agent's per-agent budget, with no clamp to the model's hard output ceiling. With several default agents (4096 each) and no connection maxTokensOverride, the batch sends a max_tokens far above a model's real cap (e.g. Claude models that cap at 8192), the provider returns HTTP 400, and executeAgentBatch's catch marks every batched agent failed.

Expected behavior

The batch (and per-agent) max_tokens should be clamped to the model's known output ceiling, so batching agents never produces a max_tokens > model maximum rejection.

Actual behavior

executeAgentBatch computes rawBatchMaxTokens = perAgentTokens.reduce((sum, t) => sum + t, 0) where each value is >= 128 and DEFAULT_AGENT_MAX_TOKENS = 4096. applyProviderMaxTokensOverride only caps to provider.maxTokensOverrideValue when one is set; otherwise the raw sum passes through into chatComplete({ maxTokens: batchMaxTokens }). Downstream, the Anthropic provider sets body.max_tokens = maxTokens with no model-cap clamp, and OpenAI's applyMaxTokensCap only caps to the override, not the model's hard limit. The individual-call path never sums, so it can't hit this.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • packages/server/src/services/agents/agent-executor.ts:649-652 — rawBatchMaxTokens = perAgentTokens.reduce(...); :201-203 applyProviderMaxTokensOverride only caps to maxTokensOverrideValue; sent at :696-707 (maxTokens: batchMaxTokens).
  • DEFAULT_AGENT_MAX_TOKENS = 4096 (packages/shared/src/types/agent.ts:463). 6 agents → 24576, 8 → 32768.
  • No model-cap clamp downstream: anthropic.provider.ts:240 (body.max_tokens = maxTokens); base-provider.ts:560-563 (applyMaxTokensCap caps only to override).
  • Provider rejects max_tokens > model maximum with 400, caught at agent-executor.ts:768, which returns makeError for every config (:786).

Steps to reproduce

  1. Enable ~8 batchable agents on a default-for-agents connection with no maxTokensOverride, pointing at a Claude/Anthropic model with an 8192 output cap.
  2. Generate. The batch sends max_tokens = 32768 → Anthropic 400 "max_tokens > model maximum" → executeAgentBatch catch returns an error result for every agent in the batch.

Suggested fix

Clamp the batch (and per-agent) max_tokens to the model's known output ceiling before sending — e.g. min(rawBatchMaxTokens, knownModelMaxOutputTokens(provider, model)) in addition to applyProviderMaxTokensOverride. (Related to #2665 but distinct: that issue is about the connection override being ignored on some providers; this is the sum exceeding the model's hard cap when no override exists.)

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of the agent subsystem. Reachable on a common config (several enabled agents, no override, a Claude model). Part of a set of agent-pipeline issues filed together.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2685 Originally assigned to: @SpicyMarinara on GitHub. ## Summary When multiple agents are batched into one LLM call, the request `max_tokens` is the **sum** of every agent's per-agent budget, with no clamp to the model's hard output ceiling. With several default agents (4096 each) and no connection `maxTokensOverride`, the batch sends a `max_tokens` far above a model's real cap (e.g. Claude models that cap at 8192), the provider returns HTTP 400, and `executeAgentBatch`'s catch marks **every** batched agent failed. ## Expected behavior The batch (and per-agent) `max_tokens` should be clamped to the model's known output ceiling, so batching agents never produces a `max_tokens > model maximum` rejection. ## Actual behavior `executeAgentBatch` computes `rawBatchMaxTokens = perAgentTokens.reduce((sum, t) => sum + t, 0)` where each value is `>= 128` and `DEFAULT_AGENT_MAX_TOKENS = 4096`. `applyProviderMaxTokensOverride` only caps to `provider.maxTokensOverrideValue` *when one is set*; otherwise the raw sum passes through into `chatComplete({ maxTokens: batchMaxTokens })`. Downstream, the Anthropic provider sets `body.max_tokens = maxTokens` with no model-cap clamp, and OpenAI's `applyMaxTokensCap` only caps to the override, not the model's hard limit. The individual-call path never sums, so it can't hit this. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - `packages/server/src/services/agents/agent-executor.ts:649-652` — `rawBatchMaxTokens = perAgentTokens.reduce(...)`; `:201-203` `applyProviderMaxTokensOverride` only caps to `maxTokensOverrideValue`; sent at `:696-707` (`maxTokens: batchMaxTokens`). - `DEFAULT_AGENT_MAX_TOKENS = 4096` (`packages/shared/src/types/agent.ts:463`). 6 agents → 24576, 8 → 32768. - No model-cap clamp downstream: `anthropic.provider.ts:240` (`body.max_tokens = maxTokens`); `base-provider.ts:560-563` (`applyMaxTokensCap` caps only to override). - Provider rejects `max_tokens > model maximum` with 400, caught at `agent-executor.ts:768`, which returns `makeError` for **every** config (`:786`). ## Steps to reproduce 1. Enable ~8 batchable agents on a default-for-agents connection with **no** `maxTokensOverride`, pointing at a Claude/Anthropic model with an 8192 output cap. 2. Generate. The batch sends `max_tokens = 32768` → Anthropic 400 "max_tokens > model maximum" → `executeAgentBatch` catch returns an error result for every agent in the batch. ## Suggested fix Clamp the batch (and per-agent) `max_tokens` to the model's known output ceiling before sending — e.g. `min(rawBatchMaxTokens, knownModelMaxOutputTokens(provider, model))` in addition to `applyProviderMaxTokensOverride`. (Related to #2665 but distinct: that issue is about the connection override being ignored on some providers; this is the *sum* exceeding the model's hard cap when **no** override exists.) ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of the agent subsystem. Reachable on a common config (several enabled agents, no override, a Claude model). Part of a set of agent-pipeline issues filed together. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Addressed on the issue-sweep branch. Agent resolution now carries each connection's known model maxOutput from the shared model table, and the executor clamps both individual agent maxTokens and summed batch maxTokens by the connection override and known model output cap before calling providers. Batch logging now reports the applied connection/model caps. Final repo checks will run after the sweep.

<!-- gh-comment-id:4761482474 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on the issue-sweep branch. Agent resolution now carries each connection's known model maxOutput from the shared model table, and the executor clamps both individual agent maxTokens and summed batch maxTokens by the connection override and known model output cap before calling providers. Batch logging now reports the applied connection/model caps. Final repo checks will run after the sweep.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1330
No description provided.