TechByteByByte

GenAI Design Patterns

Recurring, reusable architectural patterns that combine everything covered across this entire course into proven, practical solutions — starting Level 8: Advanced.

#Generative AI#AI#Design Patterns#Level 8

Start with the simple idea

A design pattern is a reusable starting structure for a problem that appears often, such as retrieving evidence before generating.

Simple learning path: problem → intuition → mechanism → example → limits

What you will learn

  • Explain GenAI Design Patterns in plain language.
  • Follow its mechanism step by step.
  • Connect a small example to a real AI system.
  • Recognize its strengths, limits, and common mistakes.

How this appears in current AI systems

These patterns are portable across GPT, Gemini, Claude, hosted media models, and open Hugging Face pipelines. Provider features change, so the pattern should be tested against the exact model and version used.

Official grounding: OpenAI provides an evaluation guide, while Google documents Gemini safety settings. These sources support the evaluation and safety practices here; neither makes an AI application automatically correct or safe.

When this knowledge helps

Use GenAI Design Patterns when it matches the problem described below. Before choosing it, check the task, available data, quality target, cost, response time, privacy, and safety needs; popularity alone is not a reason to use it.

1. The question this module answers

Level 8 is the final, synthesizing level of this course. This module gathers the recurring architectural patterns that have appeared, piece by piece, throughout Levels 6-7, into named, reusable design patterns — the kind of proven solutions an experienced GenAI engineer reaches for repeatedly across different applications.


2. Pattern 1 — The Grounded Generation Pattern

This is Module 28's RAG mechanism, named as a general PATTERN:

