mem0ai/mem0 · error
xAI API key is required
Error message
xAI API key is required
What it means
Thrown by the XAILLM constructor when neither config.apiKey nor the XAI_API_KEY environment variable is present. xAI's Grok API requires authentication, so the constructor fails fast instead of letting every request fail later with a 401. This mirrors the Python SDK's mem0/llms/xai.py behavior.
Source
Thrown at mem0-ts/src/oss/src/llms/xai.ts:18
import { OpenAILLM } from "./openai";
import { LLMConfig, Message } from "../types";
import { LLMResponse } from "./base";
/**
* xAI (Grok) LLM provider.
*
* xAI's Grok API is OpenAI-compatible, so this simply reuses {@link OpenAILLM}
* and overrides the connection defaults — mirroring `mem0/llms/xai.py` in the
* Python SDK. The API key resolves from `config.apiKey` or the `XAI_API_KEY`
* env var, and the base URL from `config.baseURL`, `XAI_API_BASE`, else
* `https://api.x.ai/v1`.
*/
export class XAILLM extends OpenAILLM {
constructor(config: LLMConfig) {
const apiKey = config.apiKey || process.env.XAI_API_KEY;
if (!apiKey) {
throw new Error("xAI API key is required");
}
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) {View on GitHub (pinned to 001c235229)
Solutions
- Export XAI_API_KEY in the environment where Node runs: export XAI_API_KEY=... (or add it to your deployment's env config).
- Or pass the key explicitly: new Memory({ llm: { provider: 'xai', config: { apiKey: process.env.XAI_API_KEY } } }).
- If using .env, ensure dotenv (or the runtime's env loading) runs before the Memory/XAILLM constructor executes.
- Verify with a quick check before constructing: if (!process.env.XAI_API_KEY) throw new Error('XAI_API_KEY missing');
Example fix
// before
const memory = new Memory({
llm: { provider: 'xai', config: {} }, // throws: xAI API key is required
});
// after
const memory = new Memory({
llm: {
provider: 'xai',
config: { apiKey: process.env.XAI_API_KEY }, // or export XAI_API_KEY
},
}); Defensive patterns
Strategy: validation
Validate before calling
const xaiKey = process.env.XAI_API_KEY;
if (!xaiKey) {
throw new Error('XAI_API_KEY is not set — add it to the environment before constructing Memory');
} Type guard
function hasXaiCredentials(config: { apiKey?: string } | undefined): boolean {
return Boolean(config?.apiKey || process.env.XAI_API_KEY);
} Try / catch
try {
memory = new Memory({ llm: { provider: 'xai', config: { apiKey: process.env.XAI_API_KEY } } });
} catch (err) {
if (err instanceof Error && err.message === 'xAI API key is required') {
// fail startup with an operator-actionable message
process.exitCode = 1;
throw new Error('Missing XAI_API_KEY: set it in the deployment environment');
}
throw err;
} Prevention
- Declare XAI_API_KEY in Docker/CI/serverless env specs so it is never silently absent.
- Assert required env vars at process start with a single config-validation module.
- Add a startup self-test that constructs Memory once, so key errors surface at boot, not mid-request.
When it happens
Trigger: Constructing Memory with llm.provider 'xai' (or instantiating XAILLM directly) without passing apiKey in config.llm.config and without XAI_API_KEY exported in the process environment. Throws synchronously at construction time, before any memory operation runs.
Common situations: Env var set in a shell but not in the deployment (Docker/CI/serverless where env must be declared), .env file present but not loaded before import, typo in the variable name (XAI_KEY, X_AI_API_KEY), or assuming the SDK reads a differently-named variable.
Related errors
- xAI LLM failed: ${message}
- Together API key is required
- Anthropic API key is required
- DeepSeek API key is required
- Groq API key is required
AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15).
Data as JSON: /api/errors/0e34ba269c625455.
Report an issue: GitHub.