From e8a8928e56ebbb33f29abcba099cef3a4152614c Mon Sep 17 00:00:00 2001 From: webtecnica <75556242+webtecnica@users.noreply.github.com> Date: Mon, 6 Jul 2026 15:27:34 -0300 Subject: [PATCH] fix(agent): include max_tokens in _resolve_runtime_agent_kwargs_for_provider return dict (#59763) --- gateway/run.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/gateway/run.py b/gateway/run.py index 8256c283d4cf6..d6c0dafe4f570 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -1872,11 +1872,32 @@ def _resolve_runtime_agent_kwargs_for_provider(provider: str) -> dict: from hermes_cli.runtime_provider import ( resolve_runtime_provider, format_runtime_provider_error, + _get_model_config, ) try: runtime = resolve_runtime_provider(requested=provider) except Exception as exc: raise RuntimeError(format_runtime_provider_error(exc)) from exc + + max_tokens = None + _env_mt = os.environ.get("HERMES_MAX_TOKENS") + if _env_mt: + try: + max_tokens = int(_env_mt) + except (ValueError, TypeError): + max_tokens = None + elif isinstance(_get_model_config(), dict): + mt = _get_model_config().get("max_tokens") + if isinstance(mt, int): + max_tokens = mt + # Fall back to a per-provider output cap (custom_providers max_output_tokens) + # only when the documented global model.max_tokens isn't set, so the global + # key always wins. + if max_tokens is None: + _runtime_mot = runtime.get("max_output_tokens") + if isinstance(_runtime_mot, int) and _runtime_mot > 0: + max_tokens = _runtime_mot + return { "api_key": runtime.get("api_key"), "base_url": runtime.get("base_url"), @@ -1885,6 +1906,7 @@ def _resolve_runtime_agent_kwargs_for_provider(provider: str) -> dict: "command": runtime.get("command"), "args": list(runtime.get("args") or []), "credential_pool": runtime.get("credential_pool"), + "max_tokens": max_tokens, }