rohitg00/agentmemory · error · Error
OPENAI_API_KEY is required for the openai provider
Error message
OPENAI_API_KEY is required for the openai provider
What it means
createBaseProvider throws this when the provider config selects the 'openai' provider but no OPENAI_API_KEY environment variable is set. The key is read at provider-construction time via getEnvVar, before any network call, so construction fails fast. It is a deliberate config validation guard, not a runtime/auth failure.
Source
Thrown at src/providers/index.ts:133
}
return new OpenRouterProvider(
geminiKey,
config.model,
config.maxTokens,
"https://generativelanguage.googleapis.com/v1beta/openai/chat/completions",
);
}
case "openrouter":
return new OpenRouterProvider(
requireEnvVar("OPENROUTER_API_KEY"),
config.model,
config.maxTokens,
"https://openrouter.ai/api/v1/chat/completions",
);
case "openai": {
const openaiKey = getEnvVar("OPENAI_API_KEY");
if (!openaiKey) {
throw new Error(
"OPENAI_API_KEY is required for the openai provider",
);
}
return new OpenAIProvider(
openaiKey,
config.model,
config.maxTokens,
config.baseURL,
);
}
case "noop":
return new NoopProvider();
case "agent-sdk":
default:
return new AgentSDKProvider();
}
}
View on GitHub (pinned to e04ba88819)
Solutions
- Export OPENAI_API_KEY in the shell before starting the process: export OPENAI_API_KEY=sk-...
- If the key lives in a .env file, ensure it is loaded (dotenv/config import or equivalent) and that the variable name is spelled OPENAI_API_KEY
- If openai was chosen by mistake, set config.provider to the provider you actually have a key for (e.g. 'openrouter' with OPENROUTER_API_KEY)
- Set the key in your CI/CD secret manager so the deployment environment has it
Example fix
// before
createProvider({ provider: 'openai', model: 'gpt-4o-mini' }); // throws: OPENAI_API_KEY is required
// after
if (!process.env.OPENAI_API_KEY) throw new Error('Set OPENAI_API_KEY first');
createProvider({ provider: 'openai', model: 'gpt-4o-mini', apiKeyEnv: 'OPENAI_API_KEY' }); Defensive patterns
Strategy: validation
Validate before calling
if (!process.env.OPENAI_API_KEY) {
throw new Error('OPENAI_API_KEY must be set before using the openai provider');
} Type guard
function hasOpenAiKey(cfg: { provider: string }): boolean {
return cfg.provider !== 'openai' || !!process.env.OPENAI_API_KEY;
} Try / catch
try {
const provider = createProvider(config);
} catch (e) {
if ((e as Error).message.includes('OPENAI_API_KEY')) {
console.error('Missing OPENAI_API_KEY; check .env loading and var name');
process.exit(1);
}
throw e;
} Prevention
- Load .env at process entry (import 'dotenv/config') and validate required vars on startup
- Keep a single provider-factory call site that pre-checks the key for the chosen provider
- Document required env vars per provider in README/.env.example
- Fail fast in CI with an env sanity-check script before running jobs
When it happens
Trigger: createProvider({ provider: 'openai', ... }) (or config defaulting to openai) with process.env.OPENAI_API_KEY unset or empty string; also triggered indirectly via createFallbackProvider or the providers() helper when the openai entry lacks a key.
Common situations: Fresh clone where the key exists only in a local .env file that is not loaded (dotenv not called or wrong path); CI environments that omit the secret; switching providers to 'openai' after previously using openrouter/minimax and forgetting the new env var; typo like OPEN_AI_API_KEY or OPENAI_APIKEY.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- AGENTMEMORY_VIEWER_HOST=${host} requires AGENTMEMORY_SECRET
- AGENTMEMORY_VIEWER_HOST=${host} requires VIEWER_ALLOWED_HOST
- ${envName} must be a positive integer, got: ${override}
- API key is required (via constructor, OPENAI_EMBEDDING_API_K
- OpenAI embedding failed (${response.status}): ${err}
AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30).
Data as JSON: /api/errors/aa7139fddc1709b9.
Report an issue: GitHub.