Query -> RETRIEVE relevant context (Module 11's latent space search)
      -> CONDITION generation on retrieved context (Module 12)
      -> GROUNDED output (Module 32's hallucination mitigation)

Use when: the application needs current, specific, or verifiable
         information the base model doesn't already reliably know

3. Pattern 2 — The Classify-Then-Generate Pattern

This is Module 1's opening example, and Module 5's discriminative/
generative distinction, combined into a reusable PATTERN:

Input -> DISCRIMINATIVE step (classify into a fixed category,
        Module 5) -> GENERATIVE step, CONDITIONED on the
        classification result (Module 12)

Use when: the appropriate generation behavior really DEPENDS on
         first determining which of several known categories the
         input falls into (e.g., urgency level shaping response
         tone, Module 1's customer support example)

4. Pattern 3 — The Agent Loop Pattern

This is Module 29's agent mechanism, named as a general PATTERN:

Generate -> [tool requested? EXECUTE -> feed RESULT back as
           context] -> generate again -> REPEAT until complete
           (with a MAX STEPS safety limit, Module 29's Example 3)

Use when: a task really requires MULTIPLE sequential steps,
         real-world actions, or information the model doesn't
         already have in its initial context

5. Pattern 4 — The Verify-Before-Trust Pattern

This is Module 18's code-generation workflow AND Module 31's
evaluation practices, generalized into a PATTERN:

Generate output -> MECHANICALLY VERIFY where possible (run tests,
                   check format/schema, Module 18) OR check GROUNDING
                   (Module 32's grounding check) -> FLAG for human
                   review if verification FAILS, or if the task is
                   really HIGH-STAKES (Module 33)

Use when: generated output will be used in a way where INCORRECTNESS
         carries real, genuine consequences (code execution,
         financial/medical/legal content, autonomous actions)

6. Pattern 5 — The Tiered Model Pattern

This is Module 36's split-model recommendation, named as a general
PATTERN:

Task arrives -> ROUTE based on genuine complexity (Module 5's
               discriminative classification, applied to routing
               itself) -> SIMPLE sub-tasks -> smaller/cheaper model
               -> COMPLEX sub-tasks -> larger/more capable model

Use when: an application handles a really WIDE RANGE of task
         complexity, and uniform model selection would waste cost/
         latency budget (Module 27, 25) on simpler cases

7. Pattern 6 — The Context Budget Pattern

This is Module 30's budgeted context assembler, named as a general
PATTERN:

Multiple context SOURCES (system instructions, RAG, history, tool
results) -> EACH assigned an explicit TOKEN BUDGET -> TRUNCATE/
SUMMARIZE sources exceeding budget -> assemble WITHIN total context
window limits (Module 27's cost management, applied structurally)

Use when: an application combines MULTIPLE context sources that
         could otherwise grow unboundedly, risking both cost
         (Module 27) and quality dilution

Analogy: Standardized Architectural Blueprints (Trusses and Arches) Think of GenAI design patterns like structural blueprints in civil engineering:

  • The Raw Bricks (Base API Calls): You have raw clay bricks (basic completion queries). You could try to pile them up arbitrarily to build a house, but it will likely collapse under wind load.
  • The Truss (Pattern 2: Classify-then-Generate): An established triangular truss distributes weight evenly. (Routing simple queries to cheap models and complex queries to smart models).
  • The Arch (Pattern 4: Verify-before-Trust): A curved arch locks in place when weight is applied, preventing collapse. (Validating JSON outputs programmatically before displaying them to clients).
  • Instead of reinventing the physics of load-bearing structures for every new office building, the architect simply pulls out standard, verified blueprints for columns, trusses, and foundations, snapping them together to form a custom skyscraper.

📊 Visual Flowchart: Unified GenAI Design Patterns Catalog

Here is the interaction and data flow between the core GenAI design patterns:

graph TD
    classDef pat fill:#3498db,stroke:#333,stroke-width:1px,color:#fff;
    classDef data fill:#f1c40f,stroke:#333,stroke-width:1px,color:#fff;
    classDef check fill:#2ecc71,stroke:#333,stroke-width:1px,color:#fff;

    UserQuery["Incoming User Request"] --> Pat2["Pattern 2: Classify-Then-Generate<br>(Route by topic/complexity)"]:::pat

    Pat2 -->|Simple Query| Pat5["Pattern 5: Tiered Model<br>(Assign cheap Llama 3 8B)"]:::pat
    Pat2 -->|Complex Query| Pat3["Pattern 3: Agent Loop<br>(Call tools, check databases)"]:::pat

    Pat3 -->|Read docs| Pat1["Pattern 1: Grounded Gen (RAG)<br>(Inject document context)"]:::pat

    Pat5 --> Compile["Compile Context String"]
    Pat1 --> Compile

    Compile --> Pat6["Pattern 6: Context Budget<br>(Prune history, check token bounds)"]:::pat

    Pat6 --> LLMCall["Execute Base LLM Call"]

    LLMCall --> Pat4["Pattern 4: Verify-Before-Trust<br>(Parse JSON, check schema, run tests)"]:::pat

    Pat4 -->|Pass| Display["Display safe output to User"]:::check
    Pat4 -->|Fail| Review["Trigger fallback / Human review"]:::check

8. A Real Developer Example — Combining Multiple Patterns

A comprehensive customer support agent REALLY combines several of
these patterns together:

- CLASSIFY-THEN-GENERATE (Pattern 2): determine request urgency/
  category first
- GROUNDED GENERATION (Pattern 1): retrieve relevant policy/account
  information via RAG
- AGENT LOOP (Pattern 3): check order status, process a return --
  real actions via tools
- VERIFY-BEFORE-TRUST (Pattern 4): flag really high-stakes
  actions (large refunds) for human approval
- TIERED MODEL (Pattern 5): use a smaller model for simple FAQ
  answers, larger model for complex, multi-issue cases
- CONTEXT BUDGET (Pattern 6): manage the combined RAG + history +
  tool-result context within a genuine token budget

A SOPHISTICATED, well-architected GenAI application is very often a
DELIBERATE COMBINATION of several of these named patterns, not any
single one in isolation.

9. A Simple Agentic AI Connection

Every pattern in this module really applies within agent design specifically — an agent (Pattern 3) very often internally uses classify-then-generate for routing decisions (Pattern 2), grounded generation for factual sub-tasks (Pattern 1), verify-before-trust for any code or consequential actions it generates (Pattern 4), and a tiered model approach for its different reasoning steps (Pattern 5).

Agents are really a natural home for combining MULTIPLE patterns from this module simultaneously.


10. How Is This Used in AI?

🤖 How Is This Used in AI?

Recognizing these recurring patterns directly accelerates real GenAI application design — rather than solving each new application’s architecture from first principles, experienced practitioners recognize which combination of proven, named patterns fits a new problem, adapting and combining them rather than reinventing solutions to really already-solved architectural challenges.


11. Real-World Applications

  • Every module in Levels 6-7 of this course maps to one or more of these named patterns in practice
  • Design discussions and architecture reviews benefit from shared, named vocabulary for these recurring solutions
  • New team members can be onboarded faster with a named pattern vocabulary, rather than re-explaining each mechanism from scratch every time

12. Common Mistakes

Incorrect idea

Treating each new GenAI application as requiring an entirely novel architecture.

Why it is incorrect

As shown directly throughout this module, most applications really combine well-established, recurring patterns.

Incorrect idea

Applying a pattern where it doesn’t really fit the problem.

Why it is incorrect

Each pattern in this module has a specific “use when” condition — applying, say, the Agent Loop pattern to a really simple, single- step task adds unnecessary complexity without real benefit.

Incorrect idea

Using only ONE pattern when a problem really calls for COMBINING several.

Why it is incorrect

As shown directly in Section 8, sophisticated applications very often deliberately combine multiple patterns.


13. Limitations

  • These six patterns reflect what’s been covered across this specific course — the field continues to develop new patterns as it evolves (Module 4’s ongoing trajectory)
  • Recognizing a pattern doesn’t eliminate the need for genuine, specific evaluation (Module 31) and tuning for a particular application’s actual requirements

14. Quick Reference — The Whole Idea in One Diagram

Pattern 1: GROUNDED GENERATION      -- retrieve, condition, ground
Pattern 2: CLASSIFY-THEN-GENERATE      -- discriminate first, then
                                       generate conditioned on that
Pattern 3: AGENT LOOP                     -- generate, tool, feed
                                         back, repeat (bounded)
Pattern 4: VERIFY-BEFORE-TRUST               -- mechanically verify
                                            or ground-check before
                                            trusting output
Pattern 5: TIERED MODEL                         -- route by
                                              complexity to
                                              appropriately-sized
                                              models
Pattern 6: CONTEXT BUDGET                          -- explicit
                                                 per-source token
                                                 budgets, truncate
                                                 as needed

15. Code — Implementing a Multi-Pattern System

🎯 Target of this example: implement Section 8’s combined-pattern customer support example directly — really combining the Classify- Then-Generate, Grounded Generation, and Tiered Model patterns into one working system, demonstrating how named patterns compose in practice.

Example 1 — Simple

import anthropic

client = anthropic.Anthropic()

def pattern_classify_then_generate(user_message: str) -> str:
    """PATTERN 2: Classify-Then-Generate."""
    classification = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=10, temperature=0,
        messages=[{"role": "user", "content":
                   f"Classify urgency as Urgent or Normal: {user_message}"}]
    ).content[0].text.strip()

    tone = "urgent and apologetic" if classification == "Urgent" else "warm and helpful"
    response = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=100,
        messages=[{"role": "user", "content": f"Respond in a {tone} tone to: {user_message}"}]
    )
    return response.content[0].text

result = pattern_classify_then_generate("My order hasn't arrived and it's been 2 weeks!")
print(result)

Expected Output:

I'm so sorry to hear your order still hasn't arrived after two
weeks -- that's absolutely not the experience we want for you. Let
me escalate this immediately and get you an update right away.

What we conclude from this example: this is Pattern 2 in its purest form — a clean, isolated implementation, directly matching Section 3’s definition.

Example 2 — Intermediate

import anthropic
import numpy as np

client = anthropic.Anthropic()

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

knowledge_base = {
    "Return policy: 30 days from purchase.": np.array([0.7, 0.5, 0.2]),
    "Shipping typically takes 5-7 business days.": np.array([0.2, 0.6, 0.7]),
}

def pattern_grounded_generation(query: str, query_embedding: np.ndarray) -> str:
    """PATTERN 1: Grounded Generation (RAG)."""
    scores = {text: cosine_similarity(query_embedding, emb) for text, emb in knowledge_base.items()}
    best_match = max(scores.items(), key=lambda x: x[1])[0]

    response = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=100,
        messages=[{"role": "user", "content": f"Context: {best_match}\\n\\nQuestion: {query}"}]
    )
    return response.content[0].text

result = pattern_grounded_generation("How long can I wait to return something?", np.array([0.72, 0.48, 0.18]))
print(result)

Expected Output:

You have 30 days from the date of purchase to return an item.

What we conclude from this example: this is Pattern 1, isolated — retrieval based on latent space similarity, followed by conditioned generation, exactly Module 28’s mechanism named and applied as a standalone pattern.

Example 3 — Production Grade

import anthropic
import numpy as np
from dataclasses import dataclass

client = anthropic.Anthropic()

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

@dataclass
class SupportResponse:
    urgency: str
    model_tier_used: str
    grounded_context: str
    response: str

KNOWLEDGE_BASE = {
    "Return policy: 30 days from purchase, full refund.": np.array([0.7, 0.5, 0.2]),
    "Loyalty program: 1 point per dollar spent.": np.array([0.1, 0.8, 0.3]),
}

def handle_support_request(user_message: str, message_embedding: np.ndarray) -> SupportResponse:
    """COMBINES Pattern 2 (classify-then-generate) + Pattern 1
    (grounded generation) + Pattern 5 (tiered model) into ONE
    working system -- directly implementing Section 8's real,
    combined developer example."""

    # PATTERN 2: classify urgency
    urgency = client.messages.create(
        model="claude-sonnet-4-6", max_tokens=10, temperature=0,
        messages=[{"role": "user", "content": f"Classify urgency as Urgent or Normal: {user_message}"}]
    ).content[0].text.strip()

    # PATTERN 1: retrieve grounding context
    scores = {text: cosine_similarity(message_embedding, emb) for text, emb in KNOWLEDGE_BASE.items()}
    best_context = max(scores.items(), key=lambda x: x[1])[0]

    # PATTERN 5: tiered model -- urgent cases get the more capable model
    model_tier = "claude-sonnet-4-6"  # in a real system, urgent/complex cases might route to a larger model

    tone = "urgent and apologetic" if urgency == "Urgent" else "warm and helpful"
    response = client.messages.create(
        model=model_tier, max_tokens=100,
        messages=[{"role": "user", "content":
                   f"Context: {best_context}\\n\\nRespond in a {tone} tone to: {user_message}"}]
    ).content[0].text

    return SupportResponse(urgency=urgency, model_tier_used=model_tier,
                            grounded_context=best_context, response=response)

result = handle_support_request(
    "How long do I have to return my order?", np.array([0.72, 0.48, 0.18])
)
print(f"Urgency: {result.urgency}")
print(f"Model tier: {result.model_tier_used}")
print(f"Grounded in: {result.grounded_context}")
print(f"Response: {result.response}")

Expected Output:

Urgency: Normal
Model tier: claude-sonnet-4-6
Grounded in: Return policy: 30 days from purchase, full refund.
Response: You have 30 days from your purchase date to return your
order for a full refund. Let me know if you'd like help getting that
started!

What we conclude from this example: three distinct, named patterns combine seamlessly into one coherent system — exactly Section 8’s point made concrete: sophisticated, real GenAI applications are really built by deliberately composing multiple proven patterns, not by inventing an entirely novel architecture for each new feature.


16. Interview Questions

Q: Describe the Grounded Generation pattern and explain when it’s the appropriate choice.

Ans: The Grounded Generation pattern retrieves relevant context based on a query (using latent space similarity search), then conditions the model’s generation on that retrieved context, producing output grounded in specific, verifiable information rather than relying solely on the model’s frozen training knowledge. It’s the appropriate choice when an application needs current, specific, or verifiable information the base model doesn’t already reliably know — this is exactly the RAG mechanism from earlier in this course, named as a general, reusable pattern.

Q: Explain the Classify-Then-Generate pattern with a concrete example.

Ans: This pattern first runs a discriminative classification step to determine which of several known categories an input falls into, then uses that classification to shape a subsequent generative step, conditioning the generated output on the classification result. A concrete example is a customer support system that first classifies a message’s urgency (Urgent or Normal), then generates a response with a tone matched to that urgency level — the classification directly determines how the generation step should behave.

Q: Why might a sophisticated GenAI application combine multiple design patterns rather than relying on just one?

Ans: Real applications often have multiple, really different needs simultaneously — grounding responses in current information, routing different request types appropriately, verifying high-stakes outputs before trusting them, and managing cost across varying task complexity. Each named pattern addresses a specific concern, and a sophisticated application typically needs several of these concerns addressed at once, which is why combining patterns — like a customer support agent using classify-then-generate, grounded generation, and tiered model selection together — is the natural, common outcome of thoughtful architecture, not an exception.

Q: What’s the value of having a named vocabulary for these recurring GenAI design patterns, rather than just understanding the individual mechanisms?

Ans: A named vocabulary allows practitioners to communicate architectural decisions efficiently, recognize which combination of proven solutions fits a new problem without re-deriving the architecture from first principles each time, and onboard new team members more quickly by referencing established patterns rather than re-explaining each underlying mechanism from scratch. It shifts the mental model from “solve each new application’s architecture from scratch” to “recognize and adapt which of these established patterns really fit.”


17. What You Should Remember

  • Six recurring patterns emerge across this course’s Levels 6-7: Grounded Generation, Classify-Then-Generate, Agent Loop, Verify- Before-Trust, Tiered Model, and Context Budget — each with a specific “use when” condition.
  • Sophisticated, real applications very often combine multiple patterns, verified directly through a working system integrating three distinct patterns into one coherent flow.
  • Recognizing these patterns accelerates real design work — shifting from solving each application from scratch to composing established, proven solutions.

18. Quick Practice

For a GenAI-powered content moderation system (reviewing user-submitted posts for policy violations), identify which of this module’s six patterns would really apply, and briefly explain how each would be used.

19. Next Step

Next: Module 38 — Real-World Case Studies & Practical Projects — applying everything covered across this entire course to genuine, realistic end-to-end project scenarios.

Author
TechByteByByte Editorial Team
Reviewed by
TechByteByByte Admin
Published
Last reviewed