RAG代理模式
构建检索增强生成代理的模式:简单RAG、代理RAG、混合搜索、交叉编码重排序、上下文检索、HyDE、CRAG、自我RAG、RAFT、GraphRAG、引用流。
RAG代理是一个将其答案基于检索文档的LLM代理,而不是依赖于基础模型记住的内容。这里的模式描述了如何以可靠的方式做到这一点:如何检索、如何重排序、如何验证、如何呈现用户可以检查的引用,以及当检索失败时如何回退。
简单RAG是入门点 — 嵌入查询,获取最近邻,粘贴到提示中 — 但生产RAG是围绕它的所有内容:在向量和词汇索引之间的混合搜索、交叉编码重排序、上下文分块、查询重写(HyDE)、纠正检索(CRAG)、对检索质量的自我反思(Self-RAG)、在检索形状任务上微调模型(RAFT)、图感知检索(GraphRAG),以及随着解析而流动的引用。
Field-tested patterns to start with
- Agentic RAG — Replace static retrieve-then-generate with autonomous agents that plan, choose sources, retrieve iteratively, reflect, and re-query.
- Naive RAG — Condition the generator on top-k chunks retrieved from an external dense index so knowledge lives outside parameters.
- Hybrid Search — Combine sparse lexical retrieval (BM25) with dense vector retrieval and fuse the results.
- Cross-Encoder Reranking — After cheap bi-encoder or BM25 retrieval, rescore top-N candidates with a cross-encoder that jointly attends over (query, candidate).
- Contextual Retrieval — Prepend a short LLM-generated description to each chunk before embedding so the chunk carries its situating context.
- HyDE — Have the LLM write a hypothetical answer document, embed it, and use it as the retrieval query.
- CRAG — Add a lightweight retrieval evaluator that grades each retrieved document and triggers corrective web search on poor retrievals.
- Self-RAG — Fine-tune the model to emit reflection tokens that decide when to retrieve, evaluate retrieved relevance, and assess generated support.
- RAFT — Train the model to ignore irrelevant retrieved documents (distractors) in a domain-specific RAG setting.
- GraphRAG — Build an LLM-extracted entity-and-relation knowledge graph plus hierarchical community summaries, then answer global queries via map-reduce over those summaries.
- Citation Streaming — Stream citations alongside generated text so the UI can render source links in place as content appears.
- Chain of Verification — Reduce hallucination by drafting an answer, generating independent verification questions, answering them in isolation, and revising.
Recommended reading
- Retrieval & RAG — 23 patterns
- Verification & Reflection — 32 patterns
Or open the full contents for all 564 patterns in 14 books.
Related guides
- AI Agents Patterns — AI agents patterns: named, reusable shapes for building AI agents that reason, use tools, coordinate, and stay safe — single-agent loops an…
- AI Agents Patterns Catalog — The AI agents patterns catalog: a complete, GoF-formal pattern language for AI agents across reasoning, planning, tool use, retrieval, memo…
- LLM Agent Design Patterns — A GoF-formal catalog of LLM agent design patterns: ReAct, tool use, plan-and-execute, reflection, step budget, and more. Each pattern decom…
- Agentic Design Patterns — A GoF-formal catalog of agentic design patterns — named, reusable shapes for building autonomous AI agents: agent loops, tool use, planning…
- Agentic AI Design Patterns — Agentic AI design patterns for systems already in production — what to ship, what to observe, what to budget, what to gate. Augmented LLM,…
- AI Agent Design Patterns — How to build an AI agent: the named shapes you reach for during design and implementation — reasoning (ReAct, plan-and-execute, reflection)…
- Agent Design Patterns — Agent design patterns treat the agent loop as a software-engineering primitive: an observe→reason→act cycle wrapped in tools, memory, super…
- Agentic Patterns — A complete pattern language for agentic systems, organised in Alexander-style books across reasoning, planning, tool use, retrieval, verifi…
- Agentic AI Architecture — How to structure agentic AI: the architectural patterns that hold an LLM-powered system together. Supervisor, orchestrator-workers, augment…
- Multi-Agent Patterns — Patterns for coordinating multiple LLM agents: supervisor, orchestrator-workers, handoff, debate, hierarchical agents, swarm, role assignme…
- AI Agent Safety Patterns — Safety patterns for LLM agents: step budget, kill switch, constitutional charter, approval queue, sandbox isolation, input/output guardrail…
About this catalog
The Agent Patterns Catalog is an open, GoF-formal reference of 564 design patterns for building LLM agents. Each pattern is decomposed in the manner of Christopher Alexander (1977) and the Gang of Four (1994). Source of truth at github.com/agentpatternscatalog/patterns — CC BY 4.0.