{"record":{"id":"b746ba92864974d5","repo":"mem0ai/mem0","slug":"xai-llm-failed-message","errorCode":null,"errorMessage":"xAI LLM failed: ${message}","messagePattern":"xAI LLM failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/llms/xai.ts","lineNumber":38,"sourceCode":"    super({\n      ...config,\n      apiKey,\n      baseURL:\n        config.baseURL || process.env.XAI_API_BASE || \"https://api.x.ai/v1\",\n      model: config.model || \"grok-4.3\",\n    });\n  }\n\n  async generateResponse(\n    messages: Message[],\n    responseFormat?: { type: string },\n    tools?: any[],\n  ): Promise<string | LLMResponse> {\n    try {\n      return await super.generateResponse(messages, responseFormat, tools);\n    } catch (err) {\n      const message = err instanceof Error ? err.message : String(err);\n      throw new Error(`xAI LLM failed: ${message}`);\n    }\n  }\n\n  async generateChat(messages: Message[]): Promise<LLMResponse> {\n    try {\n      return await super.generateChat(messages);\n    } catch (err) {\n      const message = err instanceof Error ? err.message : String(err);\n      throw new Error(`xAI LLM failed: ${message}`);\n    }\n  }\n}\n","sourceCodeStart":20,"sourceCodeEnd":51,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/llms/xai.ts#L20-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the appended original message to classify: 401/403 = key, 404 = model, 429 = rate limit, 5xx/network = transient.","Confirm the key works: curl https://api.x.ai/v1/models -H \"Authorization: Bearer $XAI_API_KEY\".","Use a valid current model id (e.g. the configured default 'grok-4.3' or another grok model from the models endpoint).","For 429s, throttle concurrent Memory.add() calls or add retry with backoff.","If overriding XAI_API_BASE, verify the proxy forwards auth headers and paths correctly."],"exampleFix":"// before\nconst memory = new Memory({\n  llm: { provider: 'xai', config: { apiKey: process.env.XAI_API_KEY } },\n});\nawait memory.add('hi', { userId: 'u1' }); // xAI LLM failed: 404 model not found\n\n// after\nconst memory = new Memory({\n  llm: {\n    provider: 'xai',\n    config: { apiKey: process.env.XAI_API_KEY, model: 'grok-4.3' },\n  },\n});","handlingStrategy":"try-catch","validationCode":"async function assertXaiReady(apiKey: string, model: string) {\n  const res = await fetch('https://api.x.ai/v1/models', {\n    headers: { Authorization: `Bearer ${apiKey}` },\n  });\n  if (res.status === 401) throw new Error('xAI key invalid');\n  const { data } = (await res.json()) as { data: { id: string }[] };\n  if (!data.some((m) => m.id === model)) throw new Error(`model '${model}' unavailable`);\n}","typeGuard":"function isXaiLlmError(err: unknown): boolean {\n  return err instanceof Error && err.message.startsWith('xAI LLM failed:');\n}","tryCatchPattern":"try {\n  const result = await memory.add(text, opts);\n} catch (err) {\n  if (!isXaiLlmError(err)) throw err;\n  const inner = (err as Error).message.slice('xAI LLM failed:'.length);\n  if (/401|403/.test(inner)) throw new Error('xAI auth failed — rotate XAI_API_KEY');\n  if (/429/.test(inner)) return backoffRetry(() => memory.add(text, opts), 3);\n  throw err;\n}","preventionTips":["Validate the xAI key and model id against /v1/models during deployment checks.","Treat 401s after key rotation as a standing runbook item: restart long-lived processes.","Cap concurrent Memory.add() calls to stay under Grok rate limits."],"tags":["llm","xai","grok","api-key","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}