logo
Published on
LiteLLM Proxy: Gateway LLM tốt nhất để thử nghiệm — và những gì cần biết trước khi production

LiteLLM Proxy: Gateway LLM tốt nhất để thử nghiệm — và những gì cần biết trước khi production

LiteLLM Proxy là lựa chọn gateway LLM phổ biến nhất hiện tại — dễ dùng, tính năng phong phú, guardrails mạnh. Nhưng trước khi đưa vào production tải lớn, có một số điều quan trọng về performance và stability bạn cần hiểu rõ.

LiteLLM Proxy: Gateway LLM tốt nhất để thử nghiệm — và những gì cần biết trước khi production

LiteLLM Proxy là open-source LLM gateway phổ biến nhất hiện tại — ~95M lượt tải PyPI mỗi tháng, ~40k GitHub stars, MIT license. Nó giải quyết rất tốt bài toán "một endpoint cho mọi LLM provider" và có hệ sinh thái tính năng ấn tượng: virtual keys, budget, guardrails, routing, caching.

Nhưng đây là bức tranh đầy đủ hơn: LiteLLM đang trong giai đoạn chuyển tiếp quan trọng. Team đang port toàn bộ hot path sang Rust để giải quyết một performance ceiling thực sự của Python. Trong khi chờ đó, có những trade-off bạn cần cân nhắc kỹ trước khi đưa vào production với tải lớn.


LiteLLM Proxy là gì?

LiteLLM Proxy là một self-hosted server đứng giữa ứng dụng của bạn và các LLM provider. Mọi request đi qua một endpoint OpenAI-compatible — bất kỳ client nào đang dùng OpenAI SDK đều hoạt động ngay mà không cần sửa code.

# Trước: gọi thẳng OpenAI
client = openai.OpenAI(api_key="sk-...")

# Sau: trỏ vào LiteLLM Proxy — không đổi gì khác
client = openai.OpenAI(
    api_key="sk-virtual-key-123",
    base_url="http://your-litellm-proxy:4000"
)

Phía sau proxy, bạn có thể route request đến OpenAI, Anthropic Claude, Google Gemini, AWS Bedrock, Azure OpenAI, Cohere, HuggingFace, VLLM — 140+ provider, 2,500+ model — mà client không cần biết. OSS với giấy phép MIT, có thêm Enterprise tier từ BerriAI.

# config.yaml — một file để cấu hình toàn bộ
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: os.environ/OPENAI_API_KEY

  - model_name: gpt-4o # cùng alias, khác provider — tự động fallback
    litellm_params:
      model: azure/gpt-4o-prod
      api_base: os.environ/AZURE_API_BASE
      api_key: os.environ/AZURE_API_KEY

  - model_name: claude-sonnet
    litellm_params:
      model: anthropic/claude-sonnet-4-6
      api_key: os.environ/ANTHROPIC_API_KEY

Các tính năng nổi bật

Virtual Keys & Budget

Thay vì phân phát API key thật cho từng developer hoặc team, bạn tạo virtual keys — có thể revoke bất kỳ lúc nào, gắn với budget và rate limit riêng.

curl -X POST http://proxy:4000/key/generate \
  -H "Authorization: Bearer sk-admin-key" \
  -d '{
    "max_budget": 50,
    "budget_duration": "30d",
    "models": ["gpt-4o", "claude-sonnet"],
    "metadata": {"team": "frontend"}
  }'

LiteLLM track chi phí theo nhiều tầng: key → user → team → global. Với multi-replica, cross-pod rate limiting dùng Redis để đảm bảo consistent enforcement.

Multi-tenancy phân ra 3 tầng — nhưng không phải tất cả đều miễn phí:

TầngOSS / EnterpriseMô tả
Organization⚠️ Enterprise onlyCorporate-wide tenant wrapper
Team✅ OSSBudget, TPM/RPM, model allowlist
Virtual Key✅ OSSService-account hoặc user-attributed

Với hầu hết use case (single-org, multi-team), Team + Virtual Key ở OSS tier là đủ.

Routing & Fallbacks

LiteLLM có 6 routing strategies và 3 loại fallback riêng biệt:

StrategyDùng khiLưu ý
simple-shuffle (default)Deployments quota tương đươngKhông tự điều chỉnh theo tải
least-busyRequest duration không đềuCần Redis shared state
latency-based-routingƯu tiên SLA latency⚠️ Không dùng cho spike traffic — dồn tải vào deployment "may mắn nhanh"
usage-based-routing-v2Nhiều deployment cùng model, quota riêngCần khai báo tpm/rpm chính xác
cost-based-routingTối ưu chi phí là số 1Deployment rẻ kém chất lượng sẽ chiếm routing
Tier-based (pattern)Fleet nhiều model khác cấp độCase study: giảm 88% chi phí (Hannecke 2026)

3 loại fallback riêng — không interchangeable:

