Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(codex-llm-proxy): map a context-window refusal in an HTTP error body (#739)

Alfredo Lopez22b02d1ab884
diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.ts
index 319d9274e..94c7ab08b 100644
--- a/codex-llm-proxy/src/codexTransport.ts
+++ b/codex-llm-proxy/src/codexTransport.ts
@@ -246,19 +246,28 @@ async function readUpstreamStream(input: {
},
})
if (!response.ok || !response.body) {
+ const credentialRejected = response.status === 401 || response.status === 403
+ const errorBody =
+ response.ok || credentialRejected
+ ? { read: 'skipped' as const }
+ : await readUpstreamErrorBody(response)
logger.warn(
{
event: 'codex_upstream_http',
operation: 'completion_stream',
status: response.status,
accountHeader: Boolean(headers['chatgpt-account-id']),
+ errorBody: errorBody.read,
},
'Codex completions upstream returned a non-success status'
)
+ if (errorBody.read === 'parsed' && errorBody.code === CONTEXT_LENGTH_EXCEEDED) {
+ throw new CodexTransportError(CONTEXT_LENGTH_EXCEEDED, 'upstream context window exceeded')
+ }
if (response.status === 400) {
throw new CodexTransportError('invalid_request', 'upstream rejected the Codex request')
}
- if (response.status === 401 || response.status === 403) {
+ if (credentialRejected) {
throw new CodexTransportError(
'connection_unavailable',
'upstream rejected the Codex credential'
@@ -269,6 +278,45 @@ async function readUpstreamStream(input: {
return consumeSse(response.body, input.onFrame, signal, names)
}
+// R9-6: a non-success completion body is read only to find a context-window
+// refusal. The recorded refusal object is well under 1 KiB; past this bound the
+// body is cancelled and the status mapping stands.
+const UPSTREAM_ERROR_BODY_MAX_BYTES = 16 * 1024
+
+// `read` is a closed set that is safe to log; no upstream text leaves here.
+type UpstreamErrorBody =
+ | { read: 'skipped' | 'absent' | 'oversize' | 'unreadable' | 'unparsable' }
+ | { read: 'parsed'; code: string | undefined }
+
+async function readUpstreamErrorBody(response: Response): Promise<UpstreamErrorBody> {
+ if (!response.body) return { read: 'absent' }
+ const reader = response.body.getReader()
+ const chunks: Uint8Array[] = []
+ let total = 0
+ try {
+ for (;;) {
+ const { done, value } = await reader.read()
+ if (done) break
+ total += value.byteLength
+ if (total > UPSTREAM_ERROR_BODY_MAX_BYTES) {
+ await reader.cancel().catch(() => undefined)
+ return { read: 'oversize' }
+ }
+ chunks.push(value)
+ }
+ } catch {
+ await reader.cancel().catch(() => undefined)
+ return { read: 'unreadable' }
+ }
+ let parsed: unknown
+ try {
+ parsed = JSON.parse(Buffer.concat(chunks).toString('utf8'))
+ } catch {
+ return { read: 'unparsable' }
+ }
+ return { read: 'parsed', code: isPlainObject(parsed) ? upstreamFailureCode(parsed) : undefined }
+}
+
function toUpstreamPayload(
request: CodexCompletionRequest,
names: ToolNameMap
diff --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.md
index 2ecbf9862..458df79cc 100644
--- a/docs/architecture/codex-subscription-transport-contract.md
+++ b/docs/architecture/codex-subscription-transport-contract.md
@@ -369,9 +369,14 @@ Stable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,
exceeds the model's context window. The upstream sends an SSE `error` event
(`error.code`) and a `response.failed` event (`response.error.code`) with
this code; the proxy forwards it as HTTP 400, or as an SSE error frame when
- text had already been streamed. It is the only upstream code the proxy
- forwards: a failure with any other upstream code, or none, stays
- `provider_unavailable`. The Host maps it to `LLM_CONTEXT_LENGTH_EXCEEDED`,
+ text had already been streamed. The same `error.code` in the JSON body of a
+ non-success HTTP reply other than 401/403 is mapped the same way; the proxy
+ reads at most `UPSTREAM_ERROR_BODY_MAX_BYTES` (16 KiB) of that body and
+ otherwise keeps the status mapping (400 `invalid_request`, 401/403
+ `connection_unavailable`, any other `provider_unavailable`). It is the only
+ upstream code the proxy forwards: a streamed failure with any other upstream
+ code, or none, stays `provider_unavailable`. The Host maps it to
+ `LLM_CONTEXT_LENGTH_EXCEEDED`,
not retryable, like `request_limit_exceeded`.
- `tool_call_limit_exceeded`: the upstream response carried more than