Skip to content

Commit a3df0bd

Browse files
bugerclaude
andauthored
fix: resolve base URL in createLanguageModel for API gateway support (#565)
createLanguageModel() resolved API keys from env vars but not base URLs. This caused dedup LLM calls to bypass API gateways (GOOGLE_API_URL, etc.) and hit provider endpoints directly, failing with "API key not valid" when using gateway-issued keys. Add resolveBaseUrl() mirroring the env-var resolution already used by ProbeAgent and FallbackManager, and pass it to createProviderInstance(). Fixes #564 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 124a819 commit a3df0bd

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

npm/src/utils/provider.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,32 @@ export function resolveApiKey(providerName) {
8484
}
8585
}
8686

87+
/**
88+
* Resolve base URL for a provider from environment variables.
89+
* Mirrors the env-var resolution in ProbeAgent and FallbackManager so that
90+
* lightweight LLM calls (dedup, etc.) route through the same API gateway.
91+
* @param {string} providerName
92+
* @returns {string|undefined}
93+
*/
94+
export function resolveBaseUrl(providerName) {
95+
const llmBaseUrl = process.env.LLM_BASE_URL;
96+
switch (providerName) {
97+
case 'anthropic':
98+
return process.env.ANTHROPIC_API_URL || process.env.ANTHROPIC_BASE_URL || llmBaseUrl;
99+
case 'openai':
100+
return process.env.OPENAI_API_URL || llmBaseUrl;
101+
case 'google':
102+
return process.env.GOOGLE_API_URL || llmBaseUrl;
103+
case 'bedrock':
104+
return process.env.AWS_BEDROCK_BASE_URL || llmBaseUrl;
105+
default:
106+
return llmBaseUrl;
107+
}
108+
}
109+
87110
/**
88111
* Create a language model instance from provider name + model name.
89-
* Resolves API keys from environment automatically.
112+
* Resolves API keys and base URLs from environment automatically.
90113
* Returns null on failure (graceful degradation for optional features).
91114
* @param {string} providerName - 'anthropic' | 'openai' | 'google' | 'bedrock'
92115
* @param {string} modelName - Model identifier (e.g., 'gemini-2.0-flash')
@@ -98,7 +121,8 @@ export async function createLanguageModel(providerName, modelName) {
98121
if (!resolvedModel) return null;
99122
try {
100123
const apiKey = resolveApiKey(providerName);
101-
const provider = createProviderInstance({ provider: providerName, ...(apiKey ? { apiKey } : {}) });
124+
const baseURL = resolveBaseUrl(providerName);
125+
const provider = createProviderInstance({ provider: providerName, ...(apiKey ? { apiKey } : {}), ...(baseURL ? { baseURL } : {}) });
102126
return provider(resolvedModel);
103127
} catch {
104128
return null;

0 commit comments

Comments
 (0)