# stphnlwnga/llm-gateway architecture

> Architecture diagram of the GitHub repository stphnlwnga/llm-gateway, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/stphnlwnga/llm-gateway
- Repository: https://github.com/stphnlwnga/llm-gateway

## Overview

llm-gateway is currently a Go walking skeleton: the live user workflow serves health, readiness, and a stub OpenAI model-list endpoint, while a separate admin command runs Postgres migrations. Configuration and structured logging support both binaries; Postgres readiness is wired into the HTTP API. The README describes provider routing, tenant keys, rate limits, budgets, usage metering, and tracing as product goals, but this milestone does not implement those request capabilities. Relationships shown here follow the excerpts and README; unsampled behavior is not inferred.

Main entry points: [`cmd/gateway/main.go`](https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gateway/main.go) (Gateway composition), [`internal/httpapi/server.go`](https://github.com/stphnlwnga/llm-gateway/blob/main/internal/httpapi/server.go) (HTTP routes and readiness), [`cmd/gatewayctl/main.go`](https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gatewayctl/main.go) (Migration command), [`cmd/gateway/serve.go`](https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gateway/serve.go) (Drain and shutdown)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Gateway runtime"]
  node_gateway["Gateway composition<br/>[main.go]"]
  node_lifecycle["Drain and shutdown<br/>[serve.go]"]
  node_httpapi["HTTP routes and readiness<br/>[server.go]"]
  node_errors["OpenAI error responses<br/>[errors.go]"]
end

subgraph group_admin["Database administration"]
  node_admincli["Migration command<br/>[main.go]"]
  node_migrator["Migration runner<br/>[migrate.go]"]
end

subgraph group_support["Shared support"]
  node_settings["Typed settings<br/>[config.go]"]
  node_logger["Structured logger<br/>[logger.go]"]
  node_postgres["Postgres pool and ping<br/>[postgres.go]"]
end

node_client(("API client"))
node_operator(("Operator"))
node_database[("Postgres")]

node_client -->|"calls API"| node_httpapi
node_gateway -->|"loads settings"| node_settings
node_gateway -->|"creates logger"| node_logger
node_gateway -->|"opens pool"| node_postgres
node_gateway -->|"builds handler"| node_httpapi
node_gateway -->|"starts serving"| node_lifecycle
node_gateway -->|"sets drain callback"| node_lifecycle
node_httpapi -->|"checks readiness"| node_postgres
node_httpapi -->|"formats errors"| node_errors
node_operator -->|"runs migration"| node_admincli
node_admincli -->|"loads settings"| node_settings
node_admincli -->|"creates logger"| node_logger
node_admincli -->|"runs command"| node_migrator
node_migrator -->|"applies migrations"| node_database
node_postgres -->|"pings"| node_database
node_lifecycle -->|"starts draining"| node_httpapi

click node_gateway "https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gateway/main.go"
click node_lifecycle "https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gateway/serve.go"
click node_httpapi "https://github.com/stphnlwnga/llm-gateway/blob/main/internal/httpapi/server.go"
click node_errors "https://github.com/stphnlwnga/llm-gateway/blob/main/internal/httpapi/errors.go"
click node_settings "https://github.com/stphnlwnga/llm-gateway/blob/main/internal/config/config.go"
click node_logger "https://github.com/stphnlwnga/llm-gateway/blob/main/internal/telemetry/logger.go"
click node_postgres "https://github.com/stphnlwnga/llm-gateway/blob/main/internal/store/postgres/postgres.go"
click node_admincli "https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gatewayctl/main.go"
click node_migrator "https://github.com/stphnlwnga/llm-gateway/blob/main/internal/store/postgres/migrate.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_gateway,node_lifecycle,node_httpapi,node_errors,node_client toneBlue
class node_admincli,node_migrator,node_database toneAmber
class node_settings,node_logger,node_postgres toneMint
class node_operator toneIndigo
```

## Components

### Gateway runtime

- [Gateway composition](https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gateway/main.go): `cmd/gateway/main.go`
- [Drain and shutdown](https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gateway/serve.go): `cmd/gateway/serve.go`
- [HTTP routes and readiness](https://github.com/stphnlwnga/llm-gateway/blob/main/internal/httpapi/server.go): `internal/httpapi/server.go`
- [OpenAI error responses](https://github.com/stphnlwnga/llm-gateway/blob/main/internal/httpapi/errors.go): `internal/httpapi/errors.go`

### Database administration

- [Migration command](https://github.com/stphnlwnga/llm-gateway/blob/main/cmd/gatewayctl/main.go): `cmd/gatewayctl/main.go`
- [Migration runner](https://github.com/stphnlwnga/llm-gateway/blob/main/internal/store/postgres/migrate.go): `internal/store/postgres/migrate.go`

### Shared support

- [Typed settings](https://github.com/stphnlwnga/llm-gateway/blob/main/internal/config/config.go): `internal/config/config.go`
- [Structured logger](https://github.com/stphnlwnga/llm-gateway/blob/main/internal/telemetry/logger.go): `internal/telemetry/logger.go`
- [Postgres pool and ping](https://github.com/stphnlwnga/llm-gateway/blob/main/internal/store/postgres/postgres.go): `internal/store/postgres/postgres.go`

### Other components

- **API client**
- **Operator**
- **Postgres**

## Connections

- API client → HTTP routes and readiness: calls API (evidence: `README.md`)
- Gateway composition → Typed settings: loads settings (evidence: `cmd/gateway/main.go`)
- Gateway composition → Structured logger: creates logger (evidence: `cmd/gateway/main.go`)
- Gateway composition → Postgres pool and ping: opens pool (evidence: `cmd/gateway/main.go`)
- Gateway composition → HTTP routes and readiness: builds handler (evidence: `cmd/gateway/main.go`)
- Gateway composition → Drain and shutdown: starts serving (evidence: `cmd/gateway/main.go`)
- Gateway composition → Drain and shutdown: sets drain callback (evidence: `cmd/gateway/main.go`)
- HTTP routes and readiness → Postgres pool and ping: checks readiness
- HTTP routes and readiness → OpenAI error responses: formats errors (evidence: `internal/httpapi/server.go`)
- Operator → Migration command: runs migration (evidence: `README.md`)
- Migration command → Typed settings: loads settings (evidence: `cmd/gatewayctl/main.go`)
- Migration command → Structured logger: creates logger (evidence: `cmd/gatewayctl/main.go`)
- Migration command → Migration runner: runs command (evidence: `cmd/gatewayctl/main.go`)
- Migration runner → Postgres: applies migrations (evidence: `internal/store/postgres/migrate.go`)
- Postgres pool and ping → Postgres: pings (evidence: `internal/store/postgres/postgres.go`)
- Drain and shutdown → HTTP routes and readiness: starts draining (evidence: `cmd/gateway/serve.go`)
