ai-gateway/ai-gateway-bundle
Symfony bundle that turns your app into an AI gateway: unified /v1 API for chat/models, OpenAI-compatible + Anthropic providers, model fallback chains, per-key auth, budgets/rate limits, caching, cost tracking, SSE streaming, Prometheus metrics, dashboard and CLI.
Installation:
composer require ai-gateway/ai-gateway-bundle
Symfony Flex auto-registers the bundle; otherwise, add it to config/bundles.php.
Database Setup:
php bin/console doctrine:database:create --if-not-exists
php bin/console doctrine:schema:update --force
Configure Routes:
Add to config/routes.yaml:
ai_gateway:
resource: .
type: ai_gateway
First API Key: Use the CLI to create a provider, model, and key:
php bin/console provider:create --name openai --format openai --api-key 'your-api-key'
php bin/console model:create --alias test_model --provider openai --model 'gpt-3.5-turbo' --pricing-input 0.001 --pricing-output 0.002
php bin/console key:create --team default --name 'dev-key' --key 'aigw_dev_key'
First Request:
Inject GatewayInterface and call:
$response = $this->gateway->chat(new NormalizedRequest(
model: 'test_model',
key: 'aigw_dev_key',
messages: [['role' => 'user', 'content' => 'Hello']]
));
Provider Setup: Define providers (e.g., OpenAI, Anthropic) via CLI or dashboard. Each provider maps to an LLM endpoint.
php bin/console provider:create --name mistral --format openai --base-url 'https://api.mistral.ai/v1'
Model Configuration: Create model aliases for provider-specific models with pricing:
php bin/console model:create --alias mistral-7b --provider mistral --model 'mistral-7b' --pricing-input 0.001 --pricing-output 0.003
Model Chains: Group models into fallback chains (e.g., primary model → backup model):
php bin/console chain:create --alias 'mistral-chain'
php bin/console chain:step:add --chain mistral-chain --model mistral-7b --priority 1 --weight 80
php bin/console chain:step:add --chain mistral-chain --model gpt-3.5-turbo --priority 2 --weight 100
Key Management: Assign API keys to teams with budget/rate limits:
php bin/console key:create --team dev-team --name 'dev-key' --key 'aigw_dev_key' --budget-per-day 10
Usage in Code:
Use GatewayInterface for unified LLM calls:
$response = $this->gateway->chat(new NormalizedRequest(
model: 'mistral-chain', // Uses fallback chain
key: 'aigw_dev_key',
messages: [...]
));
Dependency Injection:
Bind GatewayInterface to a specific chain or model dynamically:
$this->gateway->setModel('mistral-7b'); // Override for a request
Streaming Responses: Use SSE for chat completions:
$response = $this->gateway->chat($request, ['stream' => true]);
Cost Tracking:
Log costs per request via ai_gateway_cost_dollars_total metric.
Caching: Enable deterministic SHA-256 caching for repeated prompts:
ai_gateway:
cache:
enabled: true
Dashboard Integration: Embed the dashboard in admin panels or use token auth for restricted access.
Key Validation:
aigw_* prefix (e.g., aigw_dev_key). Use hash('sha256', $rawKey) for CLI-generated keys.KeyStoreInterface to check hashes.Model Chain Priorities:
chain:list to debug chain steps.Rate Limiting:
429 Too Many Requests.KeyRules.Dashboard Token:
tokenRequired: true, the dashboard redirects to a login form. Ensure the token is passed in all links.?token=YOUR_TOKEN in URLs or configure Symfony firewall.Provider Timeouts:
retry config:ai_gateway:
retry:
max_attempts: 3
delay: 1000
Request Logs:
Query gateway_request_log for failed requests:
SELECT * FROM gateway_request_log WHERE status_code != 200 ORDER BY created_at DESC;
CLI Debugging:
Use provider:list and model:list to verify configurations:
php bin/console provider:list
php bin/console model:list
Metrics: Check Prometheus metrics for cost/latency:
ai_gateway_requests_totalai_gateway_cost_dollars_totalDashboard Filters: Use the dashboard’s "Failed Requests" tab to filter by status code or model.
Custom Providers:
Extend AIGateway\Core\Provider\ProviderInterface for unsupported formats (e.g., gemini).
Middleware:
Add pre/post-processing via AIGateway\Core\Middleware\MiddlewareStack:
$stack->add(new class implements MiddlewareInterface {
public function handle(Request $request, callable $next): Response {
// Modify request/response
return $next($request);
}
});
Event Listeners:
Subscribe to ai_gateway.request events for logging/auditing:
$dispatcher->addListener('ai_gateway.request', function (RequestEvent $event) {
// Log request details
});
Custom Dashboard Pages:
Extend the dashboard by overriding Twig templates in templates/ai_gateway/.
CLI Commands:
Add custom commands by implementing AIGateway\Command\CommandInterface and registering them in services.yaml.
How can I help you explore Laravel packages today?