fix(gateway): propagate max_tokens from config.yaml to AIAgent (#20741)#39864
Merged
Conversation
max_tokens set under model: in config.yaml was silently ignored. The value was never read from config, never passed through _resolve_runtime_agent_kwargs(), _resolve_turn_agent_config(), or the session override path. Added it to all three code paths so custom/Ollama endpoints receive the correct output cap. Closes #20741
…r override Previous commit only covered the gateway runtime path. This adds: - CLI __init__: read max_tokens from model config with HERMES_MAX_TOKENS env override - CLI AIAgent() calls (interactive + background): pass max_tokens - Gateway _resolve_runtime_agent_kwargs: add HERMES_MAX_TOKENS env override All three code paths (CLI, gateway runtime, session override) now consistently propagate max_tokens to AIAgent.
Widens ViewWay's #20741 fix to the sibling config surface: a custom_providers entry can pin its own output cap via max_output_tokens (or max_tokens). _get_named_custom_provider now lifts it onto the resolved runtime at all three return sites, and the gateway uses it as a fallback only when the documented global model.max_tokens isn't set, so the global key always wins. Precedence: HERMES_MAX_TOKENS > model.max_tokens > provider max_output_tokens > None. Closes the same #20741 truncation for users who configure the cap per-provider rather than globally. Picks up the intent of #19782 (alexcam1901), reimplemented to feed ViewWay's max_tokens pipeline.
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
unresolved-import |
1 |
First entries
tests/gateway/test_max_tokens_propagation.py:18: [unresolved-import] unresolved-import: Cannot resolve imported module `pytest`
✅ Fixed issues: none
Unchanged: 5116 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
Collaborator
This was referenced Jun 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Setting
max_tokensin config.yaml now actually caps model output for gateway-spawned agents — fixing the "Response truncated due to output length limit" reports (#20741). Previously the value was read but never propagated toAIAgent, so providers without a hardcoded default (OpenRouter:freemodels, Ollama Cloud, custom OpenAI-compatible endpoints) fell back to the server's short default and truncated long generations.Salvages @ViewWay's #20804 (cherry-picked, authorship preserved) and widens it to the per-provider config surface that #19782 (@alexcam1901) targeted.
Changes
cli.py/gateway/run.py(ViewWay): readmodel.max_tokensand pass it toAIAgentacross CLI init, CLI background, gateway runtime, and session-override paths.HERMES_MAX_TOKENSenv var as the internal override mechanism (config.yaml stays the documented surface).hermes_cli/runtime_provider.py(widening): acustom_providersentry can pin its own cap viamax_output_tokens(ormax_tokens). Lifted onto the resolved runtime at all three_get_named_custom_providerreturn sites + the pooled-credential path.gateway/run.py(widening): per-provider cap is used only when the documented globalmodel.max_tokensisn't set, so the global key always wins.scripts/release.py: AUTHOR_MAP entry for ViewWay.tests/gateway/test_max_tokens_propagation.py: regression tests for the full precedence chain.Precedence
HERMES_MAX_TOKENS>model.max_tokens> per-providermax_output_tokens>NoneValidation
model.max_tokens: 16384max_output_tokens: 12000, no globalHERMES_MAX_TOKENS=2048+ bothNone(no spurious cap)E2E verified through the real
_resolve_runtime_agent_kwargs()with isolated HERMES_HOME. Targeted suite: 133 passed (test_max_tokens_propagation+test_runtime_provider_resolution).Scope note
The CLI honors the documented global
model.max_tokensfully. Per-providermax_output_tokenson the CLI path is not wired here — it requires capturing the resolved-provider dict duringcli.pyinit (a >1500-line critical file), and #35518 is reworking that surface wholesale. Gateway is the path the truncation reports come from.Closes #20741. Supersedes #20804, #19782.
Co-authored-by: ViewWay 834740219@qq.com
Infographic