How nithinvikas-av/llm_gateway works
1. System shape
- This repo is a 4-part LLM gateway: a React frontend, a FastAPI backend, PostgreSQL for persistent data, and Redis for short-lived rate-limit counters.
- docker-compose.yml is the local integration boundary; it runs all four services together.
2. Frontend boundary
- The UI lives under frontend/src and is organized by page and shared component layers.
- Entry flow starts at frontend/src/main.jsx → App.jsx, with routes for login, dashboard, keys, users, and a playground.
- The frontend does not hold business logic; it mainly calls backend APIs through frontend/src/api/* and stores auth state in frontend/src/store/authStore.js.
3. Authentication flow
- Google OAuth is the first trust boundary: backend/app/api/auth.py handles login and callback.
- After OAuth, the backend issues a JWT; the frontend uses that token for subsequent protected requests.
- Authorization is role-based (admin vs employee) and enforced in backend dependencies, not in the UI alone.
4. Backend API surface
- Route handlers are split by concern in backend/app/api/: auth.py, keys.py, gateway.py, users.py, and usage.py.
- These routes are thin controllers; they delegate real work to services under backend/app/services/.
- backend/main.py is the FastAPI entry point and likely wires the routers, middleware, and docs exposure.
5. Business logic layer
- key_service.py manages provider key storage/retrieval and encryption concerns.
- gateway_service.py is the request proxy to external LLM providers and is the main enforcement point for gateway policy.
- rate_limiter.py uses Redis to enforce per-key/request quotas.
- usage_service.py records token, request, and cost usage for analytics and billing-style reporting.
6. Persistence model
- SQLAlchemy models in backend/app/models/ define the persistent domain:
- user.py
- provider_key.py
- universal_key.py
- key_permission.py
- usage_log.py
- Alembic migrations under backend/alembic/versions/ control schema evolution; the project expects migrations to create and update the tables.
7. Data ownership and key management
- Provider API keys are stored per user per provider in provider_keys; the raw secret is never stored, only encrypted material plus IV.
- universal_keys are the keys employees use to access the gateway; they map back to the owning user and are indexed by hash for lookup.
- key_permissions attaches per-universal-key policy such as allowed models, RPM, token budget, and cost caps.
8. Gateway request flow
- A client request enters the frontend or direct API, authenticates with a JWT, then reaches backend/app/api/gateway.py.
- The gateway checks permissions and rate limits, resolves the caller’s universal key, fetches the owning provider credential, and forwards the request to the selected LLM provider.
- The response path also feeds usage accounting so every call becomes an auditable record.
9. Redis and quota enforcement
- Redis is a non-persistent control plane used for counters and rate-limit state.
- This keeps quota checks fast and separate from the durable PostgreSQL audit trail.
- The architecture therefore splits “enforcement now” in Redis from “record forever” in PostgreSQL.
10. Security and cryptography
- backend/app/core/security.py centralizes JWT creation/verification and symmetric encryption/decryption for stored provider keys.
- backend/app/core/dependencies.py is the enforcement layer for authenticated user context and admin-only access.
- backend/app/core/config.py and backend/app/core/redis_client.py externalize secrets and infra connections from business code.
11. Reporting and auditability
- usage_logs is append-only by design and is the source of truth for auditing, analytics, and cost tracking.
- The dashboard and usage endpoints read from this history rather than from transient runtime state.
- This makes the system suitable for both operational monitoring and governance.
12. Deployment and delivery
- Each tier has its own Dockerfile, and docker-compose.yml is the local orchestration layer.
- .github/workflows/deploy.yml and railway.toml indicate deployment automation to Railway-style infrastructure.
- backend/.env example and the root README show that secrets, OAuth credentials, DB URLs, and Redis settings are expected as environment configuration, not code.
13. Notable repo-specific boundary
- The repo is intentionally split between UI, API, persistence, and policy enforcement; the gateway service is the central integration point where auth, RBAC, quotas, encryption, and provider proxying all meet.
- Universal_Chat.py and secret-key.py appear to be auxiliary/top-level scripts rather than core runtime surfaces, so they are not part of the primary application architecture.
Main entry points: main.py FastAPI app · gateway.py Gateway API · docker-compose.yml Compose · gateway_service.py Gateway service
Components 23
Frontend
React UI and client-side state/API access
- App entry · Bootstraps the frontend application
frontend/src/main.jsx - Routes · Top-level app shell and route composition
frontend/src/App.jsx - Pages · Login, dashboard, keys, users, and playground screens
frontend/src/pages - API client · Browser-side API wrappers for auth, gateway, keys, usage, and users
frontend/src/api - Auth store · Client auth and token state
frontend/src/store/authStore.js
Backend
FastAPI gateway, services, and domain logic
- FastAPI app · Backend application entry point and router wiring
backend/main.py - Auth API · Google OAuth login and callback handling
backend/app/api/auth.py - Gateway API · LLM request proxy and policy enforcement endpoint
backend/app/api/gateway.py - Keys API · Provider and universal key management endpoints
backend/app/api/keys.py - Users API · User administration endpoints
backend/app/api/users.py - Usage API · Usage and reporting endpoints
backend/app/api/usage.py - Security · JWT handling and symmetric encryption for stored secrets
backend/app/core/security.py - Deps · Authenticated user context and admin-only access dependencies
backend/app/core/dependencies.py - Key service · Provider key storage, retrieval, and encryption workflow
backend/app/services/key_service.py - Gateway service · Core request proxy, policy checks, and provider forwarding
backend/app/services/gateway_service.py - Rate limiter · Redis-backed per-key and per-request quota enforcement
backend/app/services/rate_limiter.py - Usage service · Token, request, and cost accounting
backend/app/services/usage_service.py
Data layer
Persistent PostgreSQL models and Redis control-plane state
- Domain models · User, key, permission, and usage persistence model set
backend/app/models - Migrations · Alembic schema history and table evolution
backend/alembic/versions - PostgreSQL · Durable audit trail and domain data store
- Redis · Short-lived quota counters and rate-limit state
Connections 36
- Compose → App entry: runs · Local stack brings up the UI
- Compose → FastAPI app: runs · Local stack brings up the API
- Compose → PostgreSQL: includes · Local stack includes persistent database
- Compose → Redis: includes · Local stack includes quota store
- App entry → Routes: boots · Application entry mounts the route shell
- Routes → Pages: routes · Shell dispatches to page views
- Pages → API client: calls · Pages use API wrappers for backend communication
- Routes → Auth store: reads · App shell consumes auth state
- API client → FastAPI app: requests · Browser client talks to the API server
- FastAPI app → Auth API: mounts · Main app wires auth routes
- FastAPI app → Gateway API: mounts · Main app wires gateway routes
- FastAPI app → Keys API: mounts · Main app wires key management routes
- FastAPI app → Users API: mounts · Main app wires admin/user routes
- FastAPI app → Usage API: mounts · Main app wires reporting routes
- Auth API → Security: issues JWTs · OAuth login results in signed tokens
- Auth API → Deps: guards · Auth routes rely on request context helpers
- Gateway API → Deps: authorizes · Gateway enforces authenticated and role-based access
- Gateway API → Gateway service: delegates · Route handler hands off proxy logic
- Gateway API → Rate limiter: limits · Gateway checks quotas before forwarding
- Gateway API → Usage service: records · Gateway emits usage accounting after requests
- Keys API → Key service: delegates · Key endpoints use service layer for storage and crypto
- Keys API → Deps: authorizes · Key routes are protected by user context and roles
- Users API → Deps: authorizes · User admin routes require privileged access
- Usage API → Usage service: queries · Usage endpoints read accounting history
- Key service → Security: encrypts · Provider secrets are protected before persistence
- Key service → Domain models: persists · Key service works through ORM models
- Gateway service → Key service: resolves keys · Gateway fetches provider credentials for forwarding
- Gateway service → Rate limiter: consults · Gateway service enforces request policy through Redis state
- Gateway service → Usage service: emits usage · Gateway service contributes accounting records
- Usage service → Domain models: writes logs · Accounting is stored as durable usage history
- Domain models → PostgreSQL: maps to · ORM entities are backed by PostgreSQL
- Migrations → Domain models: evolves · Alembic schema tracks the domain model set
- Rate limiter → Redis: stores counters · Quota state lives in Redis rather than PostgreSQL
- Deploy → FastAPI app: deploys · Deployment automation targets the API service
- Deploy → App entry: deploys · Deployment automation targets the frontend service
- Deploy → Compose: mirrors · Infra config reflects the local composed stack