{"record":{"id":"12ef2f5f02c99d5b","repo":"decolua/9router","slug":"failed-to-consume-codex-reset-credit-error-mess","errorCode":null,"errorMessage":"Failed to consume Codex reset credit: ${error.message}","messagePattern":"Failed to consume Codex reset credit: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"open-sse/services/usage/codex.js","lineNumber":205,"sourceCode":"  }\n\n  let response;\n  let data = null;\n  try {\n    response = await proxyAwareFetch(CODEX_CONFIG.resetCreditsConsumeUrl, {\n      method: \"POST\",\n      headers: {\n        \"Authorization\": `Bearer ${accessToken}`,\n        \"Accept\": \"application/json\",\n        \"Content-Type\": \"application/json\",\n      },\n      body: JSON.stringify({ redeem_request_id: redeemRequestId }),\n    }, proxyOptions);\n\n    const text = await response.text();\n    data = text ? JSON.parse(text) : null;\n  } catch (error) {\n    throw new Error(`Failed to consume Codex reset credit: ${error.message}`);\n  }\n\n  const code = data?.code || null;\n  const windowsReset = toFiniteNumber(data?.windows_reset, 0);\n  const success = response.ok && (code === \"reset\" || windowsReset > 0);\n\n  return {\n    ok: success,\n    noCredit: response.ok && code === \"no_credit\",\n    status: response.status,\n    code,\n    windowsReset,\n    message: data?.message || null,\n    raw: data,\n  };\n}\n","sourceCodeStart":187,"sourceCodeEnd":222,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/open-sse/services/usage/codex.js#L187-L222","documentation":"This error wraps any failure that occurs while calling the Codex upstream endpoint to redeem a rate-limit reset credit. The library performs a proxied fetch that posts { redeem_request_id } and parses the response as JSON; any network error, proxy failure, or invalid-JSON body is caught and re-thrown with this prefix, discarding the original error type. It indicates the credit-consumption step of the rate-limit reset flow did not complete.","triggerScenarios":"consumeCodexRateLimitResetCredit is invoked (via consumeResult or the POST handler) and: the upstream fetch throws (network/timeout/DNS), the proxy options are invalid, or response.text() returns non-JSON text that JSON.parse cannot parse. Redeem request ids for expired or already-consumed credits may also surface through here if the upstream responds unparseably.","commonSituations":"Corporate proxy misconfiguration, the Codex endpoint being unreachable or returning an HTML error page, expired/redeemed redeem_request_id causing a non-JSON error body, or transient network flaps during the reset call.","solutions":["Check network/proxy connectivity to the Codex endpoint from the machine running the gateway","Log the underlying error.message (included in the thrown message) to identify whether it is fetch vs JSON.parse","Retry with a fresh redeem_request_id — the old one may be expired or already consumed","Verify proxyOptions (proxy URL/agent) are correct if a proxy is configured"],"exampleFix":"// before\nconst text = await response.text();\ndata = text ? JSON.parse(text) : null;\n// after\nconst text = await response.text();\ntry {\n  data = text ? JSON.parse(text) : null;\n} catch (parseErr) {\n  throw new Error(`Codex reset credit response not JSON (status ${response.status}): ${text.slice(0, 200)}`);\n}","handlingStrategy":"try-catch","validationCode":"if (typeof redeemRequestId !== \"string\" || !redeemRequestId) throw new Error(\"redeem_request_id required before consuming Codex reset credit\");","typeGuard":"function hasRedeemId(v) { return typeof v === \"string\" && v.length > 0; }","tryCatchPattern":"try {\n  await consumeCodexRateLimitResetCredit(args);\n} catch (e) {\n  if (/Failed to consume Codex reset credit/.test(e.message)) {\n    logger.warn(\"codex reset credit failed\", { cause: e.message });\n    // schedule retry with a fresh redeem_request_id\n  } else throw e;\n}","preventionTips":["Always mint a fresh redeem_request_id per attempt","Validate proxyOptions before calling if a proxy is in play","Log response status + body prefix on parse failures","Add retry with backoff for transient network errors"],"tags":["network","codex","proxy","json-parse"],"backgroundTag":"credit-redemption-request-failed","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}