Skip to content

The agent that grows with you

fix(context): proxied Codex routes resolve the Codex OAuth window, not the direct-API catalog

teknium121ef1b97f9de
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index c4b34d8458d..ce71143af36 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -643,6 +643,21 @@ def _skip_persistent_context_cache(base_url: str, provider: str) -> bool:
return (provider or "").strip().lower() in {"lmstudio", "openai-codex"}
+def _is_codex_route(provider: str, base_url: str, custom_providers: list | None) -> bool:
+ """True when the request travels the Codex Responses wire regardless of host: the native
+ ``openai-codex`` provider (also behind a ``HERMES_CODEX_BASE_URL`` / ``model.base_url`` proxy)
+ or a custom entry declaring ``api_mode: codex_responses``. The transport, not the hostname,
+ decides which window the model actually gets (#116191)."""
+ if (provider or "").strip().lower() == "openai-codex":
+ return True
+ if not base_url:
+ return False
+ with contextlib.suppress(Exception): # config unreadable → not a known Codex route
+ from hermes_cli.config import get_custom_provider_api_mode
+ return get_custom_provider_api_mode(base_url, custom_providers) == "codex_responses"
+ return False
+
+
def _save_unless_skipped(model: str, base_url: str, ctx: int, provider: str) -> None:
"""Persist ``ctx`` unless the provider opts out of the disk cache."""
if not _skip_persistent_context_cache(base_url, provider):
@@ -2023,6 +2038,19 @@ def _resolve_custom_endpoint_context_length(model: str, base_url: str, api_key:
return DEFAULT_FALLBACK_CONTEXT
+def _resolve_custom_codex_route_context_length(model: str, base_url: str, api_key: str, provider: str) -> int:
+ """Step 2 for a custom ``api_mode: codex_responses`` route — a Codex proxy on a generic URL.
+ The Codex OAuth table (with the opted-in ``-900k`` bump) answers first: the proxy's own /models
+ and the direct-API catalog both advertise the 1.05M direct window that Codex does not honour.
+ No live catalog probe — the route's key is the proxy's, not a ChatGPT token. Slugs the table
+ does not know take the ordinary endpoint probes."""
+ ctx, _source = _resolve_codex_oauth_context_length_with_source(model)
+ if ctx:
+ logger.info("Using Codex OAuth context length %s for model %r (codex_responses route at %s)", f"{ctx:,}", model, base_url)
+ return ctx
+ return _resolve_custom_endpoint_context_length(model, base_url, api_key, provider)
+
+
def _resolve_moa_context_length(model: str, custom_providers: list | None) -> Optional[int]:
"""Step 0a: MoA virtual provider — ``model`` is a preset name, so every probe would miss. Resolve
the aggregator's real provider+model (references are advisory). None on any failure."""
@@ -2175,8 +2203,12 @@ def get_model_context_length(
if endpoint_context is not None:
return endpoint_context
is_bedrock_context = _is_bedrock_context(base_url, provider)
- # 1. Persistent cache (LM Studio / Codex OAuth excluded — see _skip_persistent_context_cache).
- cached = get_cached_context_length(model, base_url) if base_url and not is_bedrock_context and not _skip_persistent_context_cache(base_url, provider) else None
+ # A Codex Responses route is keyed on its transport, not its host: behind a proxy
+ # (HERMES_CODEX_BASE_URL, model.base_url, custom api_mode: codex_responses) the URL looks
+ # generic while the window is still the Codex OAuth one (#116191).
+ codex_route = _is_codex_route(provider, base_url, custom_providers)
+ # 1. Persistent cache (LM Studio / Codex routes excluded — see _skip_persistent_context_cache).
+ cached = get_cached_context_length(model, base_url) if base_url and not is_bedrock_context and not codex_route and not _skip_persistent_context_cache(base_url, provider) else None
validated = _validate_cached_context_length(model, base_url, cached, api_key=api_key) if cached is not None else None
if validated is not None:
return validated
@@ -2192,9 +2224,11 @@ def get_model_context_length(
save_context_length(model, base_url, ctx)
return ctx
# 2. Live /models for truly custom endpoints. Known providers skip this: their /models may
- # report a provider-imposed limit (Copilot: 128k) rather than the window.
- if _is_custom_endpoint(base_url) and not _is_known_provider_base_url(base_url):
- return _resolve_custom_endpoint_context_length(model, base_url, api_key, provider)
+ # report a provider-imposed limit (Copilot: 128k) rather than the window. The native
+ # openai-codex provider skips it too even on a proxy URL — step 5 runs its live catalog probe.
+ if _is_custom_endpoint(base_url) and not _is_known_provider_base_url(base_url) and (provider or "").strip().lower() != "openai-codex":
+ resolve = _resolve_custom_codex_route_context_length if codex_route else _resolve_custom_endpoint_context_length
+ return resolve(model, base_url, api_key, provider)
# 4. Anthropic /v1/models API (only for regular API keys, not OAuth)
if provider == "anthropic" or (base_url and base_url_hostname(base_url) == "api.anthropic.com"):
ctx = _query_anthropic_context_length(model, base_url or "https://api.anthropic.com", api_key)
diff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.py
index 02f0ad8f480..29964bd8108 100644
--- a/tests/agent/test_model_metadata.py
+++ b/tests/agent/test_model_metadata.py
@@ -1325,7 +1325,43 @@ class TestGetModelContextLength:
# The local probe MUST be called exactly once
mock_local_ctx.assert_called_once()
+ # ── Codex routes are keyed on the transport, not the host (#116191) ──────────────
+ @pytest.mark.parametrize(
+ "provider, custom_providers",
+ [
+ ("custom:codex-proxy", [{"name": "codex-proxy", "base_url": "http://127.0.0.1:8317/v1", "api_mode": "codex_responses"}]),
+ ("openai-codex", None), # HERMES_CODEX_BASE_URL / model.base_url proxy per #115902
+ ],
+ )
+ def test_codex_route_behind_proxy_resolves_codex_oauth_window(self, provider, custom_providers):
+ """A Codex model served through a generic proxy URL must get the Codex OAuth window, not the
+ direct-API catalog window: the compressor otherwise fires ~2x past the Codex limit."""
+ from agent import model_metadata as mm
+ proxy_models = {"gpt-6-astra": {"id": "gpt-6-astra"}} # like CLIProxyAPI's /models: no context field
+ with (
+ patch.object(mm, "get_cached_context_length", return_value=1_050_000), # stale pre-fix entry must not win
+ patch.object(mm, "fetch_endpoint_model_metadata", return_value=proxy_models),
+ patch.object(mm, "_query_ollama_api_show", return_value=None),
+ patch.object(mm, "is_local_endpoint", return_value=False),
+ patch.object(mm, "_fetch_codex_oauth_context_lengths_with_source", return_value=({}, False)),
+ ):
+ ctx = get_model_context_length(
+ "gpt-6-astra", base_url="http://127.0.0.1:8317/v1", api_key="proxy-key",
+ provider=provider, custom_providers=custom_providers,
+ )
+ assert ctx == mm._CODEX_OAUTH_CONTEXT_FALLBACK["gpt-6-astra"]
+
+ def test_codex_proxy_route_explicit_context_length_override_still_wins(self):
+ """providers.<name>.models[].context_length beats the Codex table on a codex_responses route (#102644)."""
+ custom = [{
+ "name": "codex-proxy", "base_url": "http://127.0.0.1:8317/v1", "api_mode": "codex_responses",
+ "models": {"gpt-6-astra": {"context_length": 321_000}},
+ }]
+ ctx = get_model_context_length(
+ "gpt-6-astra", base_url="http://127.0.0.1:8317/v1", provider="custom:codex-proxy", custom_providers=custom,
+ )
+ assert ctx == 321_000
# =========================================================================
diff --git a/website/docs/integrations/providers.md b/website/docs/integrations/providers.md
index f872d61b62e..896585ad6db 100644
--- a/website/docs/integrations/providers.md
+++ b/website/docs/integrations/providers.md
@@ -1391,6 +1391,8 @@ Not to be confused with `secrets.command`, which runs a helper **once at startup
Older configs used a top-level `custom_providers:` list instead. It still works — Hermes reads both — and `hermes update` auto-migrates it to the `providers:` dict (config v12). Field names differ slightly in the dict format: legacy `model` is `default_model`, and legacy `api_mode` is `transport`.
:::
+**Context window on `codex_responses` proxies.** A custom entry with `transport: codex_responses` (a local Codex proxy, for example) resolves the context window of Codex OAuth models (`gpt-6-astra`, `gpt-5.6-sol`/`-terra`/`-luna`, `gpt-5.5`, …) from the Codex OAuth table — 272K for most slugs — not from the 1.05M direct-API catalog, so compression fires before the Codex backend's limit and its 272K billing tier. The decision follows the transport, not the hostname; the same holds for `openai-codex` behind `HERMES_CODEX_BASE_URL` or `model.base_url`. A per-model `models.<id>.context_length`, an entry-level `context_length`, or `model.context_length` still wins; the opt-in `-900k` picker variants keep their verified 900K.
+
**Reasoning effort on custom endpoints.** The configured `reasoning_effort` (`/reasoning max`, `agent.reasoning_effort`) reaches a custom endpoint unchanged on both the `chat_completions` and the `codex_responses` transport — up to `max`; only the Hermes-internal `ultra` is clamped to `max`. Two exceptions follow the host rather than the entry: a custom entry pointed at `api.openai.com` keeps OpenAI's per-model ladder (`max` is a gpt-5.6-only level there), and an entry pointed at a provider whose profile publishes a per-model vocabulary (Ramp Router) is clamped to that catalog. An endpoint that rejects the level answers with an HTTP 400 instead of Hermes silently downgrading it.
Some OpenAI-compatible endpoints need provider-specific request body fields. Add an `extra_body` map to the matching custom provider and Hermes will merge it into each chat-completions request for that endpoint: