litellmExample
đ Level Up Your LLM Game with Litellm
If youâve ever felt like juggling multiple LLM APIs is a nightmare of authentication keys, rate limits, and versioning headaches, youâre not alone. Enter Litellmâthe open-source gateway that turns a chaotic patchwork of providers into a single, coherent, and ultraâsmooth experience. In this post weâll walk through:
- What Litellm actually is (and why itâs a gameâchanger)
- How to get it up and running in minutes
- Realâworld code examples across popular languages
- Pro tips for scaling, monitoring, and costâoptimizing
Grab a coffee, and letâs dive in.
1. Why Litellm Exists
| Pain Point | Traditional Approach | Litellm Solution |
|---|---|---|
| Multiple API keys | Store a dozen secrets scattered across configs | One unified SDK call that handles auth for you |
| Different response schemas | Write custom parsing for each provider | Litellm normalizes every response into a clean JSON structure |
| Rateâlimit confusion | Manually backâoff per provider | Centralized rateâlimit handling + exponential backâoff |
| Model version drift | Pin each providerâs model name manually | Switch models on the fly without code changes |
In short, Litellm abstracts the mess away, letting you focus on prompt engineering and business logic.
2. QuickâStart: OneâCommand Install
# Install the Python client (or use the Node/Rust wrappers if you prefer)
pip install litellm[openai] # add extra brackets for other providers
Thatâs it! The library ships with adapters for OpenAI, Anthropic, Cohere, Google Gemini, Mistral, Llama, and many more. All you need next is a tiny snippet to test the waters:
import litellm
response = litellm.completion(
model="openai/gpt-4-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Give me a 3âsentence summary of the French Revolution."}
],
max_tokens=150,
)
print(response.choices[0].message.content)
Tip: If youâre not using Python, check out the Litellm GitHub repo for community wrappers in Node.js, Go, and Rust.
3. RealâWorld Example: Building a Chatbot with RetrievalâAugmented Generation
Imagine youâre constructing a customerâsupport bot that pulls facts from an internal FAQ database, then asks a LLM to synthesize an answer. Hereâs a minimal yet productionâready implementation:
import litellm
import pinecone # or any vector store SDK you prefer
# 1ď¸âŁ Retrieve relevant snippets
question = "What are the refund policies for orders shipped internationally?"
context_chunks = pinecone.query(question, top_k=3) # returns list of strings
# 2ď¸âŁ Build a prompt that includes the context
prompt = "\n".join(context_chunks) + "\n\nQ: " + question + "\nA:"
# 3ď¸âŁ Call Litellm
response = litellm.completion(
model="anthropic/claude-3-sonnet-20240515",
messages=[{"role": "user", "content": prompt}],
temperature=0.2,
max_tokens=300,
)
answer = response.choices[0].message.content
print(f"Bot: {answer}")
Why This Rocks
- No provider lockâin â swap
anthropic/claude-3-sonnet-20240515foropenai/gpt-4-turbowith a single line change. - Temperature tuning keeps the bot deterministic for policy questions.
- Context injection happens before the LLM sees the prompt, making the response grounded and trustworthy.
4. ProductionâReady Settings
| Feature | How to Enable | Example |
|---|---|---|
| Structured Logging | Set LITELLME_LOG_LEVEL=DEBUG and install loguru |
import logging; logging.basicConfig(level=logging.INFO) |
| Caching | Wrap calls with litellm.cache decorator |
@litellm.cache(expire_after=3600) |
| Rate Limiting | Use litellm.RateLimitAdapter |
adapter = litellm.RateLimitAdapter(max_rpm=120) |
| Fallbacks | Define a list of models; Litellm picks the first healthy one | model="openai/gpt-3.5-turbo,anthropic/claude-2" |
Sample settings.yaml
logLevel: INFO
cacheDir: "./litellm_cache"
maxRetries: 3
timeout: 30 # seconds
Load it with:
from litellm import set_litellm_settings
set_litellm_settings("settings.yaml")
5. Monitoring & Cost Management
a. Metrics Dashboard
Litellm emits Prometheusâcompatible metrics (litellm_requests_total, litellm_tokens_used, etc.). Push them to Grafana via your favorite exporter.
b. Billing Alerts
from litellm import tracking
tracking.usage_logger = tracking.UsageLogger(
api_key="YOUR_DASHBOARD_TOKEN",
tracked_models=["gpt-4-turbo", "claude-3-sonnet"],
cost_cents_per_token={"gpt-4-turbo": 0.03, "claude-3-sonnet": 0.025},
)
c. BudgetâFriendly Tips
- Chunk large prompts: Split > 3k token inputs into smaller pieces before sending.
- Use âminiâ models for cheap drafts (e.g.,
mixtral-8x7b) and only promote to premium models when confidence is high. - Leverage providerâlevel discounts: Many APIs offer bulk token discounts; Litellm can automatically switch to a cheaper plan once a threshold is reached.
6. Community & Extensions
- Litellm Hub: Contribute adapters, share recipes, or publish custom prompt templates.
- LLMâProxy Mode: Run Litellm as a local API gateway (Docker compose available) for teams that need a sandbox environment.
- Safety Filters: Plug in your own contentâfiltering logic via the
on_responsehookâperfect for complianceâheavy domains.
7. TL;DR â OneâLiner Summary
Litellm = universal LLM client that turns any provider into a single, seamless, productionâready interface.
Give it a spin, and watch your LLM experiments transform from âworks on my machineâ to âscales across the orgâ.
Want More?
- Deep Dive Video: Litellm Architecture Overview (YouTube)
- HandsâOn Repo: litellmâexamples â GitHub
- Slack Community:
#litellmon the LangChain Slack workspace
Happy prompting! đ