Skip to content

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

TypeScript

fix(grok-subscription): forward the upstream context overflow as context_length_exceeded (#739)

Alfredo Lopezb5eaba078cd3
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index 76e37272b..92312b753 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -281,15 +281,19 @@ async function readUpstreamStream(input: {
},
})
if (!response.ok || !response.body) {
+ const errorBody = await readUpstreamErrorBody(response)
logger.warn(
{
event: 'grok_upstream_http',
operation: 'completion_stream',
status: response.status,
- upstreamHint: await readUpstreamErrorHint(response),
+ upstreamHint: await readUpstreamErrorHint({ text: async () => errorBody.text }),
},
'Grok completions upstream returned a non-success status'
)
+ if (response.status !== 401 && errorBody.complete && isContextOverflowBody(errorBody.text)) {
+ throw new GrokTransportError('context_length_exceeded', 'upstream context window exceeded')
+ }
if (response.status === 400) {
throw new GrokTransportError('invalid_request', 'upstream rejected the Grok request')
}
@@ -319,6 +323,58 @@ async function readUpstreamStream(input: {
return consumeSse(response.body, input.onFrame, signal, names)
}
+// R10 (M1): a non-success completion body is read once, up to this bound, for
+// the log hint and to find a context-window refusal. The recorded refusal is
+// about 200 bytes; past the bound the read is cancelled, the prefix still feeds
+// the hint, and the status mapping stands.
+const UPSTREAM_ERROR_BODY_MAX_BYTES = 16 * 1024
+
+// The recorded refusal (HTTP 400 from /v1/responses, grok-4.6, 2026-09-23) has
+// the generic `code: 'invalid-argument'`; this bracketed marker inside its
+// `error` string is the only field that names the context window.
+const CONTEXT_OVERFLOW_MARKER = '[input_too_large]'
+
+async function readUpstreamErrorBody(
+ response: Response
+): Promise<{ text: string; complete: boolean }> {
+ if (!response.body) return { text: '', complete: true }
+ const reader = response.body.getReader()
+ const chunks: Uint8Array[] = []
+ let total = 0
+ let complete = true
+ try {
+ for (;;) {
+ const { done, value } = await reader.read()
+ if (done) break
+ total += value.byteLength
+ if (total > UPSTREAM_ERROR_BODY_MAX_BYTES) {
+ complete = false
+ await reader.cancel().catch(() => undefined)
+ break
+ }
+ chunks.push(value)
+ }
+ } catch {
+ complete = false
+ await reader.cancel().catch(() => undefined)
+ }
+ return { text: Buffer.concat(chunks).toString('utf8'), complete }
+}
+
+function isContextOverflowBody(text: string): boolean {
+ let parsed: unknown
+ try {
+ parsed = JSON.parse(text)
+ } catch {
+ return false
+ }
+ return (
+ isPlainObject(parsed) &&
+ typeof parsed.error === 'string' &&
+ parsed.error.includes(CONTEXT_OVERFLOW_MARKER)
+ )
+}
+
function toUpstreamPayload(
request: GrokCompletionRequestV1,
names: ToolNameMap
diff --git a/grok-llm-proxy/src/server.ts b/grok-llm-proxy/src/server.ts
index 3d06e5877..a9bdbfcfa 100644
--- a/grok-llm-proxy/src/server.ts
+++ b/grok-llm-proxy/src/server.ts
@@ -529,6 +529,9 @@ export function startProxy(config: GrokLlmProxyConfig): ProxyServers {
// bounded by the proxy.
const ATTEMPT_ERROR_STATUS: Record<string, number> = {
invalid_request: 400,
+ // The upstream's refusal of a prompt over the model's context window, at the
+ // upstream's own status class (R10).
+ context_length_exceeded: 400,
Unauthorized: 401,
origin_denied: 403,
request_hash_mismatch: 403,
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 23b8a8635..fdfe69f7a 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.429",
+ "version": "0.1.430",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.429",
+ "version": "0.1.430",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 1dc688c80..e811fda3d 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.429",
+ "version": "0.1.430",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index c024f1734..993146671 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -270,10 +270,13 @@ export class GrokSubscriptionProvider implements SingleTurnProvider {
// being failover-eligible, spend a second provider on it.
// `payload_too_large` is the proxy's 413 for an envelope over its body
// limit: the same size refusal of this conversation, one hop later (#731).
+ // `context_length_exceeded` is the upstream's refusal of a prompt over the
+ // model's context window, forwarded by the proxy (R10).
if (
code === 'request_limit_exceeded' ||
code === 'tool_call_arguments_exceeded' ||
- code === 'payload_too_large'
+ code === 'payload_too_large' ||
+ code === 'context_length_exceeded'
) {
return {
code: LlmErrorCode.ContextLengthExceeded,
diff --git a/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json b/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
index ee92857d1..47e64e8f4 100644
--- a/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
+++ b/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
@@ -79,7 +79,8 @@
"fingerprint_in_use",
"reauth_required",
"disabled",
- "invalid_tool_arguments"
+ "invalid_tool_arguments",
+ "context_length_exceeded"
],
"termsAndTestAccount": {
"automationAuthorized": false,
diff --git a/tests/e2e/fixtures/package-lock.json b/tests/e2e/fixtures/package-lock.json
index 61cfb9b4f..b0064d6de 100644
--- a/tests/e2e/fixtures/package-lock.json
+++ b/tests/e2e/fixtures/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.93",
+ "version": "1.0.94",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.93",
+ "version": "1.0.94",
"dependencies": {
"@kubernetes/client-node": "^1.0.0"
},
diff --git a/tests/e2e/fixtures/package.json b/tests/e2e/fixtures/package.json
index 07da76c37..18c8fded1 100644
--- a/tests/e2e/fixtures/package.json
+++ b/tests/e2e/fixtures/package.json
@@ -1,6 +1,6 @@
{
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.93",
+ "version": "1.0.94",
"description": "E2E test fixtures and utilities for Clerum testing",
"type": "module",
"main": "index.ts",