The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(codex): zstd-compress Responses request bodies to the official Codex backend
Tekniumaefffa077f0f
diff --git a/agent/codex_headers.py b/agent/codex_headers.pyindex 6fa2e53b084..84cbe955fcb 100644--- a/agent/codex_headers.py+++ b/agent/codex_headers.py@@ -81,6 +81,40 @@ def codex_account_headers(access_token: str) -> dict[str, str]:return headers+def codex_request_compression_hooks(base_url: str, *, async_mode: bool = False) -> dict[str, list] | None:+ """httpx ``event_hooks`` that zstd-compress Responses request bodies for the official Codex backend.++ Parity with the official Codex client (zstd on ``/responses`` with ChatGPT auth, default since+ openai/codex#10102). Lossless transport encoding: tokens and prompt caching are unchanged. Uses the+ stdlib codec (Python 3.14+); without it, or for any other endpoint/proxy, bodies stay raw JSON.+ """+ if not is_official_codex_base_url(base_url):+ return None+ try:+ from compression import zstd+ import httpx+ except ImportError:+ return None++ def compress(request: httpx.Request) -> None:+ if (request.method != "POST" or not request.url.path.endswith("/responses")+ or "content-encoding" in request.headers):+ return+ body = zstd.compress(request.read())+ request.stream = httpx.ByteStream(body)+ request._content = body+ request.headers["Content-Encoding"] = "zstd"+ request.headers["Content-Length"] = str(len(body))++ if not async_mode:+ return {"request": [compress]}++ async def compress_async(request: httpx.Request) -> None:+ compress(request)++ return {"request": [compress_async]}++def apply_required_codex_headers(client_kwargs: dict[str, Any], *, access_token: str, base_url: str) -> None:"""Keep required Codex identity after user/provider header overrides."""if not is_official_codex_base_url(base_url):diff --git a/agent/process_bootstrap.py b/agent/process_bootstrap.pyindex 13f22bcf56f..2fbaa1ad33e 100644--- a/agent/process_bootstrap.py+++ b/agent/process_bootstrap.py@@ -9,6 +9,7 @@ runs sync httpx connects through the process-wide Happy Eyeballs racerfrom __future__ import annotations+import functoolsimport socketimport sysimport threading@@ -291,6 +292,11 @@ def build_keepalive_http_client(base_url: str = "", *, async_mode: bool = False,transport_cls = httpx.AsyncHTTPTransport if async_mode else httpx.HTTPTransportclient_cls = httpx.AsyncClient if async_mode else httpx.Clientmounts = None+ # Official Codex backend only: zstd request bodies when the stdlib codec exists (None otherwise).+ from agent.codex_headers import codex_request_compression_hooks+ hooks = codex_request_compression_hooks(base_url, async_mode=async_mode)+ if hooks:+ client_cls = functools.partial(client_cls, event_hooks=hooks)if proxy is None:happy_eyeballs = not async_mode and _uses_codex_cloud_transport(base_url)# One pool serves every agent in the process, so its ceiling must cover a wholediff --git a/tests/agent/test_codex_request_compression.py b/tests/agent/test_codex_request_compression.pynew file mode 100644index 00000000000..7ff31af3bb9--- /dev/null+++ b/tests/agent/test_codex_request_compression.py@@ -0,0 +1,46 @@+"""Codex Responses bodies go out zstd-compressed to the official backend only (official-client parity)."""++import json++import pytest++from agent.process_bootstrap import build_keepalive_http_client++zstd = pytest.importorskip("compression.zstd")++OFFICIAL = "https://chatgpt.com/backend-api/codex"+++def _send_through_hooks(client, url: str, payload: dict):+ request = client.build_request("POST", url, json=payload)+ for hook in client.event_hooks["request"]:+ hook(request)+ return request+++def test_official_codex_responses_body_is_zstd_and_lossless():+ payload = {"model": "gpt-6.1-sol", "input": [{"role": "user", "content": "hi " * 500}]}+ client = build_keepalive_http_client(OFFICIAL)+ try:+ request = _send_through_hooks(client, f"{OFFICIAL}/responses", payload)+ finally:+ client.close()+ assert request.headers["Content-Encoding"] == "zstd"+ assert int(request.headers["Content-Length"]) == len(request.content)+ assert json.loads(zstd.decompress(request.content)) == payload++[email protected]("base_url, path", [+ ("https://my-proxy.example.com/backend-api/codex", "/responses"), # custom proxy: never compressed+ ("https://api.openai.com/v1", "/responses"),+ (OFFICIAL, "/models"), # official host, non-Responses endpoint+])+def test_other_destinations_stay_raw_json(base_url, path):+ payload = {"model": "x", "input": "hello"}+ client = build_keepalive_http_client(base_url)+ try:+ request = _send_through_hooks(client, f"{base_url}{path}", payload)+ finally:+ client.close()+ assert "Content-Encoding" not in request.headers+ assert json.loads(request.content) == payload