AI system design drills (D1–D4)

Four worked designs for AI systems (document intake, an LLM gateway, a permissioned RAG assistant, and taking an MVP to production), each with failure modes and metrics.

L4 · Applied AI engineer≈ 5 min read
Key points · the two-minute version
  1. D1 intake: OCR → schema extraction with evidence → deterministic validators → confident cases go through, uncertain ones go to review, plus an audit log.
  2. D2 gateway: auth, tenant budgets (RPM+TPM), PII policy, cache, router with breakers, validated fallback, telemetry.
  3. D3 permissioned RAG: ACLs at ingestion, mandatory filters at retrieval, citations checked, "not found" when evidence is missing.
  4. D4 MVP→prod: auth, Postgres with RLS, queue workers, object storage, live vendor bake-off, metrics.
  5. Always say where the model decides and where code verifies.
You should be able to
  • Design an extraction pipeline with deterministic validation and human review
  • Design a multi-provider LLM gateway
  • Critique your own system honestly and phase its hardening

Why this matters

AI design interviews look like classic ones with three extra questions: where may the model decide, how do you verify it, and how do you know quality holds over time? These four drills cover the patterns behind most real AI products. Use the framework for each.

D1 · Document intake with extraction and human review

Prompt: "Faxed and scanned documents (for example medical referrals, insurance claims or invoices) arrive all day. Extract the key fields, create a record in the system of record, and send uncertain cases to staff."

This shape (intake → extract → validate deterministically → route → human review for exceptions) is behind many commercial document-AI products. The example below uses healthcare referrals on AWS.

 Fax / scan / portal
        │
        ▼
 S3 (KMS, versioned) ──► EventBridge ──► SQS (+ DLQ)
                                          │
                                          ▼
                 Extraction worker (containers or a managed agent runtime)
                  ├─ OCR: text, forms, tables (e.g. Textract)
                  ├─ Model (via a private endpoint), strict schema:
                  │    patient, provider, diagnosis codes, urgency,
                  │    evidence spans, confidence
                  ├─ Deterministic validators: codes exist, provider ID valid,
                  │    patient match ladder: record number → DOB+name → review
                  └─ Guardrails: PII handling, grounding
                                          │
              ┌───────────────────────────┴──────────────────┐
              ▼                                              ▼
   Confident + valid                           Low confidence / conflict
   → system-of-record API (idempotency key)    → human review queue (UI)
              └────────────────► Audit log ◄─────────────────┘
                        (who, what, sources, prompt version, model)

 Cross-cutting: policy on write tools · traces with PII redacted ·
 labelled eval set in CI · per-tenant encryption keys
  • The model proposes; code verifies codes and identifiers (S4).
  • Record matching is the dangerous step: a deterministic ladder, ties go to humans, never auto-link on low confidence (S3).
  • Idempotency keys on writes; dedupe duplicate faxes by content hash + subject + date.
  • Metrics: field accuracy on labelled documents, straight-through rate, review-queue age, cost per document, reviewer correction rate fed back into evals.
  • Failure modes: bad OCR; duplicate submissions; system-of-record outage (queue, retry, never drop).

D2 · Resilient multi-provider LLM gateway

Prompt: "Many teams call several model providers. Build a shared layer that's reliable, safe and cost-controlled."

 Client ─► Gateway (FastAPI)
            ├─ Auth + tenant
            ├─ Redis token buckets (requests + tokens per minute, per tenant)
            ├─ PII policy (redact or block, by endpoint coverage)
            ├─ Cache: exact → semantic (only where allowed)
            ├─ Router: task class → model tier; breaker state per provider
            ├─ Adapters: cloud platform · Anthropic · OpenAI · Gemini · self-hosted
            ├─ Validate → one repair (low-risk only) → validated fallback
            └─ Async telemetry: tokens, cost, latency, prompt version, outcome

Decisions: fallback only between validated prompt/schema pairs; separate connect, first-token and total timeouts; hard per-tenant budgets; streaming passthrough with cancellation; the gateway owns retries so services don't stack their own. Managed alternatives exist (cloud agent gateways with built-in model fallback); know when you'd use them (10.6).

D3 · Policy assistant: RAG with permissions

Prompt: "Staff ask questions about internal policies. Different roles may see different documents. Answers must be traceable."

Ingestion: scan → parse → structure-aware chunks → version → attach ACL labels → embed → index (vectors + lexical + metadata).

Query: authenticate → resolve role filters → hybrid retrieval with mandatory permission filters → re-rank → bounded context → answer only from sources with chunk IDs → citation check → stream with sources → feedback and audit.

Tables: documents, document_chunks, access_policies, ai_runs, feedback.

Insufficient evidence returns "I couldn't find support in the approved sources", never a guess. See Q1, Q8, C4.

D4 · Take an MVP to multi-tenant production (self-critique)

Prompt: "What would you change in your own project before real customers use it?" Having an honest answer is the point. The worked example is the casebook's clip service (P1), starting from its honest status.

Today Production version Why
No auth Auth with tenants and roles Nothing else matters until this exists
SQLite (WAL) on one box Managed Postgres with tenant_id + RLS Concurrency, backups, isolation
In-memory thread job tracker Queue + workers, job state in the DB, idempotent jobs Survives restarts; scales rendering independently
Local disk + optional storage sync Object storage as the source of truth, presigned uploads No data on a single instance
Vendor adapters never run live Run the bake-off on the golden set, choose an engine per language from measured error rates Measure first
Placeholder thresholds Tune from editor verdicts Numbers come from data
Logs only Metrics: render time, queue age, vendor errors by class, cost per clip, acceptance rate The four AI axes plus product

Failure modes to name: a vendor outage mid-batch (resume from saved outputs, never re-pay); a render stuck on one worker (timeouts, visibility timeout, DLQ); a consent flag missing on an item (the pipeline refuses, by design).

Check yourself

In D1, why not let the model write directly to the system of record?

Because extraction errors (wrong patient, wrong code) have real consequences. The model proposes structured fields with evidence; deterministic validators check codes and identifiers; matching follows a ladder; low-confidence or conflicting cases go to humans; writes are idempotent and audited.

In D2, why fall back only between validated prompt/schema pairs?

Prompts and structured-output behaviour differ across models. An unvalidated fallback can return a different shape or quality, turning an outage into silent wrong answers.

In D3, where must permission filtering happen?

In server code during retrieval, before any chunk reaches the model, with RLS as a second wall and cache keys scoped by authorization context. The client never supplies the filter.

Build it

Draw D1 from memory in five minutes and name three failure modes before looking. Then write D4 for one of your own projects.

What comes next

Part 7: LLM foundations, the start of the applied AI layer.

Build it to remember

Two hands-on projects that exercise this lesson. Pick one, build it end to end, and keep it in your portfolio.

Project 1

Mini document intake (D1)

Upload invoices, extract fields with evidence, validate totals and dates, auto-approve confident ones and review the rest.

  • FastAPI
  • Tesseract or Textract
  • an LLM API
  • Postgres
  • React review UI

Practises The extract-validate-review pattern.

Project 2

Permissioned policy assistant (D3)

Two roles see different documents; answers cite chunk IDs that are verified server-side; "not found" when unsupported.

  • Next.js
  • FastAPI
  • pgvector
  • Postgres RLS
  • an LLM API

Practises Tenant-safe RAG with citations.

Can you answer every check-yourself question out loud without opening the answer?