使用 LiteLLM 統一管理 OpenClaw 的模型調用
開發 AI 應用時,最煩的就是要處理各種不同的 API 格式和金鑰。當你想要換個模型測試效果,或者想追蹤到底花了多少錢時,往往得在多個後台跳來跳去,這種瑣碎的維護工作真的很消耗精力。
如果你也覺得在不同供應商之間切換很麻煩,LiteLLM 就是你的救星。它是一個開源的 LLM Gateway,能讓你透過統一的 API 存取超過 100 種模型供應商。將 OpenClaw 串接到 LiteLLM 後,你可以集中追蹤成本與日誌,而且不需要更改 OpenClaw 配置就能隨時切換後端。
Why use LiteLLM with OpenClaw?
Section titled “Why use LiteLLM with OpenClaw?”- Cost tracking:精確掌握 OpenClaw 在所有模型上的支出
- Model routing:無需更改配置即可在 Claude, GPT-4, Gemini, Bedrock 之間切換
- Virtual keys:為 OpenClaw 建立帶有支出限制的金鑰
- Logging:完整的請求與回應日誌,方便除錯
- Fallbacks:當主要供應商掛掉時,自動切換到備援方案
需要準備的東西
Section titled “需要準備的東西”- LiteLLM
- OpenClaw
你可以選擇透過引導流程或手動完成設定。
方法一:透過 onboarding
Section titled “方法一:透過 onboarding”openclaw onboard --auth-choice litellm-api-key方法二:手動設定
Section titled “方法二:手動設定”- 啟動 LiteLLM Proxy:
pip install 'litellm[proxy]'litellm --model claude-opus-4-6- 將 OpenClaw 指向 LiteLLM:
export LITELLM_API_KEY="your-litellm-key"
openclaw這樣就完成了,OpenClaw 現在會透過 LiteLLM 進行路由。
Configuration
Section titled “Configuration”Environment variables
Section titled “Environment variables”export LITELLM_API_KEY="sk-litellm-key"Config file
Section titled “Config file”{ models: { providers: { litellm: { baseUrl: "http://localhost:4000", apiKey: "${LITELLM_API_KEY}", api: "openai-completions", models: [ { id: "claude-opus-4-6", name: "Claude Opus 4.6", reasoning: true, input: ["text", "image"], contextWindow: 200000, maxTokens: 64000, }, { id: "gpt-4o", name: "GPT-4o", reasoning: false, input: ["text", "image"], contextWindow: 128000, maxTokens: 8192, }, ], }, }, }, agents: { defaults: { model: { primary: "litellm/claude-opus-4-6" }, }, },}Virtual keys
Section titled “Virtual keys”你可以為 OpenClaw 建立一個專屬金鑰並設定支出上限:
curl -X POST "http://localhost:4000/key/generate" \ -H "Authorization: Bearer $LITELLM_MASTER_KEY" \ -H "Content-Type: application/json" \ -d '{ "key_alias": "openclaw", "max_budget": 50.00, "budget_duration": "monthly" }'接著將產生的金鑰設定為 LITELLM_API_KEY 即可。
Model routing
Section titled “Model routing”LiteLLM 可以將模型請求路由到不同的後端。請在你的 LiteLLM config.yaml 中設定:
model_list: - model_name: claude-opus-4-6 litellm_params: model: claude-opus-4-6 api_key: os.environ/ANTHROPIC_API_KEY
- model_name: gpt-4o litellm_params: model: gpt-4o api_key: os.environ/OPENAI_API_KEYOpenClaw 會持續請求 claude-opus-4-6,而 LiteLLM 會負責處理背後的路由邏輯。
Viewing usage
Section titled “Viewing usage”你可以透過 LiteLLM 的儀表板或 API 查看使用情況:
# 查看金鑰資訊curl "http://localhost:4000/key/info" \ -H "Authorization: Bearer sk-litellm-key"
# 查看支出日誌curl "http://localhost:4000/spend/logs" \ -H "Authorization: Bearer $LITELLM_MASTER_KEY"- 連線問題:LiteLLM 預設在
http://localhost:4000執行,請確保你的防火牆或網路設定允許此連線。 - 端點相容性:OpenClaw 是透過 OpenAI 相容的
/v1/chat/completions端點進行連線。 - 功能限制:所有 OpenClaw 功能在透過 LiteLLM 執行時皆能正常運作,沒有任何限制。
如果你遇到其他設定問題,可以詢問 AI Setup Assistant。
OpenClaw Expert
還是卡住了?
如果這篇文件沒有解決你的情境,直接問 OpenClaw Expert,拿到可執行步驟。