LiteLLM 教學:用同一個 Python API 呼叫 OpenAI 和 Anthropic
Call OpenAI and Anthropic from one Python API with LiteLLM
LiteLLM 提供統一的 OpenAI 格式呼叫,讓 Python 程式更改模型字串,即可在 OpenAI、Anthropic 等 100+ 個供應商之間切換。
Every LLM provider has its own SDK, auth pattern and error types. Adding a model usually means more glue code. LiteLLM gives you one OpenAI-format call for 100+ providers, so switching from OpenAI to Anthropic is a one-string change. This is the short setup from the README.
What you get
One completion call in OpenAI format returns a response from whichever provider you name. You change only the model string. You still need an account and API key for each provider, and each provider's own pricing and limits apply.
Step 1: install the SDK
uv add litellm
Step 2: set your provider keys
Set them as environment variables, such as OPENAI_API_KEY and ANTHROPIC_API_KEY. Keep them out of your code and out of version control.
Step 3: make a call
from litellm import completion
messages = [{"role": "user", "content": "Hello!"}]
print(completion(model="openai/gpt-4o", messages=messages))
Step 4: swap the provider
Change the model string and run the same call again:
print(completion(model="anthropic/claude-sonnet-4-20250514", messages=messages))
Same code, same response shape, different provider.
Going further: a shared gateway for a team
If several developers need model access without sharing raw provider keys, LiteLLM also ships a proxy with virtual keys and spend tracking.
uv tool install 'litellm[proxy]'
litellm --model gpt-4o
Point any OpenAI client at http://0.0.0.0:4000 and use a virtual key as the api_key. The README links an end-to-end tutorial for creating virtual keys: https://docs.litellm.ai/docs/proxy/docker_quick_start
One caution: the quick start listens on 0.0.0.0:4000, so add your own network, TLS and access controls before you expose it to a team.
A prompt for your coding agent
"Using the litellm repo, write a short Python script that sends 'Hello!' to openai/gpt-4o and to anthropic/claude-sonnet-4-20250514 through litellm.completion. Print both responses. Verify both API keys are set in the environment before running."
Tips
- Start with two providers you already have keys for, so you can see the swap work.
- Treat model strings as config, not code, so you can change them without a deploy.
- This walkthrough follows the README and has not been run end to end by us.
More
Step-by-step card: Call 100+ LLM providers through one OpenAI-format API. More on the repo: LiteLLM use cases.
Source repo: BerriAI/litellm.
Want the setup commands for 50 AI repos people actually ship with? Get the free AI Repos That Actually Ship pack: https://dedupedai.com/ship-pack/?utm_source=devto&utm_medium=article&utm_campaign=shippack
Get one email every Monday with the week's hottest AI repos and exactly how to use them: subscribe free at dedupedai.com
來源:DEV Community · dev.to