litellm_settings:
  fallbacks: # generic — kích hoạt với mọi lỗi
    - gpt-4o: ['claude-sonnet', 'gemini-flash']
  context_window_fallbacks: # chỉ khi context quá dài
    - gpt-4o-mini: ['gpt-4o']
  content_policy_fallbacks: # chỉ khi content bị block
    - gpt-4o: ['claude-sonnet']

Caching — 3 tầng

Tier 1 — Exact-match: hash request → Redis. Benchmark: 0.6s → 0.02s (30× nhanh hơn).

Tier 2 — DualCache: L1 in-memory + L2 Redis. Redis hit được promote lên local memory — request tiếp theo trên cùng pod bỏ qua Redis hoàn toàn.

Tier 3 — Semantic cache: embed query → vector similarity. "What's microservices?" và "Explain microservices?" hit cùng cache entry. Chi phí: +50–100ms cho non-hit.

Gotcha: Dùng redis_host/redis_port/redis_password rời nhau — cấu hình bằng redis_url chậm hơn ~80 RPS (bug chưa fix tính đến tháng 6/2026).

Logging & Observability

Mỗi request tạo StandardLoggingPayload (model, tokens, cost, latency, user, team, trace_id) fan-out đến 20+ backends: Langfuse, Datadog, Prometheus, OpenTelemetry (GenAI semantic conventions), S3/GCS, AWS SQS, Arize, Slack, và nhiều hơn nữa. Cấu hình một lần, gửi đến tất cả.

litellm_settings:
  turn_off_message_logging: true # redact message bodies khỏi tất cả callbacks — quan trọng cho compliance

Guardrails — Kiểm soát LLM ở tầng infrastructure

Đây là một trong những điểm mạnh nhất của LiteLLM. Thay vì implement guardrails trong application code, bạn cấu hình chúng tại tầng proxy — áp dụng cho mọi request, mọi team, mọi model mà không cần sửa một dòng app code nào.

Kiến trúc event hook

ModeThời điểmTác dụng
pre_callTrước khi gọi LLMKiểm tra input, block nếu vi phạm
during_callSong song với LLM callInput check, không tăng latency
post_callSau khi LLM trả vềKiểm tra cả input lẫn output
logging_onlySau khi hoàn thànhGhi log, không block
guardrails:
  - guardrail_name: 'pii-masker'
    litellm_params:
      guardrail: presidio
      mode: 'pre_call'
      pii_entities_config:
        EMAIL_ADDRESS: 'MASK'
        CREDIT_CARD: 'MASK'
        US_SSN: 'MASK'

  - guardrail_name: 'prompt-injection-check'
    litellm_params:
      guardrail: lakera_ai
      mode: 'during_call' # song song với LLM, không thêm latency
      api_key: os.environ/LAKERA_API_KEY
      default_on: true

  - guardrail_name: 'output-moderation'
    litellm_params:
      guardrail: openai_moderation
      mode: 'post_call'

Built-in guardrails (zero-latency, không cần external API)

  • Content Filter — regex và keyword matching, block từ/pattern cụ thể
  • Competitor Name Blocker — chặn tên đối thủ khỏi response
  • Topic Blocker — cấm LLM thảo luận về chủ đề nhất định
  • Insults Filter — lọc nội dung xúc phạm
  • Sensitive Data Routing — route request có sensitive data sang on-premise model thay vì block
  • Prompt Injection Detection — in-memory, không cần API ngoài

30+ guardrail providers

LiteLLM tích hợp với hơn 30 provider: Presidio (PII/PHI masking), Lakera AI (prompt injection), Aporia, OpenAI Moderation, Azure Content Safety, AWS Bedrock Guardrails, Microsoft Purview, CrowdStrike AIDR, PANW Prisma AIRS, HiddenLayer, IBM Guardrails, PromptGuard, Qualifire, và nhiều hơn nữa.


Thực trạng performance — điều quan trọng cần biết

LiteLLM được viết bằng Python (FastAPI/Uvicorn). Đây là lý do nó dễ extend và có hệ sinh thái plugin phong phú, nhưng cũng là giới hạn cứng về performance ở tải cao.

Số liệu thực tế (LiteLLM self-reported, June 2026):

Cấu hìnhThroughputOverhead per request
Single instance, 50 concurrent~453 req/s~7.5 ms
Single instance tuned~1,000 req/s (vendor claim)~8 ms P95

Ở mức trên ~500 RPS, P99 latency bắt đầu tăng đột biến. Đây là giới hạn kiến trúc của Python GIL trong workload CPU-bound — không phải vấn đề cấu hình có thể fix hoàn toàn.

Một user thực tế report trên GitHub (issue #21046) với LiteLLM v1.80.15 trên 4 vCPU/8GB, 500 concurrent requests: throughput giảm 1.7–4× so với gọi LLM trực tiếp, dù đã áp dụng đầy đủ best practices (pgbouncer, disabled spend logs, tuned config).

Rust migration — lộ trình 2026

Tháng 6/2026, LiteLLM team công bố migration toàn bộ hot path sang Rust. Target:

Python (hiện tại)Rust (mục tiêu)
Overhead per request~7.5 ms~0.05 ms
Throughput~453 req/s~6,782 req/s
Peak memory~359 MB~32 MB

Milestone:

Thời điểmMilestone
Aug 15, 2026OCR routes → Rust
Sep 1, 2026/v1/messages + streaming → Rust
Sep 15, 2026Router (load balancing, fallbacks, retries) → Rust
Dec 1, 2026Full server → Rust (axim); Python plugins vẫn chạy như sidecar

Migration được thiết kế transparent: cùng config.yaml, cùng database, cùng client API. Không cần migration từ phía user.

Lưu ý: Đây là roadmap, không phải tính năng đã ship. Tính đến tháng 6/2026, toàn bộ proxy vẫn chạy Python. Treat performance targets trên như directional intent.


Vậy LiteLLM phù hợp với ai?

Phù hợp nhất:

  • PoC và thử nghiệm nội bộ — setup nhanh, tính năng đầy đủ để validate architecture
  • Low-to-moderate traffic — tải vừa phải (< vài trăm RPS sustained), chấp nhận ~7.5ms overhead
  • Team muốn guardrails và routing ngay — không cần tự build
  • Internal tools, AI-powered dashboards — không phải latency-critical path

Cần test kỹ trước khi production tải lớn:

  • Nếu traffic sustained > 400–500 RPS, benchmark cụ thể trước — đừng giả định vendor claim 1,000 RPS là thực tế trong môi trường của bạn
  • Với latency-critical workloads (voice AI, real-time), 7.5ms overhead proxy là con số đáng cân nhắc
  • Memory leak tiềm ẩn: dùng --max_requests_before_restart 10000 để bound memory growth
  • Stability bugs được team acknowledge tháng 6/2026 (auth layer, MCP authentication, UI forms) — đang được fix với milestone Aug 2026

Nếu bạn cần production gateway với tải lớn ngay bây giờ, có hai alternative đáng cân nhắc:

  • Bifrost (Go, Apache 2.0, Maxim AI) — LiteLLM SDK-compatible, migrate chỉ cần đổi base_url. Self-reported overhead ~11µs vs ~7.5ms của LiteLLM Python; ~120 MB RAM vs ~359 MB. Lưu ý: benchmark hiện có đều do Maxim AI (vendor) tự công bố, chưa có independent verification — directional advantage của Go over Python là có cơ sở kiến trúc, nhưng số cụ thể nên treat như vendor marketing. Provider coverage hẹp hơn (~20 providers / 1,000+ models vs 140+ / 2,500+).

  • Envoy Gateway với AI Extension (CNCF, Apache 2.0) — không phải LLM-native gateway mà là Envoy proxy được extend để hiểu LLM semantics (token-based rate limiting, semantic caching, prompt guard). Phù hợp nếu team đã có Envoy/Kubernetes infrastructure và muốn LLM policy sống cùng service mesh policy — không cần thêm một control plane mới. Không có virtual key management hay built-in guardrail ecosystem như LiteLLM.


Lưu ý bảo mật

Supply Chain Incident — March 2026: PyPI packages litellm==1.82.71.82.8 bị compromise ~40 phút — payload thu thập env vars, SSH keys, cloud credentials, Kubernetes tokens. Docker image chính thức không bị ảnh hưởng.

Thực hành bắt buộc:

  • Luôn dùng official Docker image (ghcr.io/berriai/litellm) — không pip install litellm trực tiếp trong CI/CD
  • Pin đến signed SemVer tag cụ thể, không dùng :latest
  • Upgrade lên ≥ v1.84.0 để vá CVE-2026-42208 (SQL injection) và GHSA-4xpc-pv4p-pm3w (auth bypass via Host header)
  • Backup LITELLM_SALT_KEY — mất key này đồng nghĩa mất toàn bộ virtual keys, không có cách recovery

Kết

LiteLLM Proxy là điểm khởi đầu tốt nhất hiện tại cho bất kỳ team nào muốn có LLM gateway — dễ setup, tính năng phong phú, hệ sinh thái guardrails lớn nhất trong phân khúc này. Nó đặc biệt phù hợp để thử nghiệm, build internal tools, và validate architecture trước khi đi xa hơn.

Cho production ở tải cao, bức tranh phức tạp hơn: performance ceiling của Python là thực, acknowledged stability issues là thực, nhưng Rust migration roadmap cũng nghiêm túc. Quyết định đúng đắn là benchmark với traffic thực tế của bạn, không đơn thuần dựa vào vendor numbers.


Nguồn: LiteLLM Documentation · Guardrails Quick Start · LiteLLM Rust Migration Blog · LiteLLM June Stability Update

Other articles
More articles ➜