Summary: LiteLLM is an open-source Python SDK and self-hosted LLM gateway that lets you call 100+ providers — OpenAI, Anthropic, Bedrock, Vertex, Ollama and more — through one OpenAI-compatible completion() API or a local proxy on port 4000. This how-to walks through SDK install, a first Ollama call, a config-based proxy, and how LiteLLM's free self-hosted gateway compares with Portkey's free Developer plan, using only facts checked on official pages on 2026-11-07. Official screenshots are at the end of this post.
Why a single LLM interface matters
Most teams do not stay on one provider. You might draft with a local Ollama model, fall back to Claude for hard tasks, and keep an Azure OpenAI deployment for compliance. Each vendor ships a different client, error shape and auth header. LiteLLM's docs describe the project as a unified interface over 100+ LLMs in OpenAI format, with a self-hosted Proxy (LLM Gateway) that adds virtual keys, cost tracking and an admin UI.
There are two ways to use it:
- Python SDK — import
completionin your app. Good when one service owns the keys. - Proxy server — run an OpenAI-compatible gateway (default
http://0.0.0.0:4000) so any OpenAI client, LangChain, or curl script talks to one base URL while LiteLLM routes to the real provider.
If you already run local models and hit pull or GPU issues, fix the runner first with our Ollama troubleshooting guide, then put LiteLLM in front of a healthy localhost:11434.
Step 1: Install the SDK (or the leaner core package)
Official Getting Started recommends:
uv add litellm
For apps that only need the Python SDK, the same docs describe litellm-core: fewer mandatory dependencies, no bundled dashboard or gateway CLI entry points, and the same import litellm surface. Do not install litellm and litellm-core together — they share the same Python files.
To run the full AI Gateway with Admin UI, the docs point to the container Quickstart (no local Python required) or the CLI Quickstart below. There is also a Claude Code skill path: npx skills add BerriAI/litellm -g then /litellm-setup, which asks for providers, writes config and starts a test chat — useful if you want the gateway scaffolded for you.
Step 2: Make your first completion (cloud or Ollama)
Every provider uses the same shape. From the official Getting Started examples (model IDs on that page change as vendors rename models — copy the current string from the docs when you try this):
from litellm import completion
import os
os.environ["OPENAI_API_KEY"] = "your-api-key"
response = completion(
model="openai/<current-openai-model-id>",
messages=[{"role": "user", "content": "Hello, how are you?"}]
)
print(response.choices[0].message.content)
Swap the prefix for another vendor the same way: anthropic/..., vertex_ai/..., bedrock/..., azure/<deployment>. Responses stay in OpenAI Chat Completions format. Streaming is stream=True and iterating chunks; provider errors map to OpenAI-style exceptions such as litellm.AuthenticationError, RateLimitError and APIError.
Call a local Ollama model
With Ollama already running, the provider docs show:
from litellm import completion
response = completion(
model="ollama/llama2",
messages=[{"content": "respond in 20 words. who are you?", "role": "user"}],
api_base="http://localhost:11434"
)
print(response)
LiteLLM recommends the ollama_chat/... prefix when you want Ollama's POST /api/chat path for better chat responses. JSON mode uses format="json". Tool calling passes a standard tools=[...] list; if a local model lacks native tools, LiteLLM can fall back to JSON-mode tool calls. For the proxy, a minimal Ollama entry looks like:
model_list:
- model_name: "llama3.1"
litellm_params:
model: "ollama_chat/llama3.1"
api_base: "http://localhost:11434"
keep_alive: "8m"
Step 3: Run the LiteLLM proxy (LLM Gateway)
CLI Quick Start install (note the Python floor):
uv tool install 'litellm[proxy]'
Docs warn that LiteLLM 1.84.0+ needs Python 3.10+. A bare pip install 'litellm[proxy]' on an older interpreter can silently resolve to an older release (they cite 1.83.9 for the previous floor) with no error — check python --version or use uv.
Start a one-model proxy:
litellm --model ollama/llama3
# Proxy running on http://0.0.0.0:4000
Or use a YAML model list and litellm --config your_config.yaml. Debug with --detailed_debug or export LITELLM_LOG=DEBUG (must be a real logging level — setting it to None breaks import litellm).
Call it with any OpenAI client:
import openai
client = openai.OpenAI(api_key="anything", base_url="http://0.0.0.0:4000")
response = client.chat.completions.create(
model="llama3",
messages=[{"role": "user", "content": "Write a short poem"}]
)
print(response.choices[0].message.content)
When no master key is set, the docs allow a placeholder API key. Proxy endpoints include /chat/completions, /completions, /embeddings, /models and /key/generate. Virtual keys, spend tracking and the Admin UI need a database_url under general_settings in config — the CLI-only start on this page runs without a database. Load tests in the same docs say the proxy can handle 1.5k+ requests/second.
Step 4: Fallbacks, cost callbacks and observability
SDK Router support (retry / fallback across deployments) is listed on Getting Started. For cost, attach a success callback that reads kwargs.get("response_cost", 0). Observability hooks include Langfuse, MLflow and Helicone via litellm.success_callback = ["langfuse", ...]. If you already use Langfuse for traces, our Langfuse playbook covers finding expensive or bad answers after the gateway logs them.
LiteLLM vs Portkey vs raw multi-SDK
Figures below were checked on 2026-11-07 from litellm.ai/pricing and portkey.ai/pricing. Enterprise quotes are custom on both sides — we do not invent dollar amounts that are not on the public cards.
| LiteLLM Open Source | LiteLLM Enterprise | Portkey Developer (hosted) | |
|---|---|---|---|
| List price | $0 forever to self-host (no license fee) | Annual, sized to gateway request capacity / architecture / support — not per token (talk to sales). 30-day Enterprise trial key available | $0 / month Free Forever |
| What free covers | 100+ providers, virtual keys, users & teams, spend tracking, budgets + rate limits, fallbacks, request/response logging, Prometheus metrics | Everything in OSS plus SSO/SCIM, OIDC/JWT, audit logs, secret managers + key rotation, org/team admins, multi-region control plane, SLAs / dedicated support, air-gap options | 10k recorded logs / month (requests beyond that still run; extra logs are not recorded). 3-day log / 30-day metrics retention. Page labels it for prototyping — not production |
| Where it runs | Your infra (Docker / Helm / CLI); data and keys stay on your side per pricing FAQ | Self-hosted; air-gapped options listed on Enterprise cards | Hosted SaaS Developer tier; Portkey also lists a separate open-source self-host path and a Production plan at $49/month for 100k logs (+$9 per extra 100k on the public table) |
| Best when | You want a MIT-licensed gateway you operate yourself, including local Ollama behind one OpenAI URL | You need SSO, audit logs and vendor SLAs on the same gateway core | You want a hosted free tier with logs/prompts for POCs without running Postgres yourself |
Raw multi-SDK (one client per vendor) stays free of a gateway process but reintroduces per-provider auth, retries and error handling — the problem LiteLLM's unified exceptions and proxy are meant to remove.
Editor's note
Who it's for: developers who already juggle two or more LLM backends, or who want OpenAI-compatible clients to hit local Ollama without rewriting callers. Platform teams that need virtual keys and budgets on a self-hosted box.
Who it's not for: someone who only ever calls one hosted chat product in a browser and never writes API code. A consumer Copilot or ChatGPT subscription is simpler for that case.
Gotchas verified from official docs (2026-11-07):
- Python 3.10+ for LiteLLM 1.84.0+; old pip can silently pin an older wheel.
- Prefer
ollama_chat/when chat quality matters; plainollama/hits a different Ollama API path. - Proxy without
database_urlhas no Admin UI / virtual keys — add Postgres for those features. - Placeholder API keys only work when no master key is configured; turn keys on before exposing the proxy.
LITELLM_LOG=Nonebreaks imports — use DEBUG/INFO/ERROR.- Do not stack
litellm+litellm-corein one environment. - Enterprise is not a public $/1M-token sticker — capacity-based quotes only on the pricing page.
FAQ
Is LiteLLM really free in production?
Yes for the open-source gateway: the pricing FAQ states you can self-host OSS with no license fee forever. You still pay your own GPUs, cloud LLMs and ops. Enterprise is optional governance and support on the same core.
Can I put Ollama and OpenAI behind one base_url?
Yes. List both under model_list with different model_name aliases, start the proxy with that config, and point the OpenAI SDK at http://0.0.0.0:4000. Callers pick the alias; LiteLLM holds the real provider credentials.
When should I choose Portkey's free plan instead?
When you want a hosted free Developer tier with recorded logs and do not want to run the gateway yourself. Portkey's own pricing page marks that tier as not suitable for production and caps recorded logs at 10k/month. If you need unlimited self-hosted routing with local models, LiteLLM OSS fits better.
Related posts on lightmind24
- Ollama Not Working? Fixes for Pull, GPU and API — get the local runner healthy before you proxy it.
- Together AI Pricing: Credits, Rates and Limits — another OpenAI-compatible backend you can put behind LiteLLM.
- Langfuse Playbook: Find Costly and Bad LLM Answers — observe spend after the gateway callback fires.
Install the SDK or proxy once, keep every caller on the OpenAI shape, and add Ollama or a second cloud vendor as another model alias — that is the whole LiteLLM habit.
Last verified: 2026-11-07
Sources: LiteLLM Getting Started, Proxy CLI Quick Start, Ollama provider docs, litellm.ai, LiteLLM pricing, LiteLLM Enterprise, Portkey pricing, BerriAI/litellm on GitHub.
Image credit: screenshots of public LiteLLM docs (Getting Started, Proxy CLI Quick Start), litellm.ai/pricing, and portkey.ai/pricing captured 2026-11-07. See the official screenshots at the end of this post. Includes no AI-generated image.
댓글
댓글 쓰기