Designing an LLM Gateway: Multi-Provider Routing and Fallback Strategies
Relying on OpenAI alone for a production LLM service is risky. When OpenAI went down in November 2023, countless services went down with it. An LLM gateway puts multiple providers behind a single interface and centrally manages routing, fallback, and cost optimization.
What an LLM Gateway Does
Client
↓
LLM Gateway (routing / fallback / caching / logging)
↓ ↓ ↓
OpenAI Anthropic Google- Routing: Choose which model to send each request to
- Fallback: Automatically switch to another provider on failure
- Rate limiting: Manage per-provider quotas
- Cost tracking: Unified cost monitoring
Getting Started Quickly with LiteLLM
LiteLLM unifies 100+ LLMs behind an OpenAI-compatible interface.
from litellm import completion
# 모델명만 바꾸면 모든 프로바이더 동일하게 사용
def llm_call(model, messages):
response = completion(model=model, messages=messages)
return response.choices[0].message.content
llm_call("gpt-4o", [{"role": "user", "content": "안녕"}])
llm_call("claude-sonnet-4-6", [{"role": "user", "content": "안녕"}])
llm_call("gemini/gemini-1.5-pro",[{"role": "user", "content": "안녕"}])Implementing a Fallback Chain
from litellm import completion
FALLBACK_CHAIN = [
"gpt-4o",
"claude-sonnet-4-6",
"gemini/gemini-1.5-pro"
]
def resilient_llm_call(messages, preferred_model=None):
chain = ([preferred_model] + FALLBACK_CHAIN) if preferred_model else FALLBACK_CHAIN
chain = list(dict.fromkeys(chain))
last_error = None
for model in chain:
try:
response = completion(model=model, messages=messages, timeout=10, num_retries=1)
return {
"content": response.choices[0].message.content,
"model_used": model,
"was_fallback": model != chain[0]
}
except Exception as e:
last_error = e
print(f"[WARN] {model} 실패, 다음 모델로 시도")
raise RuntimeError(f"모든 모델 실패: {last_error}")Cost-Based Routing
ROUTING_TABLE = {
"low": {"primary": "gpt-4o-mini", "fallback": "claude-haiku-4-5-20251001"},
"medium": {"primary": "claude-sonnet-4-6", "fallback": "gpt-4o"},
"high": {"primary": "claude-opus-4-7", "fallback": "gpt-4o"},
}
def classify_task(messages):
user_text = " ".join(m["content"] for m in messages if m["role"] == "user")
if any(kw in user_text for kw in ["분석", "설계", "아키텍처", "전략"]):
return "high"
if any(kw in user_text for kw in ["번역", "요약", "분류", "추출"]):
return "low"
return "medium"
def smart_route(messages):
complexity = classify_task(messages)
route = ROUTING_TABLE[complexity]
return resilient_llm_call(messages, preferred_model=route["primary"])Deploying a LiteLLM Proxy Server
This lets the entire team share a single endpoint.
# litellm_config.yaml
model_list:
- model_name: gpt-4o
litellm_params:
model: gpt-4o
api_key: os.environ/OPENAI_API_KEY
- model_name: claude-sonnet
litellm_params:
model: claude-sonnet-4-6
api_key: os.environ/ANTHROPIC_API_KEY
router_settings:
routing_strategy: cost-based-routing
fallbacks:
- {"gpt-4o": ["claude-sonnet", "gemini-pro"]}
general_settings:
master_key: sk-my-master-key
database_url: os.environ/DATABASE_URLlitellm --config litellm_config.yaml --port 4000From then on, anyone on the team can use http://localhost:4000 as if it were an OpenAI endpoint. Key management, cost tracking, and fallbacks are all centralized.
Nodelog는 모든 콘텐츠의 내용과 출처를 공개 전에 검토합니다. 환경(OS·버전)에 따라 결과가 달라질 수 있는 기술 정보는 공식 문서와 함께 확인하며, 검토 기준과 정정 원칙은 편집 정책에서 안내합니다. 오류를 발견하시면 이메일로 제보해 주세요 — 확인 후 신속히 정정합니다.
Comments
Be the first to comment.