Skip to content

fix(agent): give cloud models a 32K output-token cap - #4063

Closed
kovtcharov wants to merge 3 commits into
amd:mainfrom
kovtcharov:fix/cloud-output-token-cap
Closed

kovtcharov wants to merge 3 commits into
amd:mainfrom
kovtcharov:fix/cloud-output-token-cap

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Cloud reasoning models such as fireworks.deepseek-v4p1-flash count their thinking against the agent's per-reply output cap. That cap was fixed at 8,192 tokens, which was sized for a local 32K context. On TheRock coding tasks, all three failed runs ended with a reply at exactly 8,192 tokens. Lemonade cloud models now get 32,768 output tokens, the same default Claude Code uses. Local models keep 8,192. Callers can set their own cap with a new optional max_output_tokens on the agent.

Test plan

  • pytest tests/unit/test_agent_max_output_tokens.py: these tests check the JSON body sent to Lemonade and expect max_completion_tokens 32768 for a cloud model and 8192 for a local model. They also check an explicit override, a model switch mid-session, a custom provider flagged by catalog metadata, and the Claude and OpenAI request kwargs. 13 of the 14 fail on main.
  • Agent, provider, and chat/gaia hub suites pass. The 10 failures in tests/test_lemonade_client.py and the flagship tool-count tests also fail on unmodified main.
  • gaia eval agent --category tool_selection compared against the committed baseline

Cloud reasoning models count their thinking against the agent's per-reply
output cap, and the fixed 8,192-token cap (sized for a local 32K context) was
cutting their replies off mid-thought. Lemonade cloud models now get 32,768;
local models keep 8,192.

- The cap is resolved per call from the current model, so a mid-session model
  switch takes effect. Cloud detection uses cloud_model_provider(), including
  Lemonade catalog metadata once discovered.
- New optional max_output_tokens on Agent and ChatAgentConfig overrides the
  policy; an invalid value raises. Claude and OpenAI keep their 8,192 default
  and honor an explicit value.
@github-actions github-actions Bot added documentation Documentation changes tests Test changes agents labels Sep 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Approve with suggestions

Cloud reasoning models were being cut off mid-reply because the agent capped every response at 8,192 output tokens — a number sized for a local 32K context. This PR raises that to 32,768 for Lemonade cloud models, keeps 8,192 everywhere else, and adds a caller-settable override. The core change is correct and the tests read the real request body rather than trusting a mock, which is exactly the right bar.

Two things worth tightening before merge, neither a regression from main:

  • The new per-model cap isn't applied everywhere the agent talks to the model. The main reasoning loop gets it; the memory-distillation and builder paths still use the value computed once at startup, which is decided by a slightly different rule. A custom cloud provider — the case one of the new tests covers — ends up with 32K on one path and 8K on the other.
  • The streaming path has no test. The fix touches both streaming and non-streaming calls, but the tests only exercise non-streaming. Streaming is what the Agent UI and TUI actually use, so the surface the bug was reported on is the one left unverified.

Real-world evidence

N/A — no evidence bundle was produced for this run, and no pytest environment is available here, so this verdict rests on static review plus the unit tests as written. The PR description documents the before state well (three failed TheRock runs ending at exactly 8,192 tokens) but shows no after run against a real cloud model, and the author's own test-plan item — gaia eval agent --category tool_selection compared to the committed baseline — is still unchecked. One real fireworks.* reply that previously truncated and now completes would close this out.

🔍 Technical details

Issues

🟡 Two sources of truth for the cap, and they disagree (src/gaia/agents/base/agent.py:1291 vs :1317)

AgentConfig.max_tokens is computed at init from the module-level cloud_model_provider(model_id) — no catalog metadata — while _max_output_tokens() uses the backend's metadata-aware LemonadeClient.cloud_model_provider. Only the four send_messages* call sites in the agent loop pass the latter; memory.py:1523/1839/2093, procedural_memory.py:562 and builder/agent.py:329/340 fall back to config.max_tokens. Consequences: a custom provider flagged only by catalog metadata (company.llama-4, per test_catalog_metadata_marks_a_custom_provider_as_cloud) gets 32K in the main loop and 8K in memory distillation; after a mid-session switch to a cloud model, config.max_tokens is never refreshed at all. Collapsing to one rule right after the SDK is built fixes both (the client is constructed eagerly in AgentSDK.__init__, so _max_output_tokens() is safe there):

        self.chat = AgentSDK(chat_config)
        # One rule for both seams: paths that don't pass an explicit cap get the
        # same per-model value, catalog metadata included.
        self.chat.config.max_tokens = self._max_output_tokens()

🟡 Streaming call sites are untested (src/gaia/agents/base/agent.py:5954, :6426)

_request_bodies builds the agent without streaming=True, so only send_messages is exercised; the two send_messages_stream sites are asserted by inspection alone. Passing streaming=True through _request_bodies and parameterizing the existing model-sized-cap test over both modes covers it with a couple of lines.

Nits

🟢 LOCAL_MAX_OUTPUT_TOKENS is also the Claude/OpenAI default (agent.py:51-53) — the comment justifies 8,192 by a local model's 32K context, but _max_output_tokens() returns the same constant for Claude (200K ctx) and OpenAI. The value is intentional per the PR description; the name and comment just don't cover the case.

🟢 The new param is silently clamped on Claude (agent.py:1122) — providers/claude.py:437 does max(max_tokens, 8192), so max_output_tokens=2000 with use_claude=True is ignored without a word. Pre-existing provider behaviour, but now it's the contract of a documented public knob; one clause in the docstring (and docs/spec/agent-base.mdx:141) saying Claude raises anything below 8,192 would prevent a confused bug report.

🟢 self.max_output_tokens vs self._max_output_tokens() — an attribute and a method one underscore apart, where reading the attribute instead of calling the method silently yields None for the default case. A name like _explicit_max_output_tokens for the stored value removes the trap.

Strengths

  • The tests assert the JSON body Lemonade actually receives rather than kwargs handed to an internal client — that's the #1655 lesson applied, and it's why 13 of 14 fail on main.
  • Coverage of the mid-session model switch is the right instinct: _switch_model in stdio.py swaps chat.llm_client and config.model without touching config.max_tokens, which is precisely why the per-call re-read exists.
  • The truncation error message now prints the actual cap and names the knob to change instead of pointing at an internal config field.
  • Input validation rejects bool, non-int and non-positive values at construction time rather than letting a bad cap surface as a provider 400.

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Refreshed this branch against current main so it can merge again — the branch had fallen far enough behind that GitHub could no longer compute a merge.

The update was a clean merge: nothing was resolved by hand, and no commit on this branch was rewritten. CI now runs against an up-to-date base.

…eads

The truncated-tool-call error message now names the live output-token
budget, so the code path reads self.max_output_tokens. The bare-agent
stub in this file is built with __new__ on purpose and never ran
__init__, so that attribute was missing and formatting the message
raised AttributeError instead of the ValueError the test asserts.

Set it on the stub like the other attributes it already primes.
kovtcharov added a commit to kovtcharov/gaia that referenced this pull request Sep 25, 2026
Replicates amd#4063 (kovtcharov/gaia fork, push access blocked) onto a
branch this session can push to.

Cloud reasoning models such as `fireworks.deepseek-v4p1-flash` count
their thinking against the agent's per-reply output cap. That cap was
fixed at 8,192 tokens, which was sized for a local 32K context. On
TheRock coding tasks, all three failed runs ended with a reply at
exactly 8,192 tokens. Lemonade cloud models now get 32,768 output
tokens, the same default Claude Code uses. Local models keep 8,192.
Callers can set their own cap with a new optional `max_output_tokens` on
the agent.

- Replies that got cut off already continue since amd#4054, but each
cut-off costs an extra round trip and drops thinking. This cuts off far
fewer replies.
- Claude keeps its current default. An explicit cap now reaches it, with
one exception: Claude's existing 8,192 minimum still raises any smaller
explicit value.

One change beyond the original: `main` removed the openai/litellm
providers (amd#3899), so `use_chatgpt` now raises a migration error at
construction. The original PR's OpenAI request-cap test exercised that
removed provider and has been dropped, along with a `use_chatgpt` term
in the cap expression that can never be True. No other content changes
beyond conflict resolution against current main.

## Test plan
- [x] `pytest tests/unit/test_agent_max_output_tokens.py
tests/unit/test_tool_call_priority.py` — 43 passed. These check the JSON
body sent to Lemonade and expect `max_completion_tokens` 32768 for a
cloud model and 8192 for a local model, plus an explicit override, a
model switch mid-session, a custom provider flagged by catalog metadata,
and the Claude request kwargs.
- [x] `pytest tests/unit/agents` — 1422 passed, 50 skipped; identical on
unmodified `main`.
- [x] `python util/lint.py --black --isort` clean.
- [ ] `gaia eval agent --category tool_selection`

Carried over from the review on amd#4063, not addressed here: the cap is
computed twice (once into `AgentConfig.max_tokens` at init, once per
call in `_max_output_tokens()`), so memory-distillation and builder
paths can disagree with the main loop for a catalog-flagged custom
provider; and the streaming call sites have no test.

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Co-authored-by: Ovtcharov <[email protected]>
Co-authored-by: Kalin Ovtcharov <[email protected]>
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Replicated as #4368 — fork push access (kovtcharov/gaia) was revoked mid-review, so this PR could no longer be refreshed against main. One change during the rebase: main removed the openai/litellm providers (#3899), so the OpenAI request-cap test and a dead use_chatgpt term in the cap expression were dropped. Closing in favor of the replacement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants