Grok API 中转自己搭:vLLM + LiteLLM 搭建 OpenAI 兼容代理
内容刷新 / GEO:补 English summary 与最新核对清单 — gc-grok-api-proxy-diy
Full article body is primarily in Chinese for SEO depth; key points above are localized. Use the language switcher and deep links for global navigation.

Grok API 中转自己搭:vLLM + LiteLLM 搭建 OpenAI 兼容代理
这是你完全自主搭建的 Grok API 中转 方案。 它让本地部署的 vLLM 模型通过 LiteLLM 代理,实现统一 OpenAI 兼容接口。 适用于有 vLLM 本地部署需求、希望节省中转费用或自定义模型的开发者。 决策时优先看你的硬件资源和预算边界:高并发需求选云中转,低成本本地优先。
vLLM 是目前最快部署大模型的开源框架,LiteLLM 则提供成熟的代理层,两者搭配可快速构建一个功能完整的 OpenAI 兼容端点。
现状与数据更新
2026 年 9 月,LiteLLM 已原生支持 xAI Grok 系列模型(grok-beta、grok-3-mini 等),无需额外处理。官方文档显示支持 chat completions、streaming 和批量任务。
vLLM 在 2026 年 Q3 仍保持轻量部署优势,单卡 A100 可服务 70B+ 模型,本地成本显著低于官方 $15/M 的 Grok API 定价。
核对清单
准备以下材料,确保每步可执行:
- Python 3.11+ 环境
- vLLM 0.7+(推荐 pip install vllm)
- LiteLLM 最新版(pip install litellm)
- Grok API 密钥(xAI 控制台申请)
- 至少一块 GPU(NVIDIA 显卡)
- Docker(可选,推荐用于隔离部署)
风险边界
此方案为个人本地学习与开发使用。非法律意见声明:仅供参考,请根据当前法规和政策自行评估适用性。 vLLM 本地部署模型若涉及敏感数据,建议启用 --trust-remote-code 并隔离网络环境。 LiteLLM 代理层会增加一层中转消耗,实际支出以官方渠道为准。 升级时请先在测试环境验证兼容性,避免生产中断。
搭建步骤
1. 安装与环境准备
``bash pip install vllm litellm ``
2. 部署 vLLM 本地模型
创建 vllm_grok.yaml 配置文件:
``yaml model: grok-beta engine: vllm ``
启动服务(推荐使用 Docker 方式隔离):
``bash docker run --gpus all -d \ -v $(pwd)/vllm_grok.yaml:/app/vllm_grok.yaml \ --name vllm-grok \ ghcr.io/vllm-project/vllm:main \ --model /app/vllm_grok.yaml \ --port 8000 \ --api-key your-secret-key ``
模型文件、LoRA 或量化版本均可通过 model 参数自定义(具体配置以 vLLM 官方文档为准)。
3. 配置 LiteLLM 代理
在项目根目录创建 config.yaml:
``yaml model_list: - model_name: grok-proxy litellm_params: model: xai/grok-beta api_key: xai-你的密钥 api_base: http://localhost:8000/v1 ``
启动 LiteLLM 代理(后台运行):
``bash litellm --config config.yaml --port 4000 ``
4. 验证与使用
OpenAI 兼容客户端可直接使用:
``bash curl http://localhost:4000/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer sk-your-litellm-key" \ -d '{ "model": "grok-proxy", "messages": [{"role": "user", "content": "你好"}], "max_tokens": 100 }' ``
测试成功后,即可接入 Cursor、Claude Code 等工具。 切换模型只需修改 model_name 和 model 参数,无需重启服务。
站内路径
风险与边界
此方案为个人本地学习与开发使用。非法律意见声明:仅供参考,请根据当前法规和政策自行评估适用性。 vLLM 本地部署模型若涉及敏感数据,建议启用 --trust-remote-code 并隔离网络环境。 LiteLLM 代理层会增加一层中转消耗,实际支出以官方渠道为准。 升级时请先在测试环境验证兼容性,避免生产中断。
延伸阅读
English summary
This guide covers building a fully custom Grok API proxy using vLLM for local model deployment combined with LiteLLM as the OpenAI-compatible gateway. Perfect for developers who want to reduce costs compared to official xAI pricing, add custom models or LoRAs, or route requests through a unified endpoint.
vLLM handles high-speed inference on consumer GPUs, while LiteLLM manages routing, logging, and OpenAI SDK compatibility. As of September 2026, LiteLLM officially supports all xAI Grok models (grok-beta, grok-3-mini, etc.).
The process is fully open-source and reproducible: install both libraries, deploy vLLM with your model config, then expose via LiteLLM on port 4000. It works with any OpenAI-compatible client including Cursor, Claude Code, and custom scripts.
Cost savings come from avoiding per-token charges on official Grok API; however, your hardware limits concurrency. Always test in a staging environment and monitor token usage. No third-party accounts or paid subscriptions are required.
For the latest model support and pricing, check the xAI console and vLLM release notes. This DIY approach is ideal for personal projects, research, or controlled environments where official proxies are too expensive.
适用于 GrokCode 倍率榜。信息仅供参考,不构成购买、投资或法律意见。