DOCS / ARCHITECTURE / DATA MODEL

Data Model

applications PK uuid id text name null = unlimited int monthly_token_quota null = unlimited int rate_limit_per_minute null = unrestricted text_array allowed_models cost | latency | balanced | null enum routing_strategy timestamp disabled_at api_keys PK uuid id FK uuid application_id text key_hash text key_prefix timestamp revoked_at timestamp last_used_at usage_records PK uuid id FK uuid application_id text model text provider int prompt_tokens int completion_tokens request_logs PK uuid id FK uuid application_id text model null if failed before routing text provider success | error enum status int http_status int attempts int latency_ms numeric estimated_cost_usd application_usage_counters (no attributes) rate_limit_counters (no attributes) management_api_keys PK uuid id text key_hash timestamp revoked_at issues generates generates tracked_by tracked_by

Tables

applications

The unit of tenancy. Every row doubles as its own policy document.

ColumnTypeMeaning
iduuid, PK
nametext
created_attimestamptz
monthly_token_quotainteger, nullablenull = unlimited. Enforced by quota reservation.
rate_limit_per_minuteinteger, nullablenull = unlimited. Enforced by the fixed-window rate limiter.
allowed_modelstext[], nullablenull = unrestricted. Checked against the raw requested model name, before alias resolution.
routing_strategyenum('cost','latency','balanced'), nullablenull = use the registry's declared order.
disabled_attimestamptz, nullable Set it, and every one of the application's API keys stops working immediately — no need to revoke them individually.

api_keys

ColumnTypeMeaning
iduuid, PK
application_iduuid, FK → applications.id
key_hashtext HMAC-SHA256 of the raw key, never the key itself. See Security.
key_prefixtext First 23 characters of the issued key (lattice_sk_ + 12 chars), safe to display in a UI so an admin can recognize a key without ever seeing it in full again.
revoked_attimestamptz, nullable
last_used_attimestamptz, nullableBest-effort touched on every successful auth.

usage_records

One row per completed request that produced token counts — the raw ledger application_usage_counters is a running aggregate of.

ColumnType
iduuid, PK
application_iduuid, FK
model, providertext
prompt_tokens, completion_tokensinteger, nullable
created_attimestamptz

application_usage_counters

A running total per application per calendar month, keyed by (application_id, period_start). Exists so a quota check is a single row read, not a scan over usage_records.

ColumnType
application_iduuid, FK — part of composite PK
period_starttimestamptz — month start (UTC), part of composite PK
tokens_usedinteger, default 0
requests_usedinteger, default 0

rate_limit_counters

The same "single row read" idea, for the 60-second rate-limit window instead of the monthly quota.

ColumnType
application_iduuid, FK — part of composite PK
window_starttimestamptz — floored to the current 60s window, part of composite PK
request_countinteger, default 0

request_logs

Append-only observability history, written best-effort so a logging failure never affects the client response — see Request Lifecycle .

ColumnTypeMeaning
iduuid, PK
application_iduuid, FK
request_idtextServer-generated Bun.randomUUIDv7(), correlates with structured logs.
modeltextThe requested model name, as sent by the client.
providertext, nullablenull if the request failed before routing resolved a provider (e.g. bad model name, quota exceeded).
statusenum('success','error')
http_statusinteger
error_codetext, nullable A LatticeErrorCode — see Errors.
attemptsintegerHow many providers were tried.
latency_msinteger
prompt_tokens, completion_tokens, total_tokensinteger, nullable
estimated_cost_usdnumeric(12,6), nullable See cost estimation.
created_attimestamptzIndexed alongside application_id for the management API's paginated request log.

management_api_keys

Structurally similar to api_keys, but with no foreign key to applications — a management key authorizes the gateway's own admin surface, not any one application's traffic.

ColumnType
iduuid, PK
nametext
key_hash, key_prefixtext
revoked_at, last_used_attimestamptz, nullable

provider_circuit_state / provider_latency_state

Not shown on the ERD above (they have no relationship to applications) — these are the Postgres-backed sync points for the in-memory circuit breaker and latency tracker , keyed by provider and model_id respectively.

Migrations

The gateway uses Drizzle Kit against server/database/schema.ts. See Database & Migrations for the day-to-day workflow.

LATTICE — INTERNAL AI INFRASTRUCTURE
GITHUB © 2026