abhigyanpatwari/GitNexus · error · Error
OpenRouter API key is required but was not provided
Error message
OpenRouter API key is required but was not provided
What it means
Thrown by createChatModel() in the 'openrouter' provider branch when openRouterConfig.apiKey is falsy or whitespace-only. OpenRouter is accessed through a ChatOpenAI wrapper pointed at the OpenRouter base URL; without a key the wrapper would 401 on first call, so this guard fails fast at construction. Note the DEV-mode debug log prints only hasApiKey (boolean), never the key value.
Source
Thrown at gitnexus-web/src/core/llm/agent.ts:257
// This is critical for agentic workflows with tool calls
numCtx: 32768,
});
}
case 'openrouter': {
const openRouterConfig = config as OpenRouterConfig;
// Debug logging
if (import.meta.env.DEV) {
console.log('🌐 OpenRouter config:', {
hasApiKey: !!openRouterConfig.apiKey,
model: openRouterConfig.model,
baseUrl: openRouterConfig.baseUrl,
});
}
if (!openRouterConfig.apiKey || openRouterConfig.apiKey.trim() === '') {
throw new Error('OpenRouter API key is required but was not provided');
}
return new ChatOpenAI({
openAIApiKey: openRouterConfig.apiKey,
apiKey: openRouterConfig.apiKey, // Fallback for some versions
modelName: openRouterConfig.model,
temperature: openRouterConfig.temperature ?? 0.1,
maxTokens: openRouterConfig.maxTokens,
configuration: {
apiKey: openRouterConfig.apiKey, // Ensure client receives it
baseURL: openRouterConfig.baseUrl ?? DEFAULT_OPENROUTER_BASE_URL,
},
streaming: true,
});
}
case 'minimax': {
const minimaxConfig = config as MiniMaxConfig;View on GitHub (pinned to d540b00184)
Solutions
- Obtain a key from openrouter.ai and enter it in the provider settings
- Ensure the key is non-empty and non-whitespace
- After setting it, re-create the agent (createChatModel runs at agent construction, so a stale agent won't pick up a new key)
- If no OpenRouter key is available, switch to a provider that doesn't require one (ollama)
Example fix
// before
createChatModel({ provider: 'openrouter', apiKey: '', model: 'anthropic/claude-3.5-sonnet' }); // throws
// after
createChatModel({ provider: 'openrouter', apiKey: userKey, model: 'anthropic/claude-3.5-sonnet' }); Defensive patterns
Strategy: validation
Validate before calling
function hasOpenRouterKey(cfg: { apiKey?: string }): boolean {
return typeof cfg.apiKey === 'string' && cfg.apiKey.trim().length > 0;
}
if (cfg.provider === 'openrouter' && !hasOpenRouterKey(cfg)) {
throw new Error('Configure an OpenRouter API key (from openrouter.ai)');
} Type guard
function isOpenRouterConfigReady(cfg: unknown): cfg is { provider: 'openrouter'; apiKey: string } {
return typeof cfg === 'object' && cfg !== null
&& (cfg as any).provider === 'openrouter'
&& typeof (cfg as any).apiKey === 'string'
&& (cfg as any).apiKey.trim().length > 0;
} Try / catch
try {
createChatModel({ provider: 'openrouter', apiKey, model });
} catch (e) {
if (e instanceof Error && /OpenRouter API key is required/.test(e.message)) {
promptUserForKey('openrouter');
return;
}
throw e;
} Prevention
- Obtain keys from openrouter.ai — OpenRouter proxies many providers behind one key
- Store keys in the settings UI so they survive reloads; re-create the agent after updating
- Note the DEV debug log prints only hasApiKey (boolean), never the key — safe to leave in dev
- If no key, switch to ollama (local, no key) or another provider
When it happens
Trigger: Calling createChatModel({ provider: 'openrouter', apiKey: undefined | '' | ' ', ... }). The check is !apiKey || apiKey.trim() === ''.
Common situations: The OpenRouter key wasn't entered in settings; the env var backing it is unset in the deployed environment; switching provider to openrouter without provisioning a key from openrouter.ai.
Related errors
- OpenAI API key is required but was not provided
- MiniMax API key is required but was not provided
- GLM API key is required but was not provided
- DeepSeek API key is required but was not provided
- Unsupported provider: ${(config as any).provider}
AI-assisted analysis of abhigyanpatwari/GitNexus@d540b00184 (2026-08-12).
Data as JSON: /api/errors/ca401c7d99427211.
Report an issue: GitHub.