Principle:24mlight A Share investment Agent Unified LLM Gateway
| Knowledge Sources | |
|---|---|
| Domains | LLM_Infrastructure, API_Design |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
A unified gateway pattern that abstracts multiple LLM providers (Gemini, OpenAI-compatible APIs) behind a single get_chat_completion interface with automatic client detection, exponential backoff retry, and OpenAI-format message compatibility.
Description
A unified LLM gateway solves the problem of provider lock-in and configuration complexity by providing a single entry point for all LLM calls. Key features:
- Auto-Detection: A
LLMClientFactorydetermines the appropriate client (Gemini or OpenAI-compatible) based on environment variables and parameters. Theclient_type="auto"mode automatically selects the best available provider.
- OpenAI Message Format: All messages use the standard OpenAI format (
[{"role": "system", "content": "..."}, {"role": "user", "content": "..."}]), regardless of the underlying provider.
- Exponential Backoff Retry: Uses the
backofflibrary with exponential retry on exceptions, with a maximum of 5 retries and 300 seconds max wait time.
- Geographic Error Handling: Detects Gemini API geo-restriction errors and provides user-friendly messages suggesting VPN usage.
Usage
Use this principle when:
- Building systems that need to switch between LLM providers without changing call sites
- Requiring retry logic for flaky LLM API endpoints
- Supporting multiple LLM backends (Gemini, OpenAI, self-hosted) through a unified interface
Theoretical Basis
The gateway pattern:
# Abstract pattern (not real implementation)
def get_chat_completion(messages, model=None, client_type="auto"):
client = LLMClientFactory.create_client(
client_type=client_type, model=model
)
return client.get_completion(
messages=messages,
max_retries=3,
initial_retry_delay=1
)
Factory pattern:
client_type="gemini"→ Google Gemini clientclient_type="openai_compatible"→ OpenAI-compatible APIclient_type="auto"→ Auto-detect from environment variables
Message format (OpenAI-compatible):
messages = [
{"role": "system", "content": "You are a financial analyst..."},
{"role": "user", "content": "Analyze this stock..."}
]
Related Pages
Implemented By
- Implementation:24mlight_A_Share_investment_Agent_get_chat_completion
- Implementation:24mlight_A_Share_investment_Agent_agent_endpoint