Architecture Note · AI & Edge Infrastructure
Deterministic AI Gateway: Guardrails, Circuit-Breaker Routing, and Human-Gated Evals
Why thin LLM API wrappers fail under production load—and how pre-egress redaction, SQLite-backed circuit breakers, golden regression matrices, and human sign-off gates keep multi-provider inference reliable.
The Operational Problem: Unchecked LLM Traffic Across Teams
Enterprise engineering teams running millions of monthly completions across FinServ, LegalOps, support triage, and DevOps pipelines face an immediate control gap. Routing every prompt to a single frontier model inflates token spend, leaks sensitive identifiers into external logs, and couples critical workflows to one vendor’s availability. When a provider returns HTTP 429 rate limits or a prompt revision emits prose instead of valid JSON, downstream automation stalls without an audit trail explaining what changed.
Why Naive AI Wrappers Fail in Production
Thin proxy wrappers around provider SDKs break along four predictable fault lines:
- Silent PII and secret egress: Forwarding raw prompts to upstream endpoints exposes emails, phone numbers, IBANs, SSNs/SINs, JWTs, and API credentials (
sk-live,ghp_,AKIA) in vendor telemetry and local trace tables. - Unbounded outages and residency drift: Single-endpoint wrappers fail during 5xx outages or fall back blindly across jurisdictions, violating EU-Frankfurt or private VPC data-residency boundaries.
- Silent prompt regressions and schema breakage: Editing a prompt template without golden-dataset scoring degrades accuracy or breaks strict JSON contracts—for example, prepending conversational text before an expected JSON object.
- Runaway token loops: Without per-tenant RPM/TPM ceilings and daily token caps, a single uncached retry loop can trigger a +184% spend spike before operators notice.
The Deterministic Architecture That Fixes It
A production AI gateway isolates stochastic model inference inside deterministic policy, routing, evaluation, and observability boundaries built on FastAPI and SQLite (WAL mode):
- Pre-egress redaction and guardrail enforcement (
POST /api/gateway/complete): Before any upstream call, cache lookup, or SQLite trace write, deterministic inspection scrubs PII and secrets into typed tokens ([REDACTED_SECRET],[REDACTED_IBAN],[REDACTED_SSN],[REDACTED_EMAIL],[REDACTED_PHONE]). Injection-override attempts return HTTP400, toxic payloads are halted, and outputs failing caller-supplied JSON Schema contracts—such as missingdecision, boundedconfidence(0–1),owner_team, or booleanrequires_human_signoff—return HTTP422. - Policy-driven routing, semantic caching, and circuit breakers: Workloads route across Frontier Cloud, Fast Balanced, EU-Frankfurt, and Sovereign Private VPC tiers under
Balanced SLA,Cost-First,Low-Latency Edge, orSovereign / Private-Onlypolicies. Normalized redacted prompts hit a local semantic response cache (45 ms p95, zero token spend), reducing blended cost by up to 72% relative to frontier-only routing. SQLite-tracked circuit breakers (closed→open→half_open) automatically fail over across healthy tiers or fail closed when sovereign residency permits no fallback. AnOfflineDeterministicAdapter(LLM_MODE=offline) enables deterministic local verification alongsideOpenAICompatibleAdapter. - Golden regression matrices and human-gated promotion (
POST /api/evals/run,POST /api/evals/{run_id}/promote): Candidate prompts (prompt_v1vsprompt_v2) run side by side across golden suites—or0600-permissioned multipart uploads wiped automatically afterUPLOAD_TTL_SECONDS=3600—scoring accuracy, faithfulness, JSON schema validity, p95 latency, and cost per 1k requests. Promoting a candidate enforces a mandatory human-approval gate (HTTP409if reviewer identity or rationale is missing), ensuring flagged regressions like an omitted indemnity clause require explicit sign-off and append a SHA-256 hash-chained receipt to an immutableaudit_logstable protected by SQLiteBEFORE UPDATEandBEFORE DELETEtriggers. - Per-tenant budgets and formula-safe exports (
POST /api/budgets/policies,GET /api/observability/traces): Tenant-scoped SHA-256 invite tokens enforce daily token caps, sliding-window RPM/TPM limits (429), 50% RPM emergency throttling, and signed temporary cap overrides. Observability exports produce RFC 4180 CSVs that prefix leading=,+,-,@, tabs, and carriage returns with an apostrophe to block spreadsheet formula injection.
Operational Takeaway
Reliable enterprise AI comes from deterministic controls around the model, not prompt engineering alone. When secrets are scrubbed before egress, circuit breakers respect residency boundaries, prompt changes pass side-by-side regression diffs under human sign-off, and tenant telemetry exports cleanly with hash-chained audit proofs, platform teams cut spend while keeping production predictable.