InterviewKata
Software architecture · Level 4: Communication and integration · Module 22b of 46

AI and LLM components in an architecture

Large language models arrived in most products as "just another API call". Architecturally they're stranger than that: the answer to the same request can differ every time, quality can't be checked with an equality test, latency is seconds, cost scales with words, and the provider may change the model under you. This module treats an LLM feature as a set of components with characteristics, and shows where the architectural decisions are.

1. The idea

Hiring a brilliant contractor who works by the hour, is occasionally confidently wrong, and sometimes sends a different person than last time. You wouldn't let them into every room. You'd give them a clear brief and the right documents, check their work against known answers, route simple jobs to a cheaper colleague, and keep a second contractor's number for when the first doesn't pick up.

The components that keep showing up around a model call:

ComponentIts jobArchitectural reason
Model gatewayOne internal entry point for all model calls: authentication, routing, fallbacks, rate limits, caching, cost tracking, loggingIsolates the volatile provider APIs behind a stable port (Module 07a); one place for policy
Prompt and configuration registryPrompts, model choices and parameters as versioned artifacts, deployed like codeChanging a prompt is a release: it needs review, rollback and an audit trail
Retrieval (RAG)Find relevant documents and put them in the promptGrounds answers in your data; it's a data architecture: ingestion, chunking, an index, access control, freshness
GuardrailsChecks on input and output: personal data, prompt injection, unsafe content, schema validationThe model is not a trust boundary (Module 28a)
Evaluation pipelineScore outputs against test sets and judges, on every changeEvals are the fitness functions of AI features (Module 26)
Tool accessThe model asks to call functions; your code decides and executes them with least privilegeAn agent with broad credentials is a security architecture problem

New characteristics to name

2. How it works

A support assistant answers one question

A customer asks about a refund. Watch the request pass through each component, including a provider outage.

3. Try it yourself

Route, cache, and keep quality

Two models behind a gateway: a small one (cheap and fast, weaker on hard requests) and a large one. Set how many requests are easy, how much traffic the router sends to the small model, and the cache hit rate.

Assumed figures to show the shape, not any vendor's prices: 1 million requests a month, 2,000 tokens each; small model $0.40 and large model $4.00 per million tokens; 0.6 s and 2.0 s typical latency; pass rates on the eval set: small 98% on easy and 60% on hard requests, large 99% and 92%. The router sends easy requests to the small model first.

4. Trade-offs

Quality vs cost and latency

Bigger models are better on hard tasks and cost several times more per token and per second. Routing, caching and shorter prompts move the curve; evals tell you how far you can go.

Portability vs features

A gateway with a common interface makes switching providers cheap, but hides provider-specific features (tool formats, caching options). Abstract the call, not every capability.

Hosted vs self-hosted models

Hosted APIs need no GPUs and improve constantly; self-hosted open-weight models give control over data, cost at high volume and stability, at the price of running GPU infrastructure.

Autonomy vs control

An agent that acts on its own does more; every tool it can call widens the blast radius. Confirmations, narrow tools and audit logs are the architectural brakes.

5. In real projects

Put a gateway in front of every model call from the first feature. It's where you add a second provider, a cache, a budget limit or a new logging rule without touching every service.

Treat prompts and model versions as deployable configuration: versioned, reviewed, rolled out gradually and rolled back, with the eval suite as the release gate.

RAG quality is mostly data quality: what's indexed, how it's chunked, whether permissions follow the documents, and how fresh the index is. Retrieval must apply the user's access rights, or the assistant leaks documents the user couldn't open.

Log the whole trace (prompt version, retrieved documents, model, tokens, latency, guardrail decisions), with personal data handled under the same rules as any other user data.

Know the risks by name: the OWASP Top 10 for LLM applications (prompt injection, sensitive information disclosure, excessive agency and others) is a useful checklist in reviews, and the NIST AI Risk Management Framework for governance.

Say it in the interview

Two-sentence answers
  • "How would you add an LLM to this system?" Behind an internal model gateway, with prompts versioned as configuration, retrieval for grounding, guardrails on input and output, and an eval suite as the release gate.
  • "How do you control the cost?" Route easy requests to smaller models, cache repeated answers and prompt prefixes, trim context, and track cost per request as a characteristic with a budget.
  • "How do you know it works?" An eval set of real cases scored on every change, plus production sampling and user feedback; quality is a measured number with a target.
Red flags interviewers notice
  • Every service calling a provider's SDK directly with its own key.
  • "The model will refuse bad requests" as the security plan.
  • No way to tell whether a prompt change made things better or worse.

6. Remember it

Memory hook: "Gate it, ground it, guard it, grade it"A gateway in front, retrieval for facts, guardrails around, evals to measure.
How this hook works: what "gate it, ground it, guard it, grade it" means

What the slogan means, phrase by phrase

"Gate it"
One model gateway for routing, fallbacks, limits, caching and cost.
"ground it"
Retrieval brings your data into the prompt, with the user's permissions applied.
"guard it"
Checks on what goes in and comes out; tools with least privilege.
"grade it"
Evals on every change: quality as a measured characteristic.

Why this picture fits: it's how you'd manage the brilliant, hourly-billed, sometimes-wrong contractor: a front desk, a briefing pack, a supervisor and a review of their work.

The story → the concept

The contractorThe LLM feature
The front desk that books themThe model gateway
The briefing packRetrieved documents in the prompt
Only the keys they needLeast-privilege tools and guardrails
Checking work against known answersThe eval suite
A cheaper colleague for simple jobsRouting to a smaller model

The trap it saves you from

A demo that works on stage and becomes an unmeasured, unbudgeted, unsecured dependency in production.

7. Common mistakes

No evalsPrompt changes ship on gut feeling; regressions are found by customers.
Provider SDKs everywhereSwitching models, adding a fallback or a cache means editing every service.
Retrieval that ignores permissionsThe assistant quotes documents the user isn't allowed to see.
The biggest model for everythingMost requests are easy; paying top prices and waiting seconds for them is a design choice, usually a bad one.

8. Design exercise (30 minutes)

  1. Components (10 min). Pick an AI feature in a product you know. Draw the gateway, retrieval, guardrails, evals and tools, and who owns each.
  2. Characteristics (10 min). Write targets for quality (eval pass rate), latency (time to first token), and cost per request.
  3. An ADR (10 min). Hosted API or self-hosted model? Write the decision with its drivers and the revisit trigger (Module 27).

9. Self-check quiz

Why put a model gateway in front of LLM calls?

A stable port in front of a fast-changing dependency.

What plays the role of fitness functions for an AI feature?

Non-deterministic output needs criteria-based scoring.

How do you stop a RAG assistant revealing documents a user can't open?

Permissions belong in the data path, not in the prompt.

How should a prompt change be released?

Prompts change behaviour as much as code does.

10. Go deeper

ResourceTimeWhy
Emerging Patterns in Building GenAI Products (martinfowler.com)60 minRAG, guardrails, evals and more, as patterns.
OWASP Top 10 for LLM Applications30 minThe main risks, for architecture and security reviews.
NIST AI Risk Management FrameworkreferenceGovernance language for AI risk.
The AI engineering trackcourseModules 14–19 cover model APIs, RAG, agents, evals, security and cost in depth.
D23 LLM chat platform and D31 AI support assistant90 min eachWorked designs that use these components.