02 Reference 5 min read 986 words
AI Design Patterns Quick Reference
Quick lookup for common patterns. See individual chapters for detailed implementation.
Retrieval Patterns
| Pattern | Use Case | Key Tradeoff |
|---|---|---|
| Basic RAG | Simple Q&A over documents | Easy to implement, limited accuracy |
| Hybrid Search | Combining semantic + keyword | Better recall, more complexity |
| Reranking | High-precision retrieval | Accuracy vs latency |
| Query Expansion | Ambiguous queries | Better recall, more tokens |
| HyDE | No direct matches expected | Creative, but can hallucinate |
| Parent-Child Chunking | Need surrounding context | Memory overhead |
The retrieval chain is linear until hybrid: BM25 runs beside the vector search and rejoins the same candidate list just before reranking
Text version of this diagram
Query → Embed → Vector Search → Rerank → Top-K → Generate
↓
BM25 Search ─────────┘ (hybrid)
Generation Patterns
| Pattern | Use Case | Key Tradeoff |
|---|---|---|
| Zero-Shot | Simple tasks | Fast, less reliable |
| Few-Shot | Need format control | Token cost |
| Chain-of-Thought | Reasoning tasks | Latency, shows work |
| Self-Consistency | High-stakes answers | 3-5x cost |
| Structured Output | API responses | Constrained creativity |
Agent Patterns
| Pattern | Use Case | Complexity |
|---|---|---|
| ReAct | Tool-using agents | Medium |
| Plan-and-Execute | Multi-step tasks | High |
| Multi-Agent Debate | Verification | High |
| Human-in-the-Loop | High-stakes actions | Medium |
| Swarm / Handoff | Specialised sub-agents | High |
ReAct is a closed loop: every act becomes a tool call whose result is the next observation
Text version of this diagram
┌─────────────────────────────────────────┐
│ REACT LOOP │
│ │
│ Observe → Think → Act → Observe → ... │
│ ↓ │
│ [Tool Call] │
│ ↓ │
│ [Result] │
└─────────────────────────────────────────┘
Agentic Coding Patterns (2026)
| Pattern | Use Case | Key Tool |
|---|---|---|
| Scaffold → Implement → Verify | Full feature development | Claude Code / OpenHands |
| Read-Plan-Edit | Refactoring existing code | Claude Code text_editor |
| Test-Driven Agent | High reliability code | Agent writes tests first |
| Shadow Review | PR quality gate | Agent reviews diff before merge |
| CLAUDE.md Manifest | Project context injection | Claude Code CLAUDE.md file |
| Sub-Agent Parallelism | Large codebase changes | Multiple agents per module |
The agentic coding loop exits only when the tests pass, and CLAUDE.md is what makes each pass conform to the codebase
Text version of this diagram
┌────────────────────────────────────────────────────────┐
│ AGENTIC CODING LOOP │
│ │
│ Understand → Plan → Implement → Run Tests → Fix │
│ ↑ (bash + text_editor tools) │ │
│ └──────────── Iterate until tests pass ────┘ │
│ │
│ [CLAUDE.md injects: coding style, test commands, │
│ forbidden patterns, architecture decisions] │
└────────────────────────────────────────────────────────┘
When to use which tool:
Need full autonomy + CLI → Claude Code
Need open-source + any LLM → OpenHands / Cline
Need tight IDE integration → Cursor / Windsurf
Need reproducible pipelines → OpenHands in Docker CI
Reliability Patterns
| Pattern | Problem Solved | Implementation |
|---|---|---|
| Retry with Backoff | Transient failures | Exponential backoff |
| Circuit Breaker | Cascading failures | Fail-fast after threshold |
| Fallback Model | Primary unavailable | Secondary model |
| Timeout | Slow responses | Cancel + fallback |
| Bulkhead | Resource isolation | Separate pools |
# Reliability stack
@circuit_breaker(failure_threshold=5)
@retry(max_attempts=3, backoff=exponential)
@timeout(seconds=30)
@fallback(model="gpt-4o-mini")
async def generate(prompt):
return await primary_model.generate(prompt)
Caching Patterns
| Pattern | Hit Rate | Use Case |
|---|---|---|
| Exact Match | Low | Identical queries |
| Semantic Cache | Medium | Similar queries |
| KV Cache | High | Same prefix |
| Response Cache | Varies | Deterministic outputs |
Security Patterns
| Pattern | Threat | Implementation |
|---|---|---|
| Input Validation | Prompt injection | Sanitize, detect |
| Output Filtering | Data leakage | PII detection, blocklists |
| Tenant Isolation | Cross-tenant access | Filter at query time |
| Rate Limiting | Abuse | Per-user/tenant limits |
Input → Validate → Sanitize → LLM → Filter → Validate → Output
Evaluation Patterns
| Pattern | Use Case | Metrics |
|---|---|---|
| Golden Set | Regression testing | Pass rate |
| LLM-as-Judge | Quality scoring | 1-5 scale |
| Human Eval | Ground truth | Agreement rate |
| A/B Testing | Production comparison | User metrics |
Cost Optimization Patterns
| Pattern | Savings | Tradeoff |
|---|---|---|
| Model Routing | 50-70% | Complexity |
| Caching | 20-40% | Staleness |
| Prompt Compression | 10-30% | Quality risk |
| Batch Processing | 30-50% | Latency |
Query → Classify → Route → [Small Model] or [Large Model]
↓
[Cheap: 80%] [Expensive: 20%]
Anti-Patterns to Avoid
| Anti-Pattern | Problem | Better Approach |
|---|---|---|
| Context Stuffing | Token waste | Retrieve relevant only |
| Retry Forever | Resource exhaustion | Circuit breaker |
| Trust All Output | Hallucination | Verify, ground |
| Single Model | Single point of failure | Multi-provider |
| No Observability | Blind debugging | Trace everything |
| Infinite Agentic Loop | Agent spins without progress | Max turns + Critic agent |
| Over-trusting Computer-Use | Agent clicks wrong UI elements | Screenshot validation + HITL |
| No CLAUDE.md / Manifest | Agent lacks project context | Always provide coding manifest |
| Thinking Mode Always On | 3-10x cost with no benefit | Gate on complexity classifier |
Pattern Selection Guide
Starting a new project?
- Begin with Basic RAG
- Add reranking when precision matters
- Add hybrid search for keyword-heavy content
Need reliability?
- Start with retry + timeout
- Add circuit breaker for external calls
- Add fallback models for critical paths
Cost concerns?
- Implement semantic caching first
- Add model routing for query complexity
- Batch where latency allows
See 15-ai-design-patterns/ for detailed implementations
Key takeaways
01
A lookup table, not a tutorial
Nine pattern families — retrieval, generation, agent, agentic coding, reliability, caching, security, evaluation and cost — each a table of pattern, use case and the tradeoff bought.
02
Every cost pattern carries a price
Model routing saves 50-70 percent, batch processing 30-50, caching 20-40 and prompt compression 10-30, each against a named cost such as staleness, latency or quality risk.
03
Reliability stacks as decorators
The example composes a circuit breaker at five failures, three retries with exponential backoff, a 30-second timeout and a gpt-4o-mini fallback onto one generate call.
04
Agentic coding is its own pattern family
Scaffold-implement-verify, read-plan-edit, test-driven agent, shadow review, the CLAUDE.md manifest and sub-agent parallelism are listed as file-system-level patterns distinct from orchestration.
05
Start basic, add precision later
The selection guide sequences work: basic RAG first, reranking when precision matters, hybrid search for keyword-heavy content; for cost, semantic caching before model routing.