LiteLLM Proxy
LiteLLM Proxy (LLM Gateway)
LiteLLM Proxy는 통합 API로 여러 LLM 제공자와 상호작용할 수 있게 해주는 OpenAI 호환 게이트웨이예요. 모델 이름 앞에 litellm_proxy/ 접두사를 사용하면 요청을 proxy를 통해 라우팅할 수 있어요.
출처: 문서
본문
개요 (Overview)
| 속성 | 설명 |
|---|---|
| 설명 | 통합 API로 여러 LLM 제공자와 상호작용할 수 있게 해주는 OpenAI 호환 게이트웨이 |
| LiteLLM 라우트 | litellm_proxy/ (모델 이름에 이 접두사를 추가해 모든 요청을 litellm_proxy로 라우팅 — 예: litellm_proxy/your-model-name) |
| 설정 | LiteLLM Gateway |
| 지원 엔드포인트 | /chat/completions, /completions, /embeddings, /audio/speech, /audio/transcriptions, /images, /images/edits, /rerank |
필수 변수
os.environ["LITELLM_PROXY_API_KEY"] = "" # "sk-<your-litellm-api-key>" your litellm proxy api key
os.environ["LITELLM_PROXY_API_BASE"] = "" # "http://localhost:4000" your litellm proxy api base
사용법 - 비스트리밍 (Non Streaming)
import os
import litellm
from litellm import completion
os.environ["LITELLM_PROXY_API_KEY"] = ""
# set custom api base to your proxy
# either set .env or litellm.api_base
# os.environ["LITELLM_PROXY_API_BASE"] = ""
litellm.api_base = "your-openai-proxy-url"
messages = [{ "content": "Hello, how are you?","role": "user"}]
# litellm proxy call
response = completion(model="litellm_proxy/your-model-name", messages=messages)
사용법 - 요청별 api_base, api_key 전달
api_base를 동적으로 설정해야 하면 completions에 직접 전달할 수 있어요.
import os
import litellm
from litellm import completion
os.environ["LITELLM_PROXY_API_KEY"] = ""
messages = [{ "content": "Hello, how are you?","role": "user"}]
# litellm proxy call
response = completion(
model="litellm_proxy/your-model-name",
messages=messages,
api_base = "your-litellm-proxy-url",
api_key = "your-litellm-proxy-api-key"
)
사용법 - 스트리밍 (Streaming)
import os
import litellm
from litellm import completion
os.environ["LITELLM_PROXY_API_KEY"] = ""
messages = [{ "content": "Hello, how are you?","role": "user"}]
# openai call
response = completion(
model="litellm_proxy/your-model-name",
messages=messages,
api_base = "your-litellm-proxy-url",
stream=True
)
for chunk in response:
print(chunk)
Embeddings
import litellm
response = litellm.embedding(
model="litellm_proxy/your-embedding-model",
input="Hello world",
api_base="your-litellm-proxy-url",
api_key="your-litellm-proxy-api-key"
)
Image Generation
import litellm
response = litellm.image_generation(
model="litellm_proxy/dall-e-3",
prompt="A beautiful sunset over mountains",
api_base="your-litellm-proxy-url",
api_key="your-litellm-proxy-api-key"
)
Image Edit
import litellm
with open("your-image.png", "rb") as f:
response = litellm.image_edit(
model="litellm_proxy/gpt-image-1",
prompt="Make this image a watercolor painting",
image=[f],
api_base="your-litellm-proxy-url",
api_key="your-litellm-proxy-api-key",
)
Audio Transcription
import litellm
response = litellm.transcription(
model="litellm_proxy/whisper-1",
file="your-audio-file",
api_base="your-litellm-proxy-url",
api_key="your-litellm-proxy-api-key"
)
Text to Speech
import litellm
response = litellm.speech(
model="litellm_proxy/tts-1",
input="Hello world",
api_base="your-litellm-proxy-url",
api_key="your-litellm-proxy-api-key"
)
Rerank
import litellm
response = litellm.rerank(
model="litellm_proxy/rerank-english-v2.0",
query="What is machine learning?",
documents=[
"Machine learning is a field of study in artificial intelligence",
"Biology is the study of living organisms"
],
api_base="your-litellm-proxy-url",
api_key="your-litellm-proxy-api-key"
)
다른 라이브러리와의 통합
LiteLLM Proxy는 Langchain, LlamaIndex, OpenAI JS, Anthropic SDK, Instructor 등과 동작해요.
모든 SDK 요청을 LiteLLM Proxy로 보내기
Requires v1.72.1 or higher.
이미 LiteLLM SDK를 사용하는 라이브러리/코드베이스에서 LiteLLM Proxy를 호출할 때 사용해요. 이 플래그들은 지정된 모델과 무관하게 모든 요청을 LiteLLM proxy를 통해 라우팅해요. 활성화되면 요청은 인증으로 LITELLM_PROXY_API_KEY를 사용하는 LITELLM_PROXY_API_BASE를 사용해요.
옵션 1: 코드에서 전역 설정:
# Set the flag globally for all requests
litellm.use_litellm_proxy = True
response = litellm.completion(
model="vertex_ai/gemini-3.8-flash",
messages=[{"role": "user", "content": "Hello, how are you?"}]
)
옵션 2: 환경 변수로 제어:
# Control proxy usage through environment variable
os.environ["USE_LITELLM_PROXY"] = "True"
response = litellm.completion(
model="vertex_ai/gemini-3.8-flash",
messages=[{"role": "user", "content": "Hello, how are you?"}]
)
옵션 3: 요청별 설정:
# Enable proxy for specific requests only
response = litellm.completion(
model="vertex_ai/gemini-3.8-flash",
messages=[{"role": "user", "content": "Hello, how are you?"}],
use_litellm_proxy=True
)
OAuth2/JWT 인증
LiteLLM Proxy가 OAuth2/JWT 인증(예: Azure AD, Keycloak, Okta)을 요구하면 SDK가 자동으로 토큰을 획득하고 갱신해줘요.
import litellm
from litellm.proxy_auth import AzureADCredential, ProxyAuthHandler
litellm.proxy_auth = ProxyAuthHandler(
credential=AzureADCredential(),
scope="api://my-litellm-proxy/.default"
)
litellm.api_base = "https://my-proxy.example.com"
response = litellm.completion(
model="gpt-5.6-terra",
messages=[{"role": "user", "content": "Hello!"}]
)
LiteLLM Proxy에 태그 보내기
태그를 사용하면 모니터링, 디버깅, 분석 목적으로 API 요청을 분류하고 추적할 수 있어요. extra_body 파라미터를 사용해 문자열 목록으로 태그를 보낼 수 있어요.
import litellm
response = litellm.completion(
model="gpt-5.6-terra",
messages=[{"role": "user", "content": "What is the capital of France?"}],
api_base="http://localhost:4000",
api_key="sk-<your-litellm-api-key>",
extra_body={"tags": ["user:ishaan", "department:engineering", "priority:high"]}
)
비동기:
import litellm
response = await litellm.acompletion(
model="gpt-5.6-terra",
messages=[{"role": "user", "content": "What is the capital of France?"}],
api_base="http://localhost:4000",
api_key="sk-<your-litellm-api-key>",
extra_body={"tags": ["user:ishaan", "department:engineering"]}
)