The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: local sub-64K context refusal stops assuming Ollama
teknium155db0a984753
diff --git a/agent/agent_init.py b/agent/agent_init.pyindex 246ab57af44..43c0440b7bb 100644--- a/agent/agent_init.py+++ b/agent/agent_init.py@@ -1927,14 +1927,28 @@ def _enforce_minimum_context(agent):and agent._config_context_length > 0)if _ctx and _ctx < MINIMUM_CONTEXT_LENGTH and not _allow_lmstudio_explicit_below_floor:+ floor_k = MINIMUM_CONTEXT_LENGTH // 1000+ if agent.base_url and is_local_endpoint(agent.base_url):+ # Any OpenAI-compatible local server (llama.cpp, vLLM, Ollama, ...) — the window is the+ # server's runtime setting, not the model's; never assume Ollama here (#87075).+ remedy = (+ f"Your local server is serving a {_ctx:,}-token window. Start it with at least "+ f"{floor_k}K context (llama.cpp: -c {MINIMUM_CONTEXT_LENGTH}; vLLM: --max-model-len; "+ f"Ollama: OLLAMA_CONTEXT_LENGTH={MINIMUM_CONTEXT_LENGTH} or a Modelfile num_ctx), "+ f"or set model.ollama_num_ctx in config.yaml to the window it really serves "+ f"(at least {floor_k}K)."+ )+ else:+ remedy = (+ f"Choose a model with at least {floor_k}K context. If your server "+ f"reports a window smaller than the model's true window, set "+ f"model.context_length in config.yaml to the real value "+ f"(this must be at least {floor_k}K)."+ )raise ValueError(f"Model {agent.model} has a context window of {_ctx:,} tokens, "f"which is below the minimum {MINIMUM_CONTEXT_LENGTH:,} required "- f"by Hermes Agent. Choose a model with at least "- f"{MINIMUM_CONTEXT_LENGTH // 1000}K context. If your server "- f"reports a window smaller than the model's true window, set "- f"model.context_length in config.yaml to the real value "- f"(this must be at least {MINIMUM_CONTEXT_LENGTH // 1000}K)."+ f"by Hermes Agent. {remedy}")@@ -2027,7 +2041,7 @@ def _configure_ollama_num_ctx(agent, _model_cfg, _config_context_length):if _detected and _detected > 0:agent._ollama_num_ctx = _detectedexcept Exception as exc:- _ra().logger.debug("Ollama num_ctx detection failed: %s", exc)+ _ra().logger.debug("Local server num_ctx detection failed: %s", exc)# Cap auto-detected num_ctx to the explicit context_length (GGUF metadata can advertise# 256K+ and Ollama would allocate that much VRAM); never override an explicit num_ctx.if (@@ -2042,9 +2056,11 @@ def _configure_ollama_num_ctx(agent, _model_cfg, _config_context_length):)agent._ollama_num_ctx = _config_context_lengthif agent._ollama_num_ctx and not agent.quiet_mode:+ # Name the real source: a config override is honoured on any local server, /api/show is Ollama-only._ra().logger.info(- "Ollama num_ctx: will request %d tokens (model max from /api/show)",+ "Local server num_ctx: will request %d tokens (%s)",agent._ollama_num_ctx,+ "model.ollama_num_ctx" if _override is not None else "model max from Ollama /api/show",)diff --git a/hermes_cli/cli_info_mixin.py b/hermes_cli/cli_info_mixin.pyindex 912b67a8d6d..f47eea07016 100644--- a/hermes_cli/cli_info_mixin.py+++ b/hermes_cli/cli_info_mixin.py@@ -170,7 +170,7 @@ class CLIInfoMixin:self._show_tool_availability_warnings()# Low context warning — tied to the runtime guard so guidance cannot drift.- from agent.model_metadata import MINIMUM_CONTEXT_LENGTH+ from agent.model_metadata import MINIMUM_CONTEXT_LENGTH, is_local_endpointself._show_plugin_compat_notice()if ctx_len and ctx_len < MINIMUM_CONTEXT_LENGTH:self._console_print()@@ -190,6 +190,10 @@ class CLIInfoMixin:fix = f"Ollama fix: OLLAMA_CONTEXT_LENGTH={MINIMUM_CONTEXT_LENGTH} ollama serve"elif _port == 1234:fix = "LM Studio fix: Set context length in model settings → reload model"+ elif is_local_endpoint(base_url): # llama.cpp / vLLM / any local server — not Ollama+ fix = (f"Fix: start your server with at least {MINIMUM_CONTEXT_LENGTH // 1000}K context "+ f"(llama.cpp: -c {MINIMUM_CONTEXT_LENGTH}), or set model.ollama_num_ctx in config.yaml "+ "to the window it really serves")else:fix = "Fix: Set model.context_length in config.yaml, or increase your server's context setting"self._console_print(f"[dim] {fix}[/]")diff --git a/tests/agent/test_ollama_num_ctx.py b/tests/agent/test_ollama_num_ctx.pyindex bffb9ef2c45..8d98f24b55a 100644--- a/tests/agent/test_ollama_num_ctx.py+++ b/tests/agent/test_ollama_num_ctx.py@@ -207,3 +207,21 @@ class TestServedNumCtxSatisfiesTheFloor:with pytest.raises(ValueError, match="below the minimum"):_build_agent({"agent": {}, "model": {"ollama_num_ctx": 65536}}, probed_ctx=40960,base_url="https://openrouter.ai/api/v1")+++class TestFloorRefusalNamesTheLocalServerHonestly:+ """#87075: a local OpenAI-compatible server without /api/show (llama.cpp, vLLM) that serves a+ sub-64K window must get server-agnostic guidance — raise the server's context or set+ model.ollama_num_ctx — while a hosted route keeps the model.context_length advice."""++ def test_local_refusal_names_server_flag_and_num_ctx_key(self):+ with pytest.raises(ValueError) as exc:+ _build_agent({"agent": {}, "model": {}}, probed_ctx=32768, base_url="http://127.0.0.1:8422/v1")+ msg = str(exc.value)+ assert "llama.cpp: -c 64000" in msg and "model.ollama_num_ctx" in msg+ assert "Choose a model" not in msg++ def test_hosted_refusal_keeps_context_length_advice(self):+ with pytest.raises(ValueError, match="model.context_length") as exc:+ _build_agent({"agent": {}, "model": {}}, probed_ctx=32768, base_url="https://openrouter.ai/api/v1")+ assert "ollama_num_ctx" not in str(exc.value)diff --git a/website/docs/integrations/providers.md b/website/docs/integrations/providers.mdindex 031a866c292..dde0a0422c3 100644--- a/website/docs/integrations/providers.md+++ b/website/docs/integrations/providers.md@@ -1146,6 +1146,8 @@ The model outputs something like `{"name": "web_search", "arguments": {...}}` as**Fix:** Set context to at least **64,000 tokens** for agent use. See each server's section above for the specific flag.+The startup refusal for a local endpoint (`127.0.0.1`, LAN, Docker service names) says which window the server is serving and names the fix for any OpenAI-compatible server, not just Ollama: raise the server's context (llama.cpp `-c 64000`, vLLM `--max-model-len`, Ollama `OLLAMA_CONTEXT_LENGTH`/Modelfile `num_ctx`) or set `model.ollama_num_ctx` in `config.yaml` to the window the server really serves (at least 64K). `model.ollama_num_ctx` is honoured on every local endpoint; only the automatic detection behind it uses Ollama's `/api/show`.+#### "Context limit: 2048 tokens" at startupHermes auto-detects context length from your server's `/v1/models` endpoint. If the server reports a low value (or doesn't report one at all), Hermes uses the model's declared limit which may be wrong.