0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09From an empty blueprint to a production AI system
Back in episode fifty-two, we unrolled a blueprint for Brightcart's AI assistant with empty boxes: prompts, policies, order tools, evaluation, guardrails, monitoring. Since then, we've built nearly every one of those pieces; monitoring comes next episode. Now, in the final module of the course, we put them together. And we'll do it the way real systems grow: starting from the simplest possible design, a customer's message sent straight to a model, and adding one component at a time, each one solving a specific problem.
Season Five, Module Twenty-Two, the final module: architecture and feedback. First stop: the full architecture.
0:58Step 1: build the context
Step one: give the model the right context. The customer's message is rewritten into a clear, standalone query; hybrid retrieval and a reranker fetch the relevant policy passages; tools fetch the order record; the customer's saved preferences come from memory. All of it is assembled into a prompt with the stable instructions first and the changing material last. This is what stops the assistant answering from memory and inventing policies.
1:29Step 2: input and output guardrails
Step two: guardrails, on the way in and on the way out. On the way in: mask personal data before it goes to any external model, detect prompt injection and jailbreak attempts, apply rate limits, and politely decline requests that are out of scope. On the way out: validate the format, check for unsafe content, verify that every policy claim is supported by the retrieved passages, enforce business rules, like discount codes only coming from the pricing system, and confirm that any action the reply mentions really happened.
When a check fails, retry, fall back to a safe template, or hand over to a person. There's a tension with streaming. Checking a whole reply before showing it adds delay, but streaming shows words before checks are finished. Common compromises: check incrementally as text streams, or hold back only replies flagged as high-risk.
2:29Step 3: the model router
Step three: a router. Not every question needs the most powerful model. A small, fast classifier reads each request and decides where it goes. Common questions, like opening hours, go to a cache or a template. Routine order and returns questions go to a small, inexpensive model with Brightcart's voice adapter. Complex billing disputes and multi-step problems go to a larger model, or a reasoning mode. And safety-critical cases or large refunds go to a person. The savings can be large. If eighty percent of requests go to a model costing one unit and twenty percent to one costing ten, the average is two point eight units, seventy-two percent cheaper than sending everything to the large model.
But routers make mistakes in both directions: a hard question sent to the small model loses quality, and an easy question sent to the large model wastes money. Evaluate the router like any other component.
3:30The model gateway
Alongside the router sits a model gateway, a kind of control tower between the application and every model it uses, whether self-hosted or from an API. It gives the application one consistent interface, so models and providers can be swapped without rewriting code. It holds the access keys in one secure place. It enforces rate limits and budgets. It falls back to a backup model if a provider is down or slow. It retries sensibly. And it logs every call, with its cost and timing, which feeds the monitoring we'll build next episode.
4:08Step 4 and 5: caches and agent actions
Step four: caches. A prefix cache in the serving layer, an exact cache for frequently asked questions, and a carefully tuned semantic cache that respects freshness and personal data. Step five: actions. The agent's write tools sit behind confirmation and approval gates, with idempotency keys, step limits and loop detection, all tested in a sandbox before every release. And every component costs time, so the team sets a latency budget, a target for each slice, and runs independent steps in parallel. Retrieval and the order lookup, for example, can happen at the same time.
4:52One message through the whole system
Here's the whole journey of one message. A customer writes in. The gateway checks access and rate limits. Input guardrails mask personal data and scan for manipulation. The router sees a routine returns question and chooses the small model. The query is rewritten; policies are retrieved and reranked while, in parallel, the order is looked up and the customer's preferences are loaded. The prompt is assembled, with its shared beginning already cached. The small model, with Brightcart's voice adapter, quantised and accelerated, writes the reply.
Output guardrails check the format, safety and every policy claim. The reply streams to the customer, offering to start a return. The customer confirms; the return is created exactly once, and the system itself shows the confirmation. Everything is logged, and a sample goes to the judge and to Sana's team for review. Every box in that journey is something you've learned to build in this course.
5:54Start simple, add complexity when needed
One last principle: don't build all of this on day one. Real systems grow as measured problems appear. Start with a model and a good prompt. Add retrieval when answers invent policies. Add output guardrails when unsafe or ungrounded replies slip through. Add tools when customers need actions, a router when costs climb, caches when speed or cost demand them, and agent actions when the resolution rate needs them. Every component adds delay, cost, new ways to fail and maintenance, so add each one when the evaluation shows the need, and evaluate it as a component.
6:35Try it yourself: a different router mix
Normally, the router sends eighty percent of tickets to the small model and twenty to the large one, about seventy-two percent cheaper than always using the large model. After a product recall, harder tickets increase, and the split becomes seventy-thirty. What's the saving now? Pause.
Seven tenths times one, plus three tenths times ten: three point seven, so about sixty-three percent cheaper than always using the large model. Still a big saving, but it shows why the router's mix is something to watch: costs follow the kinds of questions customers ask.
7:17Pause & Predict: when the router is wrong
Twenty percent of tickets are hard, and the router recognises ninety percent of them. So two percent of all tickets reach the small model when they shouldn't. How should the system protect those customers? Pause. With the output guardrails. If the small model's answer fails the groundedness or policy checks, the system retries with the large model, or escalates to a person. And every misrouted case becomes a training example for the router. No component needs to be perfect when the next one is watching.
7:57Try it yourself: retries and failure rate
Five percent of drafts fail an output check. A single retry, with the reason for the failure added to the prompt, fixes eighty percent of those. What share of drafts still fail? Pause. One percent: a fifth of the five percent. Those go to a person, along with the draft and the reason it failed, so the agent can fix it quickly. And because only failing drafts are retried, the extra cost is small.
8:31Fallbacks and availability
What happens when a model provider has an outage? Brightcart's gateway falls back to a second provider. If each is available ninety-nine and a half percent of the time, and their failures were independent, both would be down together only a tiny fraction of the time, giving about ninety-nine point nine nine seven five percent availability. In reality, failures can be linked, say by a shared cloud region, so there's always a final fallback: a polite message saying a colleague will reply soon.
9:03Guardrails and streaming don't mix easily
Here's a real design tension. Streaming shows words as they're generated, but an output guardrail wants to see the whole reply before approving it. Brightcart could stream and check sentence by sentence, stopping if something goes wrong. It could hold every reply until it passes, which means a longer wait. Or it can do both: stream low-risk answers, and hold high-risk ones, like refund decisions and policy statements, until they've been checked. Brightcart chooses that last option.
9:37Write actions, done safely
Actions that change things follow a strict path. The model proposes creating a return. Ordinary code checks the business rules: is the item eligible? The customer confirms in the interface. The action runs, with an idempotency key so it can't happen twice. The result is logged, and the reply quotes the real return number from the system. The model suggests, the rules check, the customer confirms, and only then does the system act.
10:09From version 0 to version 4
Brightcart didn't build all of this on day one. Version zero was a prompt and an order lookup, producing drafts for agents. Version one added retrieval with citations, and the evaluation pipeline. Version two added guardrails and a gateway with fallbacks. Version three added the router, caching, and automatic replies for order-status questions. Version four added tools for returns, memory and the fine-tuned voice. Every step was triggered by a measured problem, and every step had to pass the evaluation gates.
10:45Pause & Predict: which component fixes it?
Final exercise: which component would you investigate for each symptom? Answers quote an old policy. Costs doubled last week. A customer saw someone else's address. Replies take six seconds to start in the evening. The assistant promised a refund it never made. Pause.
The old policy: index freshness. Doubled costs: the router mix or a growing prompt. Someone else's address: retrieval permissions and the guardrails. Slow evening starts: serving capacity and queues. The promised refund: the check that compares claims against the action log. When you understand the architecture, every symptom points somewhere specific.
11:34Where the secrets live
Where do the secrets live in this architecture? Never in the prompt. API keys and database passwords stay inside the gateway and the tool services. The model only ever sees tool names and parameters. When it asks to look up an order, the tool runs with the permissions of the current, verified customer, never with an all-powerful admin key, and every call is logged. Even a fully compromised prompt can't reveal a key the model never had.
12:06Myth vs Fact
Myth: an AI product is its model. Fact: the model is the engine, and the product is the whole vehicle: context, guardrails, routing, caching, actions and monitoring. Most of the reliability, and much of the cost-efficiency, comes from everything around the model.
12:26Recap
Let's recap. A production AI system builds context by rewriting queries, retrieving and reranking passages, calling tools and loading memory. Guardrails check inputs for personal data, manipulation and scope, and outputs for format, safety, groundedness and real actions. A router sends each request to the cheapest model that can handle it, and a gateway manages every model call. Caches and gated agent actions complete the picture, all within a latency budget. And components are added only when evaluation shows they're needed.
One sentence to remember: ground it, guard it, route it, cache it, and add each piece only when the numbers ask for it.
13:13Next: monitoring and observability
Mission complete. Brightcart's assistant has a full production architecture, and every box in the original blueprint is filled, apart from monitoring, which we build next. But launching is the start, not the end. Once it's live, how do you know, every minute of every day, that it's still working: still accurate, safe, fast and affordable? Monitoring, observability and orchestration, next. See you on the map.