paulserban.eu

Writing Edition

Paul Serban

Writing, snippets & book notes

Cheat sheet · Post

Production LLM Architecture: Gateway, Orchestration, RAG, and Observability

← Back to Architecting Production LLM Systems: How AI Gateways, Orchestration, RAG, and Observability Fit Together

A reference architecture for turning a single LLM API call into a production system, using an AI gateway, an orchestration layer, retrieval, structured outputs, and observability as five composable layers around every model request.

Overview

Key Concepts

AI Gateway (Routing Layer)

Orchestration (Control Plane)

Structured Outputs & Schemas

export const TicketTriage = z.object({
  category: z.enum(["billing", "technical", "account", "other"]),
  priority: z.enum(["low", "medium", "high", "urgent"]),
  requires_human_escalation: z.boolean(),
});
// schema drives both the model-facing JSON schema and the local re-validation
return TicketTriage.parse(JSON.parse(raw));

Retrieval-Augmented Generation (RAG) & Vector Databases

Observability & Tracing (Langfuse)

Evaluation, Scoring & A/B Testing

Fine-Tuning (Last-Resort Layer)

Trade-offs / Caveats

Example in Practice

A Next.js API route composes all five layers per request: it opens a Langfuse trace, calls a retrieval module (vector DB lookup) inside a "retrieval" span, passes the retrieved context plus the question through a gateway call with tiered model fallback (callWithFallback("balanced", ...)), wraps that in a "generation" span, and returns the answer - with the trace flushed and errors logged at the end regardless of outcome. Each concern (retrieval, gateway routing, tracing) lives in its own module, independently testable and swappable, and the route itself contains only composition and error handling.

Related topics