Skip to content

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

TypeScript

fix(grok-llm-proxy): log a sanitized upstream error hint on non-success

Jose Gabriel Ramirez Encinas4479c761e14b
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index 820a0e107..51441d4d8 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -202,6 +202,33 @@ async function finalizeQuietly(
}
}
+/**
+ * A short, sanitized hint from a non-success upstream response. Upstream error
+ * bodies carry the reason a request was refused (for example a required client
+ * identity or an entitlement message), which the status alone does not give.
+ * Never logs credentials: anything token-shaped is dropped and the hint is
+ * capped, so a body that echoes a header or key cannot reach the log.
+ */
+export async function readUpstreamErrorHint(response: {
+ text: () => Promise<string>
+}): Promise<string> {
+ let raw: string
+ try {
+ raw = await response.text()
+ } catch {
+ return ''
+ }
+ const collapsed = raw.replace(/\s+/g, ' ').trim()
+ if (!collapsed) return ''
+ const withoutSecrets = collapsed
+ .replace(
+ /(?:Bearer|token|key|secret|authorization)[\s"':=]+[A-Za-z0-9._~+/-]{8,}/gi,
+ '[redacted]'
+ )
+ .replace(/\b[A-Za-z0-9._-]{40,}\b/g, '[redacted]')
+ return withoutSecrets.slice(0, 300)
+}
+
async function readUpstreamStream(input: {
request: GrokCompletionRequestV1
accessToken: string
@@ -241,6 +268,7 @@ async function readUpstreamStream(input: {
event: 'grok_upstream_http',
operation: 'completion_stream',
status: response.status,
+ upstreamHint: await readUpstreamErrorHint(response),
},
'Grok completions upstream returned a non-success status'
)
diff --git a/grok-llm-proxy/test/upstreamErrorHint.test.ts b/grok-llm-proxy/test/upstreamErrorHint.test.ts
new file mode 100644
index 000000000..72ce54d9c
--- /dev/null
+++ b/grok-llm-proxy/test/upstreamErrorHint.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from 'vitest'
+import { readUpstreamErrorHint } from '../src/grokTransport'
+
+describe('readUpstreamErrorHint', () => {
+ it('returns a collapsed, capped hint from the upstream error body', async () => {
+ const body = '{\n "error": "client version is no longer supported",\n "code": 426\n}'
+ await expect(readUpstreamErrorHint({ text: async () => body })).resolves.toBe(
+ '{ "error": "client version is no longer supported", "code": 426 }'
+ )
+ })
+
+ it('drops credential-shaped values and caps the length', async () => {
+ const token = 'a'.repeat(64)
+ const hint = await readUpstreamErrorHint({
+ text: async () => `unauthorized: Bearer ${token} for key=${token} ${'x'.repeat(600)}`,
+ })
+ expect(hint).not.toContain(token)
+ expect(hint).toContain('[redacted]')
+ expect(hint.length).toBeLessThanOrEqual(300)
+ })
+
+ it('is empty when the body cannot be read', async () => {
+ await expect(
+ readUpstreamErrorHint({
+ text: async () => {
+ throw new Error('stream closed')
+ },
+ })
+ ).resolves.toBe('')
+ })
+})