{"record":{"id":"33442e67bff8b813","repo":"rohitg00/agentmemory","slug":"this-name-api-error-response-status-tex","errorCode":null,"errorMessage":"${this.name} API error (${response.status}): ${text}","messagePattern":"(.+?) API error \\((.+?)\\): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/providers/openrouter.ts","lineNumber":57,"sourceCode":"        \"Content-Type\": \"application/json\",\n        Authorization: `Bearer ${this.apiKey}`,\n        ...(this.baseUrl.includes(\"openrouter\")\n          ? { \"HTTP-Referer\": \"https://github.com/rohitg00/agentmemory\" }\n          : {}),\n      },\n      body: JSON.stringify({\n        model: this.model,\n        max_tokens: this.maxTokens,\n        messages: [\n          { role: \"system\", content: systemPrompt },\n          { role: \"user\", content: userPrompt },\n        ],\n      }),\n    });\n\n    if (!response.ok) {\n      const text = await response.text();\n      throw new Error(`${this.name} API error (${response.status}): ${text}`);\n    }\n\n    const data = (await response.json()) as Record<string, unknown>;\n    const choices = data.choices as\n      | Array<{ message: { content: string } }>\n      | undefined;\n    const content = choices?.[0]?.message?.content;\n    if (!content) {\n      throw new Error(\n        `${this.name} returned unexpected response: ${JSON.stringify(data).slice(0, 200)}`,\n      );\n    }\n    return content;\n  }\n}\n","sourceCodeStart":39,"sourceCodeEnd":73,"githubUrl":"https://github.com/rohitg00/agentmemory/blob/e04ba88819c365c9acf9d6661ea802143e728bd6/src/providers/openrouter.ts#L39-L73","documentation":"OpenRouterProvider.call throws `${this.name} API error (${status}): ${text}` for any non-2xx OpenRouter response, embedding the provider display name and the raw body. OpenRouter errors carry detail in the body — invalid key, no credits, model not found, or upstream model provider failure — so the appended text is the primary diagnostic.","triggerScenarios":"compress()/summarize() via OpenRouterProvider.call() where response.ok is false: bad OPENROUTER_API_KEY (401), exhausted OpenRouter credits (402), unknown model slug in config.model (404), OpenRouter rate limit (429), or upstream provider error (502).","commonSituations":"Running out of OpenRouter credits mid-month; using a stale model slug after OpenRouter deprecates ':free' variants; key restricted to certain models by allowed-models policy; upstream model provider (e.g. a free backend) being temporarily down.","solutions":["Read the appended body text — OpenRouter returns JSON with the exact error metadata","If 402/401: add credits or fix the OpenRouter API key","If 404: update config.model to a current OpenRouter model slug (check openrouter.ai/models)","If 429: back off and retry, or switch to a less-rate-limited model/provider","Wrap with the resilient provider so the circuit breaker and fallbacks absorb transient 5xx"],"exampleFix":"// before\nmodel: 'anthropic/claude-3-sonnet' // 404: no endpoints found matching that model\n\n// after\nmodel: 'anthropic/claude-3.5-sonnet'","handlingStrategy":"retry","validationCode":"if (!process.env.OPENROUTER_API_KEY) throw new Error('OPENROUTER_API_KEY not set');\nif (!config.model?.includes('/')) throw new Error(`Model must be a vendor/slug pair, got: ${config.model}`);","typeGuard":null,"tryCatchPattern":"try {\n  return await provider.call(prompt);\n} catch (e) {\n  const m = /API error \\((\\d+)\\)/.exec(String(e));\n  const status = m ? Number(m[1]) : 0;\n  if (status === 429 || status >= 500) return retryWithBackoff(() => provider.call(prompt), 3);\n  if (status === 402) throw new Error('OpenRouter credits exhausted', { cause: e });\n  throw e;\n}","preventionTips":["Alert on OpenRouter credit balance before 402s occur","Pin current model slugs and re-check openrouter.ai/models after deprecation notices","Route through ResilientProvider/createFallbackProvider for transient 5xx","Honor 429 Retry-After semantics in your retry loop"],"tags":["network","api-error","openrouter","http-status"],"backgroundTag":"upstream-api-error","analyzedSha":"e04ba88819c365c9acf9d6661ea802143e728bd6","analyzedAt":"2026-08-30T01:07:40.754Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}