rohitg00/agentmemory · error · Error
API key is required (via constructor, OPENAI_EMBEDDING_API_K
Error message
API key is required (via constructor, OPENAI_EMBEDDING_API_KEY, or OPENAI_API_KEY)
What it means
OpenAIEmbeddingProvider requires an API key and resolves it from the constructor argument, then OPENAI_EMBEDDING_API_KEY, then OPENAI_API_KEY. If all three are empty it throws immediately in the constructor so misconfiguration fails fast rather than at first network call.
Source
Thrown at src/providers/embedding/openai.ts:70
readonly name = "openai";
readonly dimensions: number;
private apiKey: string;
private baseUrl: string;
private model: string;
private isAzure: boolean;
private azureApiVersion: string;
constructor(apiKey?: string) {
// Separate API key path: caller-passed wins, then OPENAI_EMBEDDING_API_KEY,
// then fall back to OPENAI_API_KEY. Allows e.g. a placeholder key for
// local endpoints that ignore Authorization (most do).
this.apiKey =
apiKey ||
getEnvVar("OPENAI_EMBEDDING_API_KEY") ||
getEnvVar("OPENAI_API_KEY") ||
"";
if (!this.apiKey) {
throw new Error(
"API key is required (via constructor, OPENAI_EMBEDDING_API_KEY, or OPENAI_API_KEY)",
);
}
// Embedding-specific base URL override; falls back to OPENAI_BASE_URL,
// then normalizeBaseUrl's default. The chat-LLM path (src/providers/openai.ts)
// still reads only OPENAI_BASE_URL, so setting OPENAI_EMBEDDING_BASE_URL
// alone moves embeddings to the new endpoint without affecting chat.
this.baseUrl = normalizeBaseUrl(
getEnvVar("OPENAI_EMBEDDING_BASE_URL") || getEnvVar("OPENAI_BASE_URL"),
);
this.model = getEnvVar("OPENAI_EMBEDDING_MODEL") || DEFAULT_MODEL;
this.dimensions = resolveDimensions(
this.model,
getEnvVar("OPENAI_EMBEDDING_DIMENSIONS"),
"OPENAI_EMBEDDING_DIMENSIONS",
);
this.isAzure = detectAzure(this.baseUrl);
this.azureApiVersion =View on GitHub (pinned to e04ba88819)
Solutions
- Export OPENAI_API_KEY (or OPENAI_EMBEDDING_API_KEY) in your environment, or add it to ~/.agentmemory/.env
- Pass the key explicitly: new OpenAIEmbeddingProvider(process.env.MY_KEY)
- Verify the .env file is actually loaded (correct path ~/.agentmemory/.env, correct loader) and the variable name is spelled exactly
- If using a keyless local gateway, still set a dummy key plus OPENAI_EMBEDDING_BASE_URL pointing at the gateway
Example fix
// before const provider = new OpenAIEmbeddingProvider(); // no key anywhere // after // ~/.agentmemory/.env: OPENAI_API_KEY=sk-... const provider = new OpenAIEmbeddingProvider(); // or new OpenAIEmbeddingProvider(process.env.OPENAI_API_KEY)
Defensive patterns
Strategy: validation
Validate before calling
function requireOpenAiKey(): string {
const key = process.env.OPENAI_EMBEDDING_API_KEY || process.env.OPENAI_API_KEY;
if (!key) throw new Error('Set OPENAI_API_KEY (or OPENAI_EMBEDDING_API_KEY) before creating the provider');
return key;
}
const provider = new OpenAIEmbeddingProvider(requireOpenAiKey()); Type guard
const hasApiKey = (k: string | undefined): k is string => typeof k === 'string' && k.trim().length > 0;
Try / catch
let provider: OpenAIEmbeddingProvider;
try {
provider = new OpenAIEmbeddingProvider();
} catch (err) {
if (err instanceof Error && err.message.startsWith('API key is required')) {
console.error('Missing OpenAI credentials: set OPENAI_API_KEY in env or ~/.agentmemory/.env');
process.exit(1);
}
throw err;
} Prevention
- Provision secrets before service start (entrypoint check for required env vars)
- Keep keys in ~/.agentmemory/.env with consistent naming across environments
- Validate all required env vars at boot with a fail-fast check
- Never rely on a shell variable that isn't exported to child processes
When it happens
Trigger: Constructing `new OpenAIEmbeddingProvider()` (or via createEmbeddingProvider/createProvider factory) with no apiKey argument while neither OPENAI_EMBEDDING_API_KEY nor OPENAI_API_KEY is set in the environment or ~/.agentmemory/.env.
Common situations: Docker container launched without passing env vars; .env file located outside the expected ~/.agentmemory/ path; key set only for the chat provider file scope but not exported in the shell; typo like OPENAI_APIKEY; using the npm package in a fresh machine where credentials were never provisioned.
Understand the failure class
Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.
Related errors
- COHERE_API_KEY is required
- GEMINI_API_KEY is required
- OPENROUTER_API_KEY is required
- VOYAGE_API_KEY is required
- GEMINI_API_KEY (or GOOGLE_API_KEY) is required for the gemin
AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30).
Data as JSON: /api/errors/47bfe41ea8c6835b.
Report an issue: GitHub.