Leer en español · Lire en français · Auf Deutsch lesen
Chapter 12 of 12 · Advanced
Architecting AI-Enabled Systems
Systems built around machine-learning models and large language models need everything earlier chapters taught, plus new answers to new questions: where a model should run, how to ground it in your own data, how to let it use tools safely, how to test something probabilistic, how to defend against prompt injection, how to control cost and latency, and what regulation such as the EU AI Act requires. This chapter is for developers and architects who know the rest of the course and must design, review or govern AI-enabled systems that are reliable, secure and affordable.
Video introduction to Chapter 12
8 min · narrated, with captionsListen only (audio)
Read the transcript
Welcome to Chapter 12, Architecting AI-Enabled Systems. It is about systems built around artificial intelligence, or AI: machine learning models, and large language models, or LLMs. They need everything the earlier chapters taught, plus new answers to new questions. Where should a model run? How do you ground it in your own data, let it use tools safely, test something probabilistic, and defend against prompt injection?
This chapter is for developers and architects who know the rest of the course, and who must design, review or govern AI-enabled systems that are reliable, secure and affordable. It builds on the whole course: every quality, style and practice, now applied to probabilistic components. It leans most on quality attributes, data architecture, security and privacy, resilience and observability, and evaluation and fitness functions. Those earlier ideas change shape here. Quality attributes now include accuracy and groundedness. Resilience must cope with rate limits and outages at a model provider. And testing becomes evaluation. This is the last chapter, so it leads on to the final assessment and the capstone case study.
Most of an AI-enabled system is ordinary software. This container diagram from Section twelve point two shows the chapter's running example: a customer support assistant for the food delivery platform. Customers chat through the mobile app, which talks to a chat service. Difficult conversations are handed off to an agent console, where human support agents pick them up. At the centre is the orchestrator, which holds the prompts, the tools and the limits. It searches a help-centre index, and makes tool calls to the existing order and refund services. Every model call passes through an AI gateway, which holds the provider keys, routes each call, and falls back from provider A to provider B. Traces of every call are kept for evaluation. Only the model is new. But its behaviour is statistical rather than specified: usually right, sometimes confidently wrong, and supplied by a provider that may throttle it, change it or retire it. That changes how everything around it must be designed.
The first four sections build the system. Section twelve point one explains what machine learning and large language models are: training and inference, tokens, context windows, sampling and embeddings, and why hallucination and non-determinism are inherent. Section twelve point two places the model in an architecture: hosted, self-hosted or on the device, behind an AI gateway, with routing, cascades, fallback ladders and humans in the loop. Section twelve point three covers retrieval-augmented generation, or RAG, the most common pattern for grounding answers in your own data, from chunking and vector indexes to hybrid search and access control. Section twelve point four moves from answering to acting: tool calling, the agent loop, workflow patterns, the Model Context Protocol, or MCP, and the reliability engineering that agents need.
The second four sections make the system trustworthy and sustainable. Section twelve point five shows how to test probabilistic behaviour, with evaluation sets, metrics, a model acting as judge, and regression evaluations that act as fitness functions. Section twelve point six covers prompt injection, excessive agency and the other risks catalogued by OWASP, and the layered defences that contain them. Section twelve point seven deals with token economics, latency, caching and batching, and the practice of versioning, tracing and monitoring model calls. And Section twelve point eight covers data governance, fairness, transparency and human oversight, and how regulation such as the EU AI Act, and frameworks such as the NIST AI Risk Management Framework, shape architecture.
A language model cannot reliably tell instructions from data, so any text in its context can try to steer it. This diagram from Section twelve point six shows the answer: deterministic layers around an untrusted model. A user's message first passes input guards, then a context builder, which marks untrusted content and pulls in data filtered by the user's own access rights. The model sits in the middle, labelled never trusted. Its output passes output guards, which check the schema and filter what reaches the user. When it asks to use a tool, a tool gateway enforces authorisation and policy. Consequential actions need human approval before they reach the business systems, while read-only calls go directly. And monitoring collects logs, traces and anomaly alerts, with a kill switch. No prompt is a security boundary. These deterministic layers decide what the model can see, what it can do, and what reaches the user.
Two examples recur. The support assistant you have just seen gains retrieval in Section twelve point three and tools in twelve point four, and is evaluated, secured and costed in the sections after that. The second is this course itself. ArchitectBot answers learners' questions by retrieving passages from the course's own chapters, and calls a ladder of models from more than one provider, so that a single outage does not silence it. Written test answers are marked by a model against a fixed rubric.
Section twelve point eight explains that the EU AI Act regulates by risk: prohibited practices, high-risk systems, transparency obligations and minimal risk, with separate obligations for general-purpose AI models. That material is general information, not legal advice. Obligations depend on the facts of each system, and on the exact legal texts and guidance in force, which continue to change. Qualified legal advisers should be involved in any real classification or compliance decision.
Treat the model as a talented, fallible and untrusted component, and let deterministic architecture decide what it sees, what it may do and what happens to its output. Ground its answers in retrieved, permitted facts. Validate everything it produces, and give it the least privilege its task needs. Require human confirmation for consequential actions. Measure its quality continuously, as you would any other quality attribute, and plan for its provider failing. Models will keep getting better, and systems built on this principle get better with them.
The sections follow the running example as it grows, so reading in order works well. But each section stands on its own, so you can go straight to the one you need. Allow about three and a half hours to read the whole chapter. Providers and models are named only as examples, because the market changes every few months while the principles do not. At the end of each section there are self-check questions. Answer them in your head before you look at the answers. When you are ready, there is an optional chapter test, which takes about forty five minutes. It has twenty multiple choice questions and two written answers. The pass mark is eighty per cent.
That is the shape of the final chapter. Start with Section twelve point one, on machine learning and large language models for architects. See you there.
At a glance
You will learn to
- Explain how machine-learning models and large language models are trained and served, and what tokens, context windows, sampling, embeddings and hallucination mean for a system's design.
- Choose where a model should run and how it is called, and design an AI gateway, model routing, cascades, fallback ladders and human review for a given set of requirements.
- Design a retrieval-augmented generation pipeline, including chunking, hybrid retrieval, re-ranking, access control, freshness and evaluation of retrieval and answers.
- Distinguish workflow patterns from autonomous agents, design tools and protocols such as MCP, and apply step limits, idempotency, checkpoints and human approval to agents.
- Design an evaluation strategy for a probabilistic system, using evaluation sets, suitable metrics, LLM-as-judge with safeguards and regression evaluations in the delivery pipeline.
- Identify the main security risks of LLM applications, including direct and indirect prompt injection, and place layered defences so that no single prompt is a security boundary.
- Estimate and reduce the cost and latency of model calls with caching, batching, streaming and cascades, and operate model-based features with versioning, tracing and monitoring.
- Explain how data governance, fairness, transparency, human oversight, the EU AI Act, the NIST AI RMF and ISO/IEC 42001 affect architectural decisions.
Before you start
The whole course, especially Chapter 2 (quality attributes), Chapter 7 (data architecture), Chapter 8 (security and privacy), Chapter 9 (resilience and observability) and Chapter 11 (evaluation and fitness functions).
Time
About 3½ hours to read; 45 minutes for the test.
Key terms
large language model · token · context window · embedding · hallucination · AI gateway · fallback ladder · retrieval-augmented generation · hybrid search · tool calling · agent · Model Context Protocol · prompt injection · LLM-as-judge
A new kind of component in a familiar architecture
Most of an AI-enabled system is ordinary software. A customer-support assistant for a food-delivery platform still has a mobile app, services, databases, queues, identity, logging and a deployment pipeline, and every chapter of this course applies to them. What is new is one component whose behaviour is statistical rather than specified: a model that produces plausible output, usually right, sometimes confidently wrong, at a cost and latency measured in tokens, supplied by a provider that may throttle it, change it or retire it.
That one component changes how the rest must be designed. Quality attributes from Chapter 2 now include accuracy and groundedness. Resilience from Chapter 9 must cope with rate limits and outages at a model provider. Security from Chapter 8 meets a new attack, prompt injection, against which no instruction is a reliable defence. Testing becomes evaluation, and governance from Chapter 11 meets new regulation. This chapter applies the whole discipline to that situation, vendor-neutrally: providers and models are named only as examples, because the market changes every few months while the architectural principles do not.
How the chapter is organised
The first four sections build the system. Section 12.1 explains, for architects, what machine learning and large language models are: training and inference, precision and recall, transformers, tokens, context windows, sampling, embeddings, fine-tuning and why hallucination and non-determinism are inherent. Section 12.2 places the model in an architecture: hosted, self-hosted or on-device; online or batch; behind an AI gateway; with routing, cascades, fallback ladders and humans in the loop; and how to choose a model. Section 12.3 covers retrieval-augmented generation, the most common pattern for grounding answers in your own data, from chunking and vector indexes to hybrid search, access control and evaluation. Section 12.4 moves from answering to acting: tool calling, the agent loop, workflow patterns, the Model Context Protocol, memory, multi-agent systems and the reliability engineering that agents need.
The second four sections make the system trustworthy and sustainable. Section 12.5 shows how to test probabilistic behaviour with evaluation sets, metrics, LLM-as-judge, online experiments and regression evaluations that act as fitness functions. Section 12.6 covers prompt injection, excessive agency and the other risks catalogued by OWASP, and the layered defences that contain them. Section 12.7 deals with token economics, latency, caching, batching and the operational practice of versioning, tracing and monitoring model calls. Section 12.8 covers data governance, fairness, transparency, human oversight and how regulation such as the EU AI Act and frameworks such as the NIST AI Risk Management Framework shape architecture.
Running examples
Two examples recur. The first is a customer-support assistant for the food-delivery platform used throughout the course: it answers questions about orders and policies, looks up order status, proposes refunds and hands difficult cases to people. It appears as a container diagram in section 12.2, gains retrieval in 12.3 and tools in 12.4, is evaluated in 12.5, secured in 12.6 and costed in 12.7.
The second is this course itself. ArchitectBot answers learners' questions by retrieving passages from the course's own chapters with keyword ranking, adding live web results when the course does not clearly cover a question, and calling a ladder of models from more than one provider so that a single outage or rate limit does not silence it. Written test answers are marked by a model at temperature 0 against a fixed rubric, with a check for answers that try to instruct the marker and an option to request a second marking. These are small, real systems with real trade-offs, described honestly, including what they do not do.
The idea to take away
Treat the model as a talented, fallible and untrusted component, and let deterministic architecture decide what it sees, what it may do and what happens to its output. Ground its answers in retrieved, permitted facts; validate everything it produces; give it the least privilege its task needs; require human confirmation for consequential actions; measure its quality continuously, as you would any other quality attribute; and plan for its provider failing. Models will keep getting better. Systems built on that principle get better with them, while systems that rely on the model behaving well become more dangerous as they are given more to do.
The 8 sections
Chapter test
Test your understanding of Chapter 12
Optional: 20 multiple-choice questions and 2 short written answers, drawn afresh from a bank of 72 questions. Pass mark 80%. Passing every chapter test, then the final assessment, earns the Be Wise Certificate of Completion.
Take the Chapter 12 test in the appFurther reading
- AI Engineering: Building Applications with Foundation Models, Chip Huyen (2025), O'Reilly Media. The most complete practitioner book on building with foundation models: evaluation, prompting, RAG, agents, fine-tuning, inference optimisation and system architecture.
- Designing Machine Learning Systems, Chip Huyen (2022), O'Reilly Media. The classic machine-learning systems book: data, features, training, deployment, monitoring and distribution shift, from an engineering point of view.
- Hands-On Large Language Models, Jay Alammar and Maarten Grootendorst (2024), O'Reilly Media. A visual, practical explanation of tokens, embeddings, transformers, semantic search, RAG and fine-tuning, accessible to readers without a machine-learning background.
- Build a Large Language Model (From Scratch), Sebastian Raschka (2024), Manning. For readers who want to see exactly how tokenisation, attention, pretraining and instruction fine-tuning work by building a small model in code.
- Attention Is All You Need, Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser and Illia Polosukhin (2017), NeurIPS 2017 (arXiv:1706.03762). The paper that introduced the transformer architecture behind today's language models.
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, Patrick Lewis and colleagues (2020), NeurIPS 2020 (arXiv:2005.11401). The paper that named retrieval-augmented generation; useful for seeing what the original idea was and how today's practice has broadened it.
- Building effective agents, Erik Schluntz and Barry Zhang (2024), Anthropic. A short, influential essay distinguishing workflows from agents and recommending the simplest pattern that works.
- Artificial Intelligence Risk Management Framework (AI RMF 1.0), NIST AI 100-1, National Institute of Standards and Technology (2023), NIST. A voluntary, widely used framework (govern, map, measure, manage) for identifying and managing AI risks across a system's life.