Giới thiệu
litelm tách phần routing và chuyển đổi message của LiteLLM thành một thư viện khoảng 2.900 dòng, chỉ phụ thuộc openai và httpx. Mục tiêu là giữ đường gọi model qua nhiều provider mà không mang theo proxy server, cache, theo dõi chi phí và những lớp tính năng khác của LiteLLM.
Dự án đang ở trạng thái alpha. Tuyên bố compatibility chỉ áp dụng cho bề mặt routing, format và DSPy được ghi rõ; đây không phải bản thay thế toàn bộ LiteLLM.
Tính năng chính
Những phần được giữ lại gồm:
- Routing bằng chuỗi
provider/modeltới endpoint tương ứng. - Chuyển đổi message cho Anthropic, Bedrock, Cloudflare và Mistral.
- Streaming và
stream_chunk_builder. - Tool use, hay function calling.
- Embedding và text completion.
- OpenAI Responses API.
- Mock response phục vụ test.
- Biến thể async cho các hàm chính.
Những phần chủ động bị loại bỏ gồm:
Routercho load balancing và fallback.- Proxy server.
- Cache, budget, theo dõi chi phí và đếm token.
- Image generation, audio, OCR và fine-tuning.
- Agent, guardrail và scheduler.
Repo liệt kê route cho 19 provider. OpenAI, Anthropic, Groq, Mistral, xAI, OpenRouter và Azure được đánh dấu đã verify. Bedrock, Cloudflare, Together, Fireworks, DeepSeek, Perplexity, DeepInfra, Gemini, Cohere, Ollama, vLLM và LM Studio chưa được verify. Endpoint tương thích OpenAI có thể được truyền bằng api_base.
Cài đặt
Gói cơ bản cài openai và httpx. SDK riêng cho Anthropic hoặc Bedrock chỉ được thêm khi chọn extra tương ứng.
pip install litelm
pip install 'litelm[anthropic]'
pip install 'litelm[bedrock]'
pip install 'litelm[all]'
Cách sử dụng
API cố gắng giữ tên hàm, argument và kiểu response giống LiteLLM. Nếu code hiện tại chỉ dùng đường completion cơ bản, thay đổi import có thể nhỏ.
import litelm
response = litelm.completion(
"openai/gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
Streaming dùng cùng hàm với stream=True:
for chunk in litelm.completion(
"groq/llama-3.1-70b-versatile",
messages=[...],
stream=True,
):
print(chunk.choices[0].delta.content or "", end="")
Embedding cũng có interface trực tiếp:
response = litelm.embedding(
"openai/text-embedding-3-small",
input=["hello world"],
)
Các hàm completion, embedding, responses và text_completion có biến thể async tương ứng là acompletion, aembedding, aresponses và atext_completion.
API key và model local
Thư viện đọc API key qua biến môi trường của từng provider, hoặc nhận trực tiếp api_key và api_base khi gọi hàm.
response = litelm.completion(
"openai/gpt-4o",
messages=messages,
api_key="...",
api_base="https://example.com/v1",
)
Với server local tương thích OpenAI, truyền endpoint cho vLLM, Ollama hoặc LM Studio:
response = litelm.completion(
"openai/my-model",
messages=[...],
api_base="http://localhost:8000/v1",
)
Tool calling và xử lý lỗi
Tool được truyền theo schema function calling quen thuộc:
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
},
},
}]
response = litelm.completion(
"openai/gpt-4o",
messages=[{"role": "user", "content": "Weather in Paris?"}],
tools=tools,
tool_choice="required",
)
Lỗi từ provider được chuyển về hierarchy riêng, gồm ContextWindowExceededError, RateLimitError và AuthenticationError:
from litelm import AuthenticationError, ContextWindowExceededError, RateLimitError
try:
response = litelm.completion("openai/gpt-4o", messages=messages)
except ContextWindowExceededError:
# Rút ngắn prompt rồi thử lại.
pass
except RateLimitError:
# Backoff trước khi retry.
pass
except AuthenticationError:
# Kiểm tra lại API key.
pass
Phạm vi kiểm thử và giới hạn
Maintainer cho biết code được con người định hướng và AI hỗ trợ. Từ ngày 14/5/2026, nhiều phần được viết qua Pi dùng GPT-5.5; các tuyên bố compatibility dựa trên test và review của maintainer, không dựa trên việc code do AI hay người viết.
Theo attestation ngày 11/9/2026, maintainer đã rà 360 commit trong đường routing và formatting của LiteLLM, kiểm tra các upstream test liên quan và sửa khoảng trống theo hướng test-first. Repo báo 262 test riêng pass, 55 skip; 45 live test provider và 10 DSPy smoke test cũng pass với dependency lock hiện tại. Baseline test được port từ LiteLLM có 75 test pass và không còn lỗi assertion hoặc runtime được xem là có thể xử lý trong phạm vi công bố.
Những con số đó vẫn không biến alpha thành drop-in replacement cho mọi workload. Dev nên lập inventory các phần LiteLLM đang dùng, đặc biệt proxy, fallback, cache, budget và cost tracking; chỉ thử thay khi ứng dụng thực sự cần phần call path hẹp. Sau đó benchmark latency, memory, hành vi lỗi và từng provider trên workload của chính mình, nhất là các provider chưa được verify.