ai-gateway/ai-gateway-bundle
最新稳定版本:v1.0.1
Composer 安装命令:
composer require ai-gateway/ai-gateway-bundle
包简介
PHP AI Gateway — Unified LLM proxy with fallback, caching, rate limiting and cost tracking
README 文档
README
Symfony bundle that turns any project into an AI gateway — unified LLM access, per-key auth, budget enforcement, cost tracking, and a dashboard.
Why?
Every project that calls LLMs reinvents the same plumbing: provider SDKs, API key management, retry logic, cost tracking, budget limits. AIGateway bundles all of this into one Symfony package powered by the official Symfony AI library.
One composer require, one config file, all models.
What it does
- Unified API — Expose
/v1/chat/completions,/v1/models,/v1/healthin your Symfony app - Multi-provider — OpenAI, Anthropic, Gemini, Ollama, Azure, any OpenAI-compatible endpoint
- Per-key auth — Hierarchical teams with restrictive rule inheritance
- Budget enforcement — Daily/monthly per-key budget limits in USD
- Rate limiting — Sliding window per key
- Fallback pipelines — Try model A → model B → model C automatically
- Response caching — Deterministic SHA-256 cache
- Cost tracking — Token counting and cost per request
- Streaming SSE — Server-sent events for chat completions
- Prometheus metrics —
ai_gateway_requests_total,ai_gateway_cost_dollars_total, etc. - Web dashboard — Dark-themed UI at
/dashboardwith Chart.js analytics - Route prefix — Optional
routes.prefixto mount under/ai-gateway/or any prefix
Install
composer require ai-gateway/ai-gateway-bundle
Symfony Flex auto-registers the bundle. If not:
// config/bundles.php return [ AIGateway\Bundle\AIGatewayBundle::class => ['all' => true], ];
Configure
# config/packages/ai_gateway.yaml ai_gateway: providers: openai: format: openai api_key: '%env(OPENAI_API_KEY)%' anthropic: format: anthropic api_key: '%env(ANTHROPIC_API_KEY)%' ollama: format: ollama base_url: 'http://localhost:11434' # Any OpenAI-compatible provider deepseek: format: openai api_key: '%env(DEEPSEEK_API_KEY)%' base_url: 'https://api.deepseek.com' models: gpt_4o: provider: openai model: gpt-4o pricing: { input: 2.50, output: 10.00 } claude_sonnet: provider: anthropic model: claude-sonnet-4-20250514 pricing: { input: 3.00, output: 15.00 } local_llama: provider: ollama model: llama3 pricing: { input: 0.0, output: 0.0 } deepseek_chat: provider: deepseek model: deepseek-chat pricing: { input: 0.27, output: 1.10 } aliases: smart: gpt_4o fast: deepseek_chat auth: enabled: false
Load routes
Add to your config/routes.yaml:
ai_gateway: resource: . type: ai_gateway
All routes are now available: /v1/chat/completions, /v1/models, /v1/health, /dashboard, etc.
With a prefix
ai_gateway: providers: { ... } routes: prefix: /ai-gateway
Routes become /ai-gateway/v1/chat/completions, /ai-gateway/dashboard, etc.
Use in your code
Inject GatewayInterface wherever you need AI:
use AIGateway\Core\GatewayInterface; use AIGateway\Core\NormalizedRequest; final class MyService { public function __construct( private readonly GatewayInterface $gateway, ) {} public function ask(string $prompt): string { $request = new NormalizedRequest( model: 'smart', messages: [['role' => 'user', 'content' => $prompt]], ); $response = $this->gateway->chat($request); return $response->content; } }
You control the routes, auth, and middleware. AIGateway handles the provider communication via Symfony AI.
Auth & Teams
Enable auth to require API keys for all requests:
ai_gateway: auth: enabled: true required: true
CLI — Create a Team
php bin/console ai-gateway:team:create \
--name "Engineering" \
--budget-per-day 100 \
--models "gpt_4o,claude_sonnet"
CLI — Create an API Key
php bin/console ai-gateway:key:create \
--name "Frontend App" \
--team <team-id> \
--budget-per-day 20 \
--models "gpt_4o"
Hierarchy
A team defines maximum limits. A key inherits team rules and can only restrict further:
Team "Engineering" ($100/day, gpt_4o + claude_sonnet)
└── Key "Mathieu" ($20/day, gpt_4o only)
└── Key "CI Bot" ($5/day, claude_sonnet only, 10 req/min)
Supported Providers
| Provider | Format | Notes |
|---|---|---|
| OpenAI | openai |
Native Symfony AI bridge |
| Anthropic | anthropic |
Claude models |
| Google Gemini | gemini |
Gemini Pro, Flash |
| Ollama | ollama |
Local models |
| Azure OpenAI | azure |
Enterprise Azure |
| Any OpenAI-compatible | openai + base_url |
DeepSeek, Groq, OpenRouter, Mistral... |
Custom Provider Example
ai_gateway: providers: groq: format: openai api_key: '%env(GROQ_API_KEY)%' base_url: 'https://api.groq.com/openai/v1' openrouter: format: openai api_key: '%env(OPENROUTER_API_KEY)%' base_url: 'https://openrouter.ai/api/v1'
API Endpoints
| Method | Path | Description |
|---|---|---|
POST |
/v1/chat/completions |
Chat completion (supports stream: true) |
GET |
/v1/models |
List available models |
GET |
/v1/health |
Health check |
GET |
/v1/metrics |
Prometheus metrics |
GET |
/v1/stats |
JSON usage statistics |
GET |
/dashboard |
Web dashboard |
GET |
/dashboard/keys |
API key management |
GET |
/dashboard/teams |
Team management |
GET |
/dashboard/analytics |
Charts and analytics |
CLI Commands
| Command | Description |
|---|---|
ai-gateway:key:create |
Create an API key with optional overrides |
ai-gateway:key:list |
List all API keys |
ai-gateway:key:info <id> |
Show key details + usage |
ai-gateway:key:revoke <id> |
Disable an API key |
ai-gateway:team:create |
Create a team with rules |
ai-gateway:team:list |
List all teams |
ai-gateway:team:info <id> |
Show team details + keys |
ai-gateway:stats |
Show gateway usage statistics |
Configuration Reference
ai_gateway: routes: enabled: true # Enable/disable route loading prefix: '' # Route prefix (e.g. /ai-gateway) providers: <name>: format: openai|anthropic|gemini|ollama|azure api_key: string base_url: string|null # Required for ollama, azure, and custom providers completions_path: '/v1/chat/completions' streaming: true vision: false function_calling: true max_tokens_per_request: 128000 models: <alias>: provider: string model: string pricing: { input: float, output: float } max_tokens: 128000 pipelines: <name>: models: [model1, model2, ...] aliases: <alias>: <model-alias> <alias>: 'pipeline:<name>' retry: max_attempts: 2 delay_ms: 1000 backoff: fixed|exponential auth: enabled: false required: true
Want a standalone server?
If you just want to run AIGateway as a dedicated server without integrating it into an existing project, check out symfony-ai-gateway-standalone — a pre-configured Symfony project with Docker, auth setup script, and ready-to-run config.
Requirements
- PHP >= 8.2
- Symfony ^7.0 || ^8.0
- Doctrine DBAL ^4.4 (for auth storage, SQLite or any DBAL-supported DB)
License
MIT
ai-gateway/ai-gateway-bundle 适用场景与选型建议
ai-gateway/ai-gateway-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「php」 「proxy」 「gateway」 「ai」 「openai」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ai-gateway/ai-gateway-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ai-gateway/ai-gateway-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ai-gateway/ai-gateway-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lazy loading for middleware and request handlers
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Get working public proxy list for free by PubProxy (No API key required).
Alfabank REST API integration
Standalone proxy server in PHP sockets, I am using Guzzle to route the requests. Idea was if I can make HTTP Proxy Server in php to get more control over proxy and custom logic. Currently it only supports http requests you can choose port of your own choice plus if you want to make it public or priv
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 56
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-13