rohitg00/agentmemory · error · Error

OpenAI API error (${response.status}): ${text}

Error message

OpenAI API error (${response.status}): ${text}

What it means

OpenAIProvider.call throws this when the Chat Completions HTTP response status is not ok; the raw response body text is included for diagnosis. It surfaces upstream rejections — 401 auth, 404 unknown model, 429 quota/rate limit, 5xx outage — with the provider's own error payload appended.

Source

Thrown at src/providers/openai.ts:128

          method: "POST",
          headers: buildAuthHeaders(this.apiKey, this.isAzure),
          body: JSON.stringify(body),
        },
        this.timeoutMs,
      );
    } catch (err) {
      const aborted = err instanceof Error && err.name === "AbortError";
      if (aborted) {
        throw new Error(
          `OpenAI API request timed out after ${this.timeoutMs}ms — set OPENAI_TIMEOUT_MS (or AGENTMEMORY_LLM_TIMEOUT_MS) to raise the bound or check the provider status.`,
        );
      }
      throw err;
    }

    if (!response.ok) {
      const text = await response.text();
      throw new Error(`OpenAI API error (${response.status}): ${text}`);
    }

    const data = (await response.json()) as {
      choices?: Array<{
        message?: { content?: string; reasoning?: string; reasoning_content?: string };
      }>;
    };
    const message = data.choices?.[0]?.message;
    const content = message?.content;
    if (content) {
      return content;
    }
    // Fallback: some thinking models return reasoning but no content.
    // DeepSeek V4 / Qwen3 / GLM / Kimi return `reasoning_content`;
    // older OpenAI o-series + some compatibles return `reasoning`. #627
    const reasoning = message?.reasoning ?? message?.reasoning_content;
    if (reasoning) {
      return reasoning;

View on GitHub (pinned to e04ba88819)

Solutions

  1. Inspect the appended response text for OpenAI's error code and message
  2. If 401: regenerate and re-export OPENAI_API_KEY
  3. If 404: correct the config.model to a valid model id
  4. If 429/insufficient_quota: wait, add billing credit, or lower request rate; retry with backoff
  5. If 5xx: retry later or switch provider via createFallbackProvider

Example fix

// before
model: 'gpt-4o-mni' // OpenAI API error (404): model not found

// after
model: 'gpt-4o-mini'
Defensive patterns

Strategy: try-catch

Validate before calling

if (!process.env.OPENAI_API_KEY) throw new Error('OPENAI_API_KEY not set');
if (!/^[a-z0-9.\/-]+$/i.test(config.model ?? '')) throw new Error(`Suspicious model id: ${config.model}`);

Try / catch

try {
  return await provider.call(prompt);
} catch (e) {
  const m = /OpenAI API error \((\d+)\)/.exec(String(e));
  const status = m ? Number(m[1]) : 0;
  if (status === 429 || status >= 500) return retryWithBackoff(() => provider.call(prompt));
  if (status === 401 || status === 404) throw new Error(`OpenAI config problem (${status}); check key/model`, { cause: e });
  throw e;
}

Prevention

When it happens

Trigger: compress() or summarize() via OpenAIProvider.call() where fetch resolves with response.ok === false; wrong OPENAI_API_KEY, nonexistent or deprecated model id, quota exhausted, or OpenAI-side 5xx.

Common situations: Revoked/rotated key not updated in env (401 'Incorrect API key'); model name typo like 'gpt-4o-mni' (404); hitting usage tier limits (429); org billing issue (insufficient_quota); pointing OPENAI_BASE_URL at a compatible endpoint that rejects the request shape.

Related errors


AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30). Data as JSON: /api/errors/d99dff1be7d5fed3. Report an issue: GitHub.