Architecture for a live AI feature.
Connect interfaces, orchestration, retrieval, model adapters, validation, traces, and fallbacks.
Start with the meaning.
A live AI feature is a chain of services and decisions. The interface collects a request. A server authenticates the user and checks permissions. An orchestration layer chooses evidence and builds the prompt. A model adapter calls the chosen service. Validation checks the response, and the interface presents either a reviewed candidate or a controlled fallback. Logs and traces help the team understand what happened.
Keeping these responsibilities separate makes failures diagnosable. If the wrong document was retrieved, a prompt change is unlikely to fix the source problem. If the model returns invalid JSON, the adapter or output contract needs attention. If the user sees another person's data, access control has failed and the model's wording is irrelevant.
How the idea works.
Design adapters around a stable internal response shape. This allows a model version or provider to change without rewriting every page. Record request IDs, timing, model version, prompt version, document IDs, validation result, and user-visible status. Avoid logging raw sensitive prompts unless there is a justified, controlled need.
A fallback is a product behavior. It might show a search result instead of a generated answer, ask for clarification, or route the case to a human. Define it for absent evidence, model outage, invalid output, and permission denial. The fallback should preserve the user's work and explain what happened in plain language.
Long-running tasks may need queues and job status rather than an open browser request. Idempotency matters when a client retries: a generated draft can be repeated harmlessly, while an external action may be duplicated. Separate generation from execution so a retry cannot accidentally send two messages or create two records.
A request trace should let an operator reconstruct a failure without exposing more user data than necessary. Record the request identifier, task and prompt versions, model version, retrieved source IDs, validation outcome, stage timings, and user-visible status. If the answer is wrong, the team can then ask whether the wrong document was selected, whether the model ignored a good passage, or whether validation failed to catch the claim. Without stage visibility, every incident becomes guesswork.
Fallbacks need interface copy as well as server logic. “No supported source was found” tells a user something actionable; a blank card or endless spinner does not. If a model is unavailable, offer the underlying search or preserve the draft request for later. If a generated action needs review, show the exact proposed effect. Reliability is experienced at the user interface, even when its mechanisms live in the backend.
Follow the code.
def handle(request, auth, retrieve, model, validate):
user = auth(request)
evidence = retrieve(request["question"], user=user)
if not evidence:
return {"status": "no_evidence", "answer": None}
candidate = model(request["question"], evidence)
if not validate(candidate, evidence):
return {"status": "needs_review", "answer": None}
return {"status": "ready", "answer": candidate}The sequence is deliberate: authenticate before retrieval, retrieve with the authenticated user, then generate and validate. The returned status lets the interface distinguish a supported answer from a controlled failure. The example omits exception handling and logging so the responsibilities remain visible; a production implementation would add both around the stages.
The validate function receives the evidence, allowing it to check citations against the passages supplied to the model. A stronger validation may require a human or a separate factuality check.
What the result does not say.
An application can be reliable with a modest model and careful architecture. Operational clarity often matters more than a complicated chain of prompts.
Design observability to answer specific questions: which stage failed, for which users, after which change, and with what cost? Collect only the data needed for that diagnosis.
One clear idea.
A reliable AI feature has explicit boundaries between permission, evidence, generation, validation, and presentation.
The AI cookbook
Complete, explained local workflows sit alongside this reading course.