AI Engineering for Builders
Chapter 6 of 6~25 min read

Production AI Architecture

You'll leave with: A pre-flight checklist for shipping AI features to production.


The gap between a demo and a system

Getting a model to produce a good response is a solved problem. You have an API key, a decent prompt, and ten minutes. The demo works.

Shipping something that works reliably for a thousand users a day — that stays within budget, degrades gracefully when the model behaves unexpectedly, catches bad outputs before they reach users, and gets measurably better over time — is a different engineering problem entirely.

This chapter is about that gap. Not theory — a concrete architecture that production AI applications actually use. Five patterns that solve the real problems: getting relevant context in, keeping bad outputs out, routing intelligently across models, avoiding unnecessary costs, and composing multiple steps into systems that hold together.


The production pattern

A production AI application isn't a single API call. It's a pipeline with distinct stages, each responsible for a different quality problem:

User input
    ↓
[1] INPUT GUARDRAILS — block before it gets to the model
    ↓
[2] CONTEXT ENHANCEMENT — add what the model needs to know
    ↓
[3] MODEL ROUTING — send to the right model
    ↓
[4] GENERATION — the API call
    ↓
[5] OUTPUT GUARDRAILS — validate before it reaches the user
    ↓
[6] CACHING — store for reuse
    ↓
User sees response

Each stage is optional depending on your application. A simple internal tool might skip input guardrails. A low-traffic application might skip caching. But for any user-facing application at meaningful scale, each of these stages addresses failure modes that will hit you in production.


Stage 1: Input guardrails

Input guardrails run before the model sees anything. Their job: catch inputs that will produce bad outcomes — either because they're off-topic, because they're adversarial, or because processing them at full capability would be wasteful.

Topic enforcement. If your application is a customer service bot for a software product and a user asks for medical advice, don't let that query hit your primary model. A fast, cheap classifier can reject off-topic inputs at a fraction of the cost of a full model call. The classifier doesn't need to be sophisticated — a small model or even a keyword-based filter catches obvious cases. For borderline cases, fail toward the model: a false rejection is worse than a false pass.

Toxicity and safety filtering. For consumer applications, filter explicit or harmful inputs before they reach the generation stage. Most providers offer dedicated safety classifiers — OpenAI's Moderation API, for example — that are faster and cheaper than running the primary model for classification.

Length and format validation. Reject inputs that exceed your context budget before building the full context. A user who pastes a 200,000-token document into a 4,000-token input field isn't going to get a useful response. Catch it early.

Input guardrails add a small amount of latency in exchange for substantial cost savings and consistent behavior at the edges. The economics are straightforward: a guardrail that runs for $0.0001 per call and blocks 5% of inputs from hitting a $0.01-per-call model pays for itself quickly.


Stage 2: Context enhancement

Context enhancement is where your application injects the information the model needs but doesn't have. This is the stage where RAG lives — but it's broader than retrieval.

Retrieval. For knowledge-grounded applications, this stage fetches relevant documents from your vector store and formats them for injection. The retrieval pipeline from Chapter 5 plugs in here.

User context. Many applications produce better responses with user-specific information: their tier, their history with your product, their stated preferences, their previous messages in the session. This doesn't require retrieval — it comes from your database. Query it before calling the model, not inside the prompt.

Tool results. For applications that need to call tools — a calculator, a database query, a web search — the tool call happens here, and the results are injected into context before generation. This is different from agent patterns (covered below) where the model decides which tools to call. In simple tool use, your application logic decides what to run and injects the results deterministically.

Formatting matters. How you format injected context affects generation quality. A few principles:


Stage 3: Model routing

Not every query needs your best model.

A typical production request distribution looks like this: 60–70% of queries are straightforward — common questions, simple tasks, low-stakes requests that a fast, cheap model handles well. 20–30% are moderately complex. 5–10% require the full capability of your frontier model.

If you route all queries to GPT-4o, you're spending frontier-model cost on queries that GPT-4o mini would handle equally well. At scale, this is significant: GPT-4o costs approximately 16x more per output token than GPT-4o mini. Routing 70% of your traffic to the cheaper model at equivalent quality changes your unit economics substantially.

How to route:

Rule-based routing classifies queries by type and directs them to pre-assigned models. Simple, predictable, fast. Works when your query distribution has distinct categories with known complexity levels.

Model-based routing uses a small classifier to predict query complexity, then routes accordingly. More flexible, but adds a classification step. The classifier itself needs to be fast and cheap — a routing model that costs as much as the cheaper destination defeats the purpose.

Cascade routing sends every query to the fast model first, checks confidence or quality metrics, and escalates to the capable model only when the fast model's response fails a quality threshold. This is effective but adds latency on escalated queries.

Whichever approach you use: evaluate routing decisions with your eval set. A routing classifier that misclassifies 20% of complex queries as simple will produce hard-to-debug quality degradation.


Stage 4 and 5: Caching and output guardrails

Caching

LLM API calls have two properties that make caching valuable: they're expensive, and the same (or similar) queries often repeat.

Exact caching stores responses by request hash. Identical prompt + parameters → cache hit. This is most useful for common static queries: FAQ answers, templated content, responses to high-frequency questions your user base regularly asks. Simple to implement, high hit rate on predictable queries.

Semantic caching embeds incoming queries and checks for similar queries in cache. If a query is semantically similar enough to a cached query (above a similarity threshold), return the cached response without a model call. This extends cache coverage to paraphrases: "What are your cancellation policies?" and "How do I cancel my subscription?" may hit the same cached response.

Semantic caching requires careful threshold tuning. Set the similarity threshold too low and you return cached responses for queries that deserve fresh ones. Set it too high and you get low hit rates. Test against your production query distribution to find the right threshold.

Prompt caching, offered by Anthropic and increasingly by other providers, caches the KV state of repeated prompt prefixes at the inference layer. If your system prompt and large document context stay constant across many calls, you pay the input token cost once and get a discount on subsequent calls with the same prefix. At $3.00 per million input tokens for standard Claude 3.5 Sonnet vs $0.30 for cached tokens, this matters when you're injecting a 20,000-token knowledge base into every call.

Output guardrails

Output guardrails run after generation, before the user sees anything. Their job: catch responses that are harmful, off-format, factually grounded incorrectly, or otherwise unsuitable.

Schema validation. If you're using structured outputs, this is a hard check: does the response conform to the expected schema? Well-implemented structured output APIs (OpenAI, Anthropic) guarantee schema conformance, making this trivial. Without structured outputs, you need a parser that handles malformed responses gracefully — either by retrying the call or returning a safe fallback.

Hallucination detection. For RAG applications where responses should be grounded in retrieved documents, an output guardrail can verify that claims in the response are supported by the source documents. This is typically another model call — a grounding check — that adds latency but catches the failure mode that caused the Air Canada incident.

Sensitive data detection. Check that the model hasn't included information from one user's context in a response destined for another user. This is a data leak failure mode that's easy to miss in single-user testing and catastrophic in production.

Retry logic. When output guardrails fail, your options are: retry the generation call, return a fallback response, or escalate to a more capable model. Define these policies explicitly. Retrying indefinitely is expensive; never retrying means guardrail violations surface to users.


Agent patterns

An agent is a system where the model decides what actions to take, not just what text to generate. Instead of calling tools deterministically and injecting results into context, you give the model access to tools and let it decide whether and how to use them.

The simplest agent loop:

1. Model receives task + available tools
2. Model decides: call a tool, or produce final answer
3. If tool call: execute tool, inject result into context, go to step 2
4. If final answer: return to user

This unlocks tasks that can't be completed in a single model call: searching the web and synthesizing results, writing code and running it, querying a database and interpreting the results, multi-step research workflows.

When agents are appropriate:

When agents are not appropriate:

Agent failure modes:

Agents fail in ways that pipelines don't. A pipeline fails at a predictable step. An agent can fail by choosing the wrong tool, misinterpreting a tool result, entering a loop, or taking actions you didn't anticipate.

Mitigations:


Monitoring and observability

Standard application monitoring — uptime, latency, error rates — is necessary but not sufficient for AI applications. A system can have 100% uptime, sub-second latency, and zero 5xx errors while producing consistently bad responses. None of your infrastructure metrics will tell you.

What to monitor:

Output quality. Route a sample of production responses through your AI judge pipeline from Chapter 3. Track pass rate over time. A sustained drop in quality — even a few percentage points — tells you something changed: a model update, a distribution shift in inputs, a prompt regression.

Failure category distribution. When outputs fail your quality check, classify why. Are failures concentrated in a specific input type? A specific topic area? A specific time of day correlating with a different user demographic? Patterns in failures point to specific engineering fixes.

Latency by component. Measure latency at each stage of your pipeline, not just end-to-end. A retrieval system that was fast last week and is slow this week has a different root cause than a model call that's slow. You can't diagnose what you can't separate.

Cost per query. Track token consumption per request. A spike in input tokens — maybe users are pasting large documents, or your context enhancement is injecting too much — shows up here before it shows up in your invoice.

Closing the feedback loop

Monitoring without action is reporting. The loop closes when production observations change what you build.

Concretely:

  1. When a low-quality output reaches a user and they signal it (thumbs down, explicit complaint, session abandonment), capture the full trace: input, context, response.
  2. Review captured failures. Cluster them by failure type. The first few you look at are almost always enlightening — they represent failure modes you didn't anticipate.
  3. Route confirmed failures into your development evaluation set. These are now test cases. Any future change needs to pass them.
  4. When you identify a pattern — a class of input your prompt handles badly, a retrieval failure on a document type, an output guardrail gap — fix it and verify the fix with your eval set before deploying.

This is the compound loop. Each production failure makes your evaluation set more representative. Each improvement to your evaluation set makes future changes more reliable. Systems built on this loop improve continuously. Systems without it drift.


A pre-flight checklist

Before you ship an AI feature to production:

Evaluation

Interface layer

Pipeline

Model

Monitoring


Where to go from here

You've now got the mental model, the infrastructure framework, and the decision tools for every major AI engineering problem.

To go deeper on any of these areas, Synapse has full curriculum tracks on evaluation, prompt engineering, RAG architecture, and production observability — each building from the foundations in this course to the engineering depth you need to ship real systems.

The gap between AI demos and AI products is evaluation, iteration discipline, and production infrastructure. You have the framework. Build the thing.


Primary sources: Chip Huyen, AI Engineering (O'Reilly, 2025), Chapters 9–10. Anthropic documentation on prompt caching (2024). OpenAI documentation on structured outputs and moderation API (2024).


← Previous
RAG vs Finetuning — The Decision Framework

Go deeper with the community

This course is the mental model. The community adds production code repos, AI in a Shell (structured learning app + AI tutor), weekly engineering calls, and monthly 1:1 sessions.

Join communityFirst 15 members free · $29/mo after