Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Ai Gateway Bundle Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Symfony Native Integration: The bundle is designed for Symfony (Flex auto-registration, Doctrine DBAL, Symfony AI library), making it a seamless fit for Symfony-based applications. Non-Symfony PHP projects would require significant abstraction work.
  • Unified AI Gateway Pattern: Aligns with modern microservices and API-driven architectures by abstracting LLM provider complexity behind a single interface (GatewayInterface).
  • Modularity: Decouples AI logic from business logic, enabling reuse across services (e.g., chatbots, data processing).
  • Extensibility: Supports custom providers via the openai/anthropic formats, allowing future-proofing for niche or proprietary LLMs.

Integration Feasibility

  • Low-Coupling: Inject GatewayInterface into services via dependency injection (Symfony’s native pattern), minimizing invasive changes.
  • Configuration-Driven: Core setup (providers, models, chains) is managed via CLI/dashboard, reducing manual code changes.
  • Route Prefixing: Optional /ai-gateway/ prefix avoids route collisions in existing Symfony apps.
  • Database Agnostic: Uses Doctrine DBAL (supports SQLite, PostgreSQL, MySQL), but requires schema migrations.

Technical Risk

  • Symfony Dependency: Tight coupling to Symfony components (e.g., Symfony AI library, Doctrine) may complicate adoption in non-Symfony PHP stacks.
  • Database Schema: Initial schema setup (doctrine:schema:update) is mandatory and could fail in constrained environments (e.g., shared hosting).
  • Dashboard Security: Built-in token auth is basic; projects with strict security requirements may need to integrate with Symfony’s firewall.
  • Performance Overhead: Caching (SHA-256) and rate limiting add latency; benchmarking is critical for high-throughput use cases.
  • Provider-Specific Quirks: Some providers (e.g., Ollama, local endpoints) may introduce instability or require custom error handling.

Key Questions

  1. Symfony Version Compatibility: Does the target Symfony version (e.g., 6.4+) align with the bundle’s requirements?
  2. Provider Support: Are all required providers (e.g., OpenAI, Anthropic, custom endpoints) supported by the bundle’s formats?
  3. Cost Tracking Granularity: Does the token-level cost tracking meet budgeting needs (e.g., per-team vs. global limits)?
  4. Fallback Logic: How will model failures in chains be logged/alerted (e.g., Slack, Prometheus)?
  5. Dashboard Accessibility: Will the dashboard be exposed to internal teams only, or require additional auth layers?
  6. Scaling Assumptions: Does the bundle handle expected request volumes (e.g., 10K RPS) without external load balancing?
  7. Compliance: Are API key storage and request logging compliant with data protection regulations (e.g., GDPR)?

Integration Approach

Stack Fit

  • Symfony Ecosystem: Ideal for Symfony 5.4+/6.x apps using Doctrine, Flex, and Symfony AI.
  • PHP 8.1+: Required for modern features (e.g., readonly properties, typed constructors).
  • Database: Supports PostgreSQL/MySQL/SQLite; avoid SQLite in production due to concurrency limits.
  • Monitoring: Prometheus metrics integrate with Grafana/Prometheus stacks; SSE streaming works with modern frontend frameworks (e.g., React, Vue).

Migration Path

  1. Pilot Phase:
    • Install the bundle in a staging environment (composer require ai-gateway/ai-gateway-bundle).
    • Configure a single provider/model chain via CLI (provider:create, model:create, chain:create).
    • Test with GatewayInterface in a non-critical service.
  2. Gradual Rollout:
    • Replace direct LLM SDK calls with the bundle’s GatewayInterface in modules (e.g., chat, search).
    • Migrate auth to use API keys (aigw_* prefix) instead of hardcoded credentials.
  3. Dashboard Adoption:
    • Enable the dashboard with token auth (DASHBOARD_TOKEN).
    • Offload provider/model management to non-dev teams via UI.
  4. Full Cutover:
    • Replace all LLM SDKs with the bundle’s unified API.
    • Enable budget enforcement and rate limiting in production.

Compatibility

  • Existing APIs: The /v1/chat/completions endpoint mirrors OpenAI’s API, easing migration for apps using OpenAI SDKs.
  • Auth Systems: Integrates with Symfony’s security component or uses built-in token auth.
  • CI/CD: CLI commands enable infrastructure-as-code for providers/models (e.g., Terraform + Symfony CLI).
  • Legacy Systems: Non-Symfony PHP apps can use the bundle’s HTTP API (/v1/* endpoints) as a microservice.

Sequencing

  1. Infrastructure:
    • Set up database (PostgreSQL recommended) and run schema migrations.
    • Configure Symfony security (firewall or dashboard token).
  2. Configuration:
    • Add providers via CLI/dashboard (e.g., OpenAI, Anthropic).
    • Define model chains with fallback logic.
  3. Development:
    • Inject GatewayInterface into services and replace SDK calls.
    • Test caching, rate limiting, and cost tracking.
  4. Monitoring:
    • Expose Prometheus metrics to your observability stack.
    • Set up alerts for budget thresholds or provider failures.
  5. Go-Live:
    • Enable dashboard for teams.
    • Monitor /v1/health endpoint for uptime.

Operational Impact

Maintenance

  • Provider Management: Centralized via dashboard/CLI; no YAML edits after initial setup.
  • Key Rotation: Built-in API key management with hierarchical teams.
  • Schema Updates: Doctrine migrations handle database changes; monitor for breaking changes.
  • Dependency Updates: Bundle follows Symfony’s release cycle; test upgrades for compatibility.

Support

  • Troubleshooting:
    • Use /dashboard for real-time request logs and metrics.
    • Prometheus metrics (ai_gateway_requests_total) help diagnose latency or failures.
    • CLI commands (provider:list, model:list) validate configurations.
  • Common Issues:
    • Rate Limiting: Adjust rateLimitPerMinute per team/key.
    • Budget Overages: Set alerts for ai_gateway_cost_dollars_total.
    • Provider Failures: Use model chains with fallbacks to mitigate outages.
  • Documentation: README is minimal; expect to supplement with internal runbooks for CLI/dashboard workflows.

Scaling

  • Horizontal Scaling: Stateless design allows scaling Symfony app instances; use Redis for distributed caching.
  • Database: Partition gateway_request_log table if logging high volumes.
  • Rate Limiting: Sliding window algorithm scales to high QPS but may require Redis for distributed environments.
  • Cost Tracking: Aggregated metrics are lightweight; token-level logging may need archiving.

Failure Modes

Component Failure Mode Mitigation
Provider API Outage or throttling Model chains with fallbacks to alternative providers.
Database Connection loss Use read replicas; implement retry logic in Doctrine.
Caching Layer Cache invalidation issues Short TTLs for deterministic requests; monitor cache hit ratios.
Dashboard Token leakage or unauthorized access Combine with Symfony firewall; rotate tokens regularly.
Symfony App High latency under load Optimize PHP-FPM worker count; use OPcache.
Budget Enforcement Unexpected cost spikes Set hard monthly limits; alert on 80% usage.

Ramp-Up

  • Developer Onboarding:
    • 1-hour workshop on GatewayInterface, CLI commands, and dashboard.
    • Cheat sheet for common tasks (e.g., "How to add a new provider").
  • Team Roles:
    • Devs: Use GatewayInterface in code; manage models/chains via CLI.
    • Ops: Monitor Prometheus metrics; handle provider outages.
    • Product: Configure budgets/rate limits via dashboard.
  • Training Materials:
    • Recorded demo of dashboard workflows.
    • Example GatewayBootstrap class for CI/CD provisioning.
  • Feedback Loop:
    • Track usage of model chains vs. direct models.
    • Survey teams on dashboard usability and CLI ergonomics.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle