Transit API

GrokCode 自助 LLM 网关:LiteLLM Fargate 代理 Bedrock 模型

Build a self-service LLM gateway with LiteLLM on AWS Fargate that puts Amazon Bedrock foundation models behind an OpenAI-compatible API. Developers get instant access through a sin

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.

GrokCode 自助 LLM 网关:LiteLLM Fargate 代理 Bedrock 模型

什么是 GrokCode 自助 LLM 网关?

GrokCode 自助 LLM 网关是用 LiteLLM 在 AWS Fargate 上部署的代理, 把 Amazon Bedrock 的基础模型(Foundation Models)封装成 OpenAI 兼容的 API 接口。开发者只需配置一次,就能让 Cursor、Claude Code、Continue 等工具直接调用 Bedrock 上的 Claude Sonnet、Haiku 等模型,平台团队则通过门户实现用户自助开 key、设置预算、查看消耗。

此方案最适合 AI 产品团队、开发者平台或需要统一模型接入的管理者:你不需要让每个开发者直连 Bedrock,也不用自己维护模型列表和成本控制。目标是“一个入口、统一管理、按使用计量”,同时保留 Bedrock 的原生性能和隔离能力。

谁适合搭建?怎么决策?

  • 适用人群:需要集中控制多个团队 API 密钥的组织(产品经理、运维、财务团队)。
  • 不适合人群:个人开发者只需临时测试 Bedrock 模型时,可以直接用官方控制台;只想一键代理轻量工具的,LiteLLM Docker 版即可。
  • 决策依据:如果你已有 AWS 账号且愿意接受 CDK + Fargate 基础设施成本,建议选;否则用纯开源 LiteLLM + Vercel 部署的轻量版即可(成本更低,但缺乏企业级预算门户)。

GrokCode 自助 LLM 网关 核心优势

  • OpenAI 兼容:开发者配置 base_url 和 api_key 即可,无需改模型列表。
  • 按用户预算隔离:每个开发者拿到自己的 key,可设置每日/周消耗上限。
  • 模型隔离与观测:通过 Amazon Bedrock Application Inference Profiles 实现 per-model 配额和 IAM 最小权限。
  • 多区域支持:推荐 Global cross-Region inference(CRIS),自动路由到容量充足的区域。

核心架构

整个网关由五部分组成:

  • LiteLLM 代理(运行在 AWS Fargate)
  • 开发者自助门户(内置于 LiteLLM)
  • Amazon RDS(PostgreSQL)存储 key 与消费记录
  • Amazon ElastiCache for Valkey 缓存响应
  • AWS Secrets Manager 存放 master_key 与数据库凭证
  • Amazon CloudWatch 监控 RPM/TPS
  • Langfuse 提供应用级追踪(延迟、token、调试)

开发者请求从 IDE 直达 Fargate 上的 LiteLLM,LiteLLM 解析模型名后调用 Bedrock,并通过 IAM task role 完成授权。

搭建步骤

1. 准备 AWS 环境

创建 AWS 账号,开启 Amazon Bedrock 模型访问权限并分配 IAM 角色给 Fargate task。

2. 部署 LiteLLM on Fargate

参考 LiteLLM 官方 Docker Quick Start,在 Fargate 上启动任务。配置如下(推荐使用 Enterprise 版,免费版仅支持 5 用户):

```yaml model_list: - model_name: claude-sonnet-4-20250514 litellm_params: model: bedrock/arn:aws:bedrock:us-east-1:111122223333:application-inference-profile/sonnet-profile-id aws_region_name: us-east-1 max_retries: 3 model_info: input_cost_per_token: 0.000003 output_cost_per_token: 0.000015

litellm_settings: max_budget: 250 budget_duration: 7d max_parallel_requests: 10 cache: true cache_params: type: redis host: elasticache-endpoint port: 6379 database_url: os.environ/DATABASE_URL ```

环境变量通过 Secrets Manager 注入:LITELLM_MASTER_KEY 与 DATABASE_URL。

3. 创建 Bedrock Inference Profiles(推荐使用 CDK)

使用 AWS CDK 创建 Application Inference Profile:

``typescript const sonnetProfile = new bedrock.CfnApplicationInferenceProfile(this, 'SonnetProfile', { inferenceProfileName: 'claude-sonnet-gateway', modelSource: { copyFrom: arn:aws:bedrock:us-east-1:${this.account}:inference-profile/global.anthropic.claude-sonnet-4-20250514-v1:0, }, description: 'Claude Sonnet for developer coding assistants (Global CRIS)', }); ``

IAM 策略必须同时包含 profile ARN 与底层 foundation-model ARN(Region 通配符)。

4. 开发者接入配置

在 Cursor 或 Claude Code 中设置:

``json { "apiProvider": "openai-compatible", "apiBaseUrl": "", "apiKey": "sk-user-abc123", "model": "claude-sonnet-4-20250514" } ``

开发者可在 LiteLLM 门户自行创建 key 并绑定预算。

成本与配额示例

模型输入单价($ /M)输出单价($ /M)推荐预算上限(每日)
Claude Sonnet 40.0030.01550 USD
Claude Haiku 40.0010.00530 USD

实际价格以 AWS 官网 Bedrock 定价页为准。Fargate 任务按 CPU 利用率自动伸缩,无需预留实例。

监控与观测

开启 CloudWatch 监控 InvokeModel 指标。Langfuse 可补充 prompt-level 延迟与错误追踪。

延伸阅读

风险与边界

搭建此网关时请注意:

  • Fargate 任务需持续运行,基础成本不会为零(约 0.5–2 USD/小时,视规模而定)。
  • 每个开发者 key 必须绑定预算,否则容易超支。
  • LiteLLM Enterprise 版需付费授权,免费版用户数有限。
  • 生产环境建议添加 Langfuse + Guardrails,避免裸奔。
  • 所有成本以当日 AWS 定价为准,价格可能随区域和促销调整。

非法律意见声明:本文为技术参考,仅供学习与评估使用,不构成任何业务、法律或财务建议。实际部署前请自行验证架构与合规性。

English summary

GrokCode Self-Service LLM Gateway is a production-grade proxy built with LiteLLM on AWS Fargate that exposes Amazon Bedrock foundation models as a fully OpenAI-compatible API. Developers configure their IDEs once (Cursor, Claude Code, Continue, etc.) to point to the gateway URL and use their own API key; the gateway enforces per-user budgets, rate limits, and model isolation through AWS Application Inference Profiles. The architecture includes Fargate scaling, Valkey caching, RDS for spend tracking, CloudWatch metrics, and optional Langfuse observability. This solution is ideal for AI platforms and teams needing centralized cost control and model management without exposing Bedrock directly to end-users. All configuration snippets, CDK constructs, and deployment steps are provided for quick replication. Costs are tracked at token level and capped per key, with real-time visibility into usage. Always verify current AWS pricing and model availability in the official documentation before deployment.

(正文字符约 2450,去除空白后中文为主)

适用于 GrokCode 倍率榜。信息仅供参考,不构成购买、投资或法律意见。