Modular AI Architectures: Swappable Stages
You'll learn to
- -Split an AI application into stages that each have a written contract
- -Swap one stage for another without touching the rest of the pipeline
- -Explain why a contract protects the shape of the data but not the quality of the answer
- -Decide where a module boundary earns its cost and where it does not
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.
Most AI demos begin as one long function: take the question, call a search, paste the results into a prompt, call the model, print the answer. It works until the day you want a better retriever, a cheaper model, or a safety check in the middle. Then every change touches every line, and nobody is sure what else broke. A modular architecture is the habit of cutting that function into stages you can replace one at a time.
An Analogy: A Kitchen With Stations
A restaurant kitchen has a prep station, a grill, a pastry counter and a pass where plates are checked. Each station takes in a known kind of thing and hands on a known kind of thing. If the grill cook leaves, you hire another grill cook, and the pastry station never notices. If every cook did every job on one crowded counter, losing one person would stop the kitchen. In an AI system the stations are retrieval, generation and checking, and the thing handed along is a plain record of what is known so far.
What a Stage and a Contract Are
- -A stage does one job: retrieve context, generate an answer, check the answer, rewrite the query, redact private data.
- -A contract says what a stage takes in and what it must hand back, for example 'takes a question, returns the same record plus a list of context passages'.
- -A pipeline is stages run in order, each one reading the record the stage before it produced.
Keep the contract small and boring. A record with named fields is enough to start. The point is not the data format, it is that the next stage can rely on a field being there. The code below builds a three-stage pipeline where every stage takes a record and returns a record, and a small runner checks that each stage kept its promise.
Read the three outputs in order. The keyword retriever finds the refunds passage and the answer quotes it. The second run swaps in a poor retriever that always returns the longest document. Nothing in the pipeline had to change, and the contract still held, yet the answer is now about shipping, and the grounding check still says grounded=True because the answer does quote the passage it was given. The third run removes a field the next stage needs, and the runner stops at once with a message that names the stage.
That is the real lesson. A contract catches the failures of shape, such as a missing field or the wrong type, quickly and cheaply. It cannot tell you a swap made answers worse. Only evaluation can, which is why the Evals modules come before this one. Every swap you make should be followed by the same test set.
Where to Cut and Where Not To
- -Cut where you expect to swap or compare: retriever, reranker, model, guardrail.
- -Cut where a different team or a different scaling need lives: a GPU model server and a CPU search service age differently.
- -Do not cut inside a step whose pieces always change together. Two functions that must be edited as a pair belong in one stage.
- -Remember what a boundary costs: a contract to maintain, one more place to log, and, if it becomes a network call, latency and a new way to fail.
Write the contract before the code. If you cannot say in one sentence what a stage takes in and hands back, the stage is probably two stages, or half of one. The same instinct is why each box in the GenAI Lab canvas is one component with one job.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!