Skip to content

Commit 17b903c

Browse files
committed
fix(llm): respect LLM_PROXY_URL for OpenRouter models endpoint
- Modified get_available_models() to use proxy when LLM_PROXY_URL is configured - Added URL construction to convert /messages proxy URL to /models endpoint - Ensures model listing requests go through proxy for credit tracking - Fixes bypass where model requests went directly to OpenRouter API
1 parent bd8861c commit 17b903c

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

gptme/llm/llm_openai.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,16 +546,30 @@ def get_available_models(provider: Provider) -> list[ModelMeta]:
546546
raise ValueError(f"Provider {provider} does not support listing models")
547547

548548
config = get_config()
549-
api_key = config.get_env_required("OPENROUTER_API_KEY")
550549

551-
headers = {
552-
"Authorization": f"Bearer {api_key}",
553-
}
550+
# Check if we should use the proxy
551+
proxy_key = config.get_env("LLM_PROXY_API_KEY")
552+
proxy_url = config.get_env("LLM_PROXY_URL")
553+
554+
if proxy_key and proxy_url:
555+
# Use proxy for models endpoint
556+
# Strip /messages from proxy URL and replace with /models
557+
api_key = proxy_key
558+
if proxy_url.endswith("/messages"):
559+
base_url = proxy_url.rsplit("/messages", 1)[0]
560+
url = f"{base_url}/models"
561+
else:
562+
# Fallback if URL structure is different
563+
url = f"{proxy_url.rstrip('/')}/models"
564+
headers = {"x-api-key": api_key}
565+
else:
566+
# Direct OpenRouter API call (fallback)
567+
api_key = config.get_env_required("OPENROUTER_API_KEY")
568+
url = "https://openrouter.ai/api/v1/models"
569+
headers = {"Authorization": f"Bearer {api_key}"}
554570

555571
try:
556-
response = requests.get(
557-
"https://openrouter.ai/api/v1/models", headers=headers, timeout=30
558-
)
572+
response = requests.get(url, headers=headers, timeout=30)
559573
response.raise_for_status()
560574
data = response.json()
561575

0 commit comments

Comments
 (0)