mem0ai/mem0 · error

xAI LLM failed: ${message}

Error message

xAI LLM failed: ${message}

What it means

Thrown by the xAI (Grok) wrapper when the OpenAI-compatible generateResponse call against https://api.x.ai/v1 (or a custom base URL) fails. The wrapper re-throws the parent OpenAILLM error prefixed with 'xAI LLM failed:' so the provider is identifiable in logs. The underlying message carries xAI's HTTP status and body.

Source

Thrown at mem0-ts/src/oss/src/llms/xai.ts:38

    super({
      ...config,
      apiKey,
      baseURL:
        config.baseURL || process.env.XAI_API_BASE || "https://api.x.ai/v1",
      model: config.model || "grok-4.3",
    });
  }

  async generateResponse(
    messages: Message[],
    responseFormat?: { type: string },
    tools?: any[],
  ): Promise<string | LLMResponse> {
    try {
      return await super.generateResponse(messages, responseFormat, tools);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      throw new Error(`xAI LLM failed: ${message}`);
    }
  }

  async generateChat(messages: Message[]): Promise<LLMResponse> {
    try {
      return await super.generateChat(messages);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      throw new Error(`xAI LLM failed: ${message}`);
    }
  }
}

View on GitHub (pinned to 001c235229)

Solutions

  1. Read the appended original message to classify: 401/403 = key, 404 = model, 429 = rate limit, 5xx/network = transient.
  2. Confirm the key works: curl https://api.x.ai/v1/models -H "Authorization: Bearer $XAI_API_KEY".
  3. Use a valid current model id (e.g. the configured default 'grok-4.3' or another grok model from the models endpoint).
  4. For 429s, throttle concurrent Memory.add() calls or add retry with backoff.
  5. If overriding XAI_API_BASE, verify the proxy forwards auth headers and paths correctly.

Example fix

// before
const memory = new Memory({
  llm: { provider: 'xai', config: { apiKey: process.env.XAI_API_KEY } },
});
await memory.add('hi', { userId: 'u1' }); // xAI LLM failed: 404 model not found

// after
const memory = new Memory({
  llm: {
    provider: 'xai',
    config: { apiKey: process.env.XAI_API_KEY, model: 'grok-4.3' },
  },
});
Defensive patterns

Strategy: try-catch

Validate before calling

async function assertXaiReady(apiKey: string, model: string) {
  const res = await fetch('https://api.x.ai/v1/models', {
    headers: { Authorization: `Bearer ${apiKey}` },
  });
  if (res.status === 401) throw new Error('xAI key invalid');
  const { data } = (await res.json()) as { data: { id: string }[] };
  if (!data.some((m) => m.id === model)) throw new Error(`model '${model}' unavailable`);
}

Type guard

function isXaiLlmError(err: unknown): boolean {
  return err instanceof Error && err.message.startsWith('xAI LLM failed:');
}

Try / catch

try {
  const result = await memory.add(text, opts);
} catch (err) {
  if (!isXaiLlmError(err)) throw err;
  const inner = (err as Error).message.slice('xAI LLM failed:'.length);
  if (/401|403/.test(inner)) throw new Error('xAI auth failed — rotate XAI_API_KEY');
  if (/429/.test(inner)) return backoffRetry(() => memory.add(text, opts), 3);
  throw err;
}

Prevention

When it happens

Trigger: Any Memory operation requiring LLM output (add, update, search filtering) with provider 'xai' while the Grok API returns an error: invalid key (401), nonexistent model in config.llm.config.model (e.g. not a grok model), insufficient credits, rate limits, or network failure to api.x.ai.

Common situations: Defaulting to a model name that xAI renamed or deprecated, using a test key from a different workspace, hitting free-tier rate limits during batch ingestion, or pointing XAI_API_BASE at a proxy that mangles the request.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/b746ba92864974d5. Report an issue: GitHub.