Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

Remove Codex model registry cache

ehendrix234be67178647f
diff --git a/headroom/providers/proxy_routes.py b/headroom/providers/proxy_routes.py
index 3f26910f5..94a7933f3 100644
--- a/headroom/providers/proxy_routes.py
+++ b/headroom/providers/proxy_routes.py
@@ -6,7 +6,6 @@ from __future__ import annotations
import json
import logging
import os
-import time
from typing import Any, TypedDict, cast
from urllib.parse import quote
@@ -17,8 +16,6 @@ from headroom.proxy.handlers.openai import _resolve_codex_routing_headers
logger = logging.getLogger("headroom.proxy.routes")
-_CODEX_MODELS_CACHE: dict[tuple[str, str], tuple[float, tuple[str, ...]]] = {}
-
class OpenAIModelItem(TypedDict):
id: str
@@ -88,18 +85,6 @@ def _codex_client_version(requested_client_version: str | None = None) -> str:
return os.getenv("HEADROOM_CODEX_CLIENT_VERSION", "0.130.0")
-def _codex_models_cache_ttl_seconds() -> float:
- raw_value = os.getenv("HEADROOM_CODEX_MODELS_CACHE_TTL_SECONDS", "300")
- try:
- return max(0.0, float(raw_value))
- except ValueError:
- logger.warning(
- "Invalid HEADROOM_CODEX_MODELS_CACHE_TTL_SECONDS=%r; disabling Codex models cache",
- raw_value,
- )
- return 0.0
-
-
def _openai_model_item(model_id: str) -> OpenAIModelItem:
return {
"id": model_id,
@@ -171,21 +156,7 @@ async def _fetch_chatgpt_codex_model_ids(
requested_client_version: str | None,
) -> tuple[str, ...] | None:
client_version = _codex_client_version(requested_client_version)
- upstream_headers, account_id = _normalize_codex_registry_headers(headers)
- cache_key = (account_id, client_version)
- cache_ttl = _codex_models_cache_ttl_seconds()
- now = time.monotonic()
- cached = _CODEX_MODELS_CACHE.get(cache_key)
- if cached is not None:
- cached_at, cached_model_ids = cached
- if cache_ttl > 0 and now - cached_at < cache_ttl:
- logger.debug(
- "Using cached Codex model IDs from upstream model registry: %s",
- list(cached_model_ids),
- )
- return cached_model_ids
- _CODEX_MODELS_CACHE.pop(cache_key, None)
-
+ upstream_headers, _account_id = _normalize_codex_registry_headers(headers)
url = (
"https://chatgpt.com/backend-api/codex/models"
f"?client_version={quote(client_version, safe='')}"
@@ -222,8 +193,6 @@ async def _fetch_chatgpt_codex_model_ids(
logger.warning("Codex model registry returned no model slugs")
return None
- if cache_ttl > 0:
- _CODEX_MODELS_CACHE[cache_key] = (now, model_ids)
logger.info("Fetched %d Codex models from upstream model registry", len(model_ids))
logger.debug("Fetched Codex model IDs from upstream model registry: %s", list(model_ids))
return model_ids
diff --git a/tests/test_provider_proxy_routes.py b/tests/test_provider_proxy_routes.py
index 204ca3fb8..24478fa90 100644
--- a/tests/test_provider_proxy_routes.py
+++ b/tests/test_provider_proxy_routes.py
@@ -341,7 +341,6 @@ def test_gemini_batch_embed_contents_passthrough_uses_gemini_target(monkeypatch)
def test_v1_models_fetches_codex_registry_under_chatgpt_auth(monkeypatch) -> None:
monkeypatch.setenv("HEADROOM_CODEX_CLIENT_VERSION", "0.130.0")
- monkeypatch.setenv("HEADROOM_CODEX_MODELS_CACHE_TTL_SECONDS", "0")
proxy_routes = importlib.import_module("headroom.providers.proxy_routes")
debug_messages: list[tuple[str, tuple[object, ...]]] = []
monkeypatch.setattr(
@@ -424,8 +423,6 @@ def test_v1_models_falls_back_to_synthetic_list_under_chatgpt_auth(monkeypatch)
synthesize an OpenAI-compatible response with the known-supported
Codex/ChatGPT model set instead, so Codex's model-picker refresh succeeds.
"""
- monkeypatch.setenv("HEADROOM_CODEX_MODELS_CACHE_TTL_SECONDS", "0")
-
class FakeAsyncClient:
async def get(self, url, **kwargs): # type: ignore[no-untyped-def]
return httpx.Response(403, json={"error": "forbidden"})
@@ -458,13 +455,10 @@ def test_v1_models_falls_back_to_synthetic_list_under_chatgpt_auth(monkeypatch)
assert entry["owned_by"] == "openai"
-def test_v1_models_get_single_dynamic_under_chatgpt_auth(monkeypatch) -> None:
+def test_v1_models_get_single_dynamic_under_chatgpt_auth() -> None:
"""The single-model variant (`/v1/models/{id}`) is also called by
Codex for some flows. It should use the Codex registry first so
dynamically exposed model slugs validate consistently."""
- monkeypatch.setenv("HEADROOM_CODEX_MODELS_CACHE_TTL_SECONDS", "300")
- importlib.import_module("headroom.providers.proxy_routes")._CODEX_MODELS_CACHE.clear()
-
class FakeAsyncClient:
def __init__(self) -> None:
self.calls = 0
@@ -504,7 +498,7 @@ def test_v1_models_get_single_dynamic_under_chatgpt_auth(monkeypatch) -> None:
"owned_by": "openai",
}
assert unknown.status_code == 404
- assert fake_http_client.calls == 1
+ assert fake_http_client.calls == 2
def test_v1_models_still_forwards_under_non_chatgpt_auth() -> None:
diff --git a/tests/test_proxy_codex_route_aliases.py b/tests/test_proxy_codex_route_aliases.py
index d5804aa90..84d6d86dd 100644
--- a/tests/test_proxy_codex_route_aliases.py
+++ b/tests/test_proxy_codex_route_aliases.py
@@ -167,7 +167,6 @@ def test_codex_model_metadata_fetches_codex_registry_for_chatgpt_auth(monkeypatc
OpenAI-compatible payload from its slugs.
"""
monkeypatch.setenv("HEADROOM_CODEX_CLIENT_VERSION", "0.130.0")
- monkeypatch.setenv("HEADROOM_CODEX_MODELS_CACHE_TTL_SECONDS", "0")
class FakeAsyncClient:
def __init__(self):