Skip to content

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

Merged
kovtcharov-amd merged 6 commits into
mainfrom
replicate/cloud-output-token-cap-4063
Sep 25, 2026
Merged

kovtcharov-amd merged 6 commits into
mainfrom
replicate/cloud-output-token-cap-4063

Conversation

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Replicates #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.

One change beyond the original: main removed the openai/litellm providers (#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

  • 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.
  • pytest tests/unit/agents — 1422 passed, 50 skipped; identical on unmodified main.
  • python util/lint.py --black --isort clean.
  • gaia eval agent --category tool_selection

Carried over from the review on #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.

Kalin Ovtcharov and others added 5 commits September 21, 2026 09:10
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.
…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.
# Conflicts:
#	src/gaia/agents/base/agent.py
main removed the openai/litellm providers (#3899), so use_chatgpt now raises
a migration error at construction. The OpenAI request-cap test exercised that
removed provider and the cloud-cap expression still carried a use_chatgpt term
that can never be True.
@github-actions github-actions Bot added documentation Documentation changes tests Test changes agents labels Sep 25, 2026
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 25, 2026
Merged via the queue into main with commit 0e553bb Sep 25, 2026
42 of 44 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the replicate/cloud-output-token-cap-4063 branch September 25, 2026 20:12
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

Cloud reasoning models like fireworks.deepseek-v4p1-flash now get a 32K output cap instead of 8K — the right fix for replies truncated mid-thinking. Implementation is clean: the two constants are well-named, the per-call _max_output_tokens() correctly follows model switches, and the input validation properly guards against bool subclassing int. All four LLM call sites are updated and the tests cover local vs. cloud defaults, explicit overrides, catalog-metadata detection, model switches, and the Claude path.

Two carried-over gaps worth a follow-up issue:

🟡 Streaming call sites have no test coverage. The four call sites with max_tokens=self._max_output_tokens() include two streaming paths (send_messages_stream at lines ~6194 and ~6666). The test harness uses a non-streaming mock response, so only the non-streaming paths are exercised. If streaming silently dropped or misrouted the max_tokens kwarg, these tests would stay green. A streaming variant of test_request_body_carries_model_sized_output_cap would close this.

🟡 AgentConfig.max_tokens and _max_output_tokens() can disagree after a custom-provider model switch. The AgentConfig.max_tokens is set once at init from cloud_model_provider(model_id). Any later switch to a custom provider whose cloud status is learned via catalog metadata after startup will have the right cap at the call sites (per-call _max_output_tokens() uses the live backend metadata), but the init-time config value stays stale. Memory-distillation and builder paths that read self.chat.config.max_tokens directly will see the wrong cap. The PR description acknowledges this; a follow-up issue to consolidate to a single source of truth would eliminate the ambiguity.

🔍 Technical details

Streaming test gap: _request_bodies() in test_agent_max_output_tokens.py returns a non-streaming reply (choices[0].message.content). agent.process_query("hi") will only exercise streaming if the agent decides to stream — verifying the non-streaming assertion green does not mean the streaming path sends the same max_tokens. Adding a streaming mock (e.g. SSE chunks) and asserting the same max_completion_tokens in those bodies would close it.

Double-computation: agent.py:1416–1428 sets AgentConfig.max_tokens at init. _max_output_tokens() at agent.py:1452–1464 is called per LLM call. Any code that reads self.chat.config.max_tokens rather than calling _max_output_tokens() will see the init-time value. The _max_output_tokens() docstring says "re-read per call so a model switch mid-session takes effect" — true for the main loop, but overstated for paths that bypass it.

Minor nit (~50 words): The _max_output_tokens() guard getattr(getattr(self.chat, "llm_client", None), "_backend", None) accesses _backend as a private attribute. If LemonadeClient ever restructures its internals this silently falls back to LOCAL_MAX_OUTPUT_TOKENS instead of raising, which is a soft version of the silent-fallback pattern the codebase discourages. Acceptable given no public accessor exists today, but worth a comment.

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