RAG Architecture Patterns: Beyond Basic Vector Search
A comprehensive guide to advanced RAG techniques including hybrid search, reranking, GraphRAG, and self-corrective patterns with production AWS implementation examples.
Retrieval-Augmented Generation (RAG) systems usually start with plain vector similarity search. That baseline breaks down on three predictable fronts: exact keyword matches, questions whose answer spans several documents, and retrieved context that nobody checked for relevance.
The default worth reaching for first is hybrid search combined with parent-child chunking. Reranking, self-corrective retrieval (CRAG), and knowledge graphs (GraphRAG) sit above it as escalations, added when measurement shows the default falls short. The patterns below map onto AWS Bedrock Knowledge Bases and OpenSearch, and RAGAS metrics decide whether an escalation paid for itself.
The Problem with Basic RAG#
Vector similarity alone leaves three specific gaps in production.
Missing Exact Matches#
Vector embeddings excel at capturing semantic meaning but struggle with precise matches. When users search for “AWS CDK”, “GAN architecture”, or specific product codes, pure semantic search often misses these exact terms. The embedding model treats “GAN” (Generative Adversarial Network) as semantically similar to general “neural network” content, diluting precision.
# Basic RAG implementation - common starting point
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
vectorstore = Chroma.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(model="text-embedding-3-small")
)
# Vector similarity search
query = "What is GAN architecture?"
results = vectorstore.similarity_search(query, k=5)
# Problem: May miss documents with exact "GAN" term
# Returns semantically similar "neural network" docs instead
Multi-Hop Reasoning Failures#
Basic RAG retrieves documents based on single-step similarity. Complex queries requiring connections across multiple documents fail systematically:
- “Which AWS service launched in 2020 has the lowest cold start time?”
- “What are the security implications of using serverless databases with Lambda?”
These questions need information synthesis from disparate sources, something single-step retrieval cannot handle.
No Quality Verification#
Standard RAG pipelines pass retrieved documents directly to the LLM without verifying relevance. Irrelevant context causes hallucinations and degraded answer quality. This becomes particularly problematic when retrieval returns marginally related documents that the LLM treats as authoritative.
Hybrid Search: Combining Semantic and Keyword Retrieval#
The first practical upgrade for RAG systems combines dense vector search with sparse keyword matching. This hybrid approach addresses the exact match problem while maintaining semantic understanding.
Implementation Strategy#
Hybrid search runs two independent retrievals:
- Dense retrieval: Vector similarity (semantic understanding)
- Sparse retrieval: BM25 keyword matching (exact term matching)
Results merge using Reciprocal Rank Fusion (RRF):
RRF(d) = Σ 1/(k + rank(d))
Where k is a constant (typically 60) and rank(d) is the document’s position in each result set.
Working Implementation#
from langchain.retrievers import EnsembleRetriever
from langchain_community.vectorstores import Chroma
from langchain_community.retrievers import BM25Retriever
from langchain_openai import OpenAIEmbeddings
# Dense vector retriever
vectorstore = Chroma.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(model="text-embedding-3-small")
)
vector_retriever = vectorstore.as_retriever(
search_kwargs={"k": 10}
)
# Sparse keyword retriever
bm25_retriever = BM25Retriever.from_documents(docs)
bm25_retriever.k = 10
# Hybrid ensemble with weighted RRF (k defaults to 60)
ensemble_retriever = EnsembleRetriever(
retrievers=[vector_retriever, bm25_retriever],
weights=[0.5, 0.5] # Equal weighting
)
# Retrieve with both methods
results = ensemble_retriever.invoke(
"What is GAN architecture?"
)
Performance Characteristics#
What the second retriever changes:
- Named entities land reliably, because BM25 matches the literal string that the embedding model blurs into its neighbourhood
- Abbreviations behave the same way (GAN, RAG, AWS, CDK)
- Latency grows by one retrieval pass.
EnsembleRetrieverruns the two passes concurrently only on the async path (ainvoke); the synchronousinvokeabove calls the retrievers one after another - Recall improves without costing precision, since the two result sets are merged rather than intersected
RRF works on ranks because the raw scores do not share a scale. Cosine similarity sits roughly between 0 and 1, while a BM25 score has no upper bound and shifts from query to query. Adding the two directly lets BM25 dominate, so an “equal” weight is not equal at all. Weighted score fusion works only after each list is rescaled, which is the min-max step OpenSearch’s hybrid search applies by default:
# Alternative: weighted score fusion instead of RRF
def min_max(scores: dict[str, float]) -> dict[str, float]:
"""Rescale one retriever's scores to [0, 1]"""
if not scores:
return {}
lo, hi = min(scores.values()), max(scores.values())
if hi == lo:
return {doc_id: 1.0 for doc_id in scores}
return {doc_id: (s - lo) / (hi - lo) for doc_id, s in scores.items()}
def weighted_fusion(
vector_scores: dict[str, float],
bm25_scores: dict[str, float],
alpha: float = 0.5,
) -> list[tuple[str, float]]:
"""
Inputs map doc_id -> raw score, where higher means more relevant.
alpha = 0.0: keyword scores only
alpha = 0.5: equal weight, once both lists share the [0, 1] scale
alpha = 1.0: vector scores only
"""
vec = min_max(vector_scores)
kw = min_max(bm25_scores)
fused = {
doc_id: alpha * vec.get(doc_id, 0.0) + (1 - alpha) * kw.get(doc_id, 0.0)
for doc_id in vec.keys() | kw.keys()
}
return sorted(fused.items(), key=lambda x: x[1], reverse=True)
A LangChain Document carries no score, so the inputs are plain doc_id -> score maps built from calls such as similarity_search_with_score. Some vector stores return a distance there, where lower means closer; invert those values before fusing. Min-max scaling also stretches a weak result list to the full range, which is one more reason RRF is the safer default.
Prototype vs Production Hybrid Search#
The ensemble above is a prototype. BM25Retriever.from_documents(docs) builds its BM25 index in process memory, and the class has no method to add, update or persist documents. Every process rebuilds the index at startup, every replica holds its own copy, and a changed document means a rebuild.
In production the keyword side belongs in a search engine that keeps one shared, incrementally updated index. OpenSearch and Elasticsearch run BM25 natively, and an OpenSearch hybrid query combines the lexical and vector clauses inside one request, with score normalization handled by a search pipeline. Bedrock Knowledge Bases exposes the same idea as overrideSearchType: 'HYBRID' on stores that hold a filterable text field. Keep the in-memory ensemble for notebooks and evaluation runs, where rebuilding from a fixed document set is cheap.
Multi-Stage Reranking: Precision After Recall#
Hybrid search improves recall, but production systems often need even higher precision. Multi-stage reranking addresses this through a two-phase approach.
Architecture Pattern#
- Stage 1: High-recall retrieval - Cast a wide net (k=50-100)
- Stage 2: Cross-encoder reranking - Precision scoring on candidates
- Stage 3: Top-k selection - Final set for LLM (k=5-10)
This pattern separates retrieval (fast, broad) from relevance scoring (slower, precise).
Cross-Encoder vs Bi-Encoder#
Understanding the difference matters for implementation:
- Bi-encoder (traditional embeddings): Encodes query and documents separately, compares vectors
- Cross-encoder: Feeds query + document pairs into BERT-based model for direct relevance scores
Cross-encoders produce more accurate relevance scores but don’t scale to large collections (must score each candidate individually). This makes them perfect for the reranking stage.
Implementation#
from sentence_transformers import CrossEncoder
import numpy as np
# Stage 1: High-recall retrieval
initial_results = vectorstore.similarity_search(
query,
k=50 # Cast wide net
)
# Stage 2: Cross-encoder reranking
reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
# Create query-document pairs
query_doc_pairs = [
(query, doc.page_content)
for doc in initial_results
]
# Score all pairs
scores = reranker.predict(query_doc_pairs)
# Stage 3: Sort by score and take top-k
reranked_indices = np.argsort(scores)[::-1][:10]
final_docs = [initial_results[i] for i in reranked_indices]
final_scores = [scores[i] for i in reranked_indices]
# Use final_docs for LLM generation
print(f"Top result score: {final_scores[0]:.3f}")
What Reranking Buys#
Reranking changes the order of the result set, not its size. Because the cross-encoder reads the query and the document together, it separates two chunks that a bi-encoder maps to nearly the same vector. The gain shows up in ranking metrics such as MRR@5 and in precision on domain-specific queries, where the top three slots decide the answer the model writes.
The cost is one model inference per candidate. Implementations batch those pairs, so 50 candidates go through the model in a handful of batched forward passes rather than 50 serial ones. Whether that fits a sub-second budget depends on the model size, the batch size, and the hardware. A small cross-encoder on a GPU or a hosted reranking endpoint gets there; a large model on CPU does not. Measure that combination before deciding.
When to Use Reranking#
Decide with two measurements on your own data: a ranking metric (RAGAS context precision, or MRR@k) on an evaluation set, and the reranker’s p95 latency for your model, candidate count and hardware.
Implement reranking when:
- The evaluation set shows the right chunk inside the candidate list but below the top-k cut
- Complex technical queries with nuanced relevance
- Legal, medical, financial domains where a wrong top chunk is expensive
- The measured reranker p95 fits inside the latency budget
Skip reranking when:
- The measured p95 does not fit the budget, even after trimming the candidate count or switching to a smaller model
- Simple FAQ systems
- Limited computational budget
- The evaluation set already shows the right chunk at the top without it
Chunking Strategies: Context Preservation#
How you split documents into chunks significantly impacts retrieval quality. Poorly chunked content can destroy otherwise solid RAG implementations.
Strategy Comparison#
Fixed-Size Chunking (Baseline)
from langchain.text_splitter import CharacterTextSplitter
splitter = CharacterTextSplitter(
chunk_size=512,
chunk_overlap=50
)
chunks = splitter.split_documents(documents)
Problems:
- Breaks sentences arbitrarily
- Splits code blocks mid-function
- No respect for logical boundaries
Semantic Chunking
from langchain_experimental.text_splitter import SemanticChunker
from langchain_openai import OpenAIEmbeddings
splitter = SemanticChunker(
embeddings=OpenAIEmbeddings(model="text-embedding-3-small"),
breakpoint_threshold_type="percentile"
)
chunks = splitter.split_documents(documents)
Benefits:
- Preserves topic coherence
- Natural section boundaries
- Higher preprocessing cost
Parent-Child Hierarchical Chunking
This approach searches on small chunks for precision but returns larger parent chunks for context. It works well as a default strategy for technical documentation.
from langchain.retrievers import ParentDocumentRetriever
from langchain.storage import InMemoryStore
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
# Parent splitter (large chunks for context)
parent_splitter = RecursiveCharacterTextSplitter(
chunk_size=2000,
chunk_overlap=200
)
# Child splitter (small chunks for precision)
child_splitter = RecursiveCharacterTextSplitter(
chunk_size=400,
chunk_overlap=50
)
# Store parent docs separately
docstore = InMemoryStore()
# Vector store indexes child chunks
vectorstore = Chroma(
embedding_function=OpenAIEmbeddings(model="text-embedding-3-small")
)
# Retriever configuration
retriever = ParentDocumentRetriever(
vectorstore=vectorstore,
docstore=docstore,
child_splitter=child_splitter,
parent_splitter=parent_splitter,
)
# Add documents
retriever.add_documents(documents)
# Retrieval searches child chunks but returns parents
results = retriever.invoke(
"How do I optimize Lambda cold starts?"
)
# Results contain full parent context
Performance Impact#
Parent-child retrieval trades storage for coherence. The corpus lives twice: parents in a document store, children in the vector index. Query-time cost stays close to plain vector search, because the extra work is a key lookup in the docstore after the vector hit.
The model then reads the section a fragment belongs to, in place of a 400-token slice that starts mid-sentence and ends mid-table.
Best Practices#
Rules that hold across content types:
- Terminate at natural boundaries: End chunks at sentence or paragraph breaks
- Add metadata: Include document title, section headers in chunk metadata
- Overlap strategically: 10-20% overlap prevents information loss at boundaries
- Match strategy to content:
- Technical docs → Semantic or hierarchical
- Code → Function/class-level chunks
- Narrative → Sliding window with overlap
- Structured data → Parent-child with metadata
Self-RAG and Corrective RAG: Quality Verification#
Basic RAG assumes retrieved documents are relevant. This assumption fails frequently in production, causing hallucinations and poor answers. Self-correcting patterns address this with explicit quality checks.
The CRAG Pattern#
Corrective RAG (CRAG) introduces a retrieval evaluator that grades document relevance before generation. Based on confidence scores, the system routes to different processing paths.
Workflow:
- Retrieve documents
- Grade each document’s relevance
- Route based on aggregate confidence:
- High confidence (>0.7): Proceed with knowledge refinement
- Low confidence (<0.3): Trigger web search
- Medium confidence (0.3-0.7): Combine web search + refinement
Knowledge Refinement partitions documents into “knowledge strips”, grades each strip, and filters irrelevant content before passing to the LLM.
Implementation with LangGraph#
from langgraph.graph import StateGraph, END
from langchain_tavily import TavilySearch
from langchain_openai import ChatOpenAI
from typing import TypedDict, List
from langchain.schema import Document
# Define workflow state
class RAGState(TypedDict):
query: str
documents: List[Document]
relevance_score: float
web_results: List[Document]
answer: str
# Initialize components
vectorstore = Chroma.from_documents(docs, OpenAIEmbeddings(model="text-embedding-3-small"))
llm = ChatOpenAI(model="gpt-4o-mini")
web_search = TavilySearch(max_results=3)
# Node functions
def retrieve(state):
"""Retrieve initial documents"""
docs = vectorstore.similarity_search(state["query"], k=5)
return {"documents": docs}
def grade_documents(state):
"""Grade document relevance"""
# Grade each document with its own prompt
scores = []
for doc in state["documents"]:
prompt = f"""
Score the relevance of this document to the query on a scale of 0-1.
Query: {state["query"]}
Document: {doc.page_content[:500]}
Return only a number between 0 and 1.
"""
response = llm.invoke(prompt)
score = float(response.content.strip())
scores.append(score)
avg_score = sum(scores) / len(scores)
return {"relevance_score": avg_score}
def route_query(state):
"""Route based on relevance score"""
score = state["relevance_score"]
if score > 0.7:
return "correct"
elif score < 0.3:
return "incorrect"
else:
return "ambiguous"
def perform_web_search(state):
"""Fallback web search"""
web_results = web_search.invoke(state["query"])
web_docs = [
Document(page_content=result["content"])
for result in web_results
]
return {"web_results": web_docs}
def refine_knowledge(state):
"""Partition and filter document strips"""
refined_docs = []
for doc in state["documents"]:
# Simple strip partitioning (sentence-level)
sentences = doc.page_content.split('. ')
for sentence in sentences:
# Grade each sentence
grade_prompt = f"""
Is this sentence relevant to: {state["query"]}?
Sentence: {sentence}
Answer only: yes or no
"""
response = llm.invoke(grade_prompt)
if "yes" in response.content.lower():
refined_docs.append(sentence)
return {"documents": [Document(page_content=". ".join(refined_docs))]}
def generate_answer(state):
"""Generate final answer"""
context_docs = state.get("documents", [])
web_docs = state.get("web_results", [])
all_context = context_docs + web_docs
context_text = "\n\n".join([doc.page_content for doc in all_context])
prompt = f"""
Answer the question based on this context:
Context:
{context_text}
Question: {state["query"]}
Answer:
"""
response = llm.invoke(prompt)
return {"answer": response.content}
# Build the graph
workflow = StateGraph(RAGState)
# Add nodes
workflow.add_node("retrieve", retrieve)
workflow.add_node("grade", grade_documents)
workflow.add_node("web_search", perform_web_search)
workflow.add_node("refine", refine_knowledge)
workflow.add_node("generate", generate_answer)
# Add edges
workflow.set_entry_point("retrieve")
workflow.add_edge("retrieve", "grade")
# Conditional routing after grading
workflow.add_conditional_edges(
"grade",
route_query,
{
"correct": "refine",
"incorrect": "web_search",
"ambiguous": "web_search"
}
)
workflow.add_edge("refine", "generate")
workflow.add_edge("web_search", "generate")
workflow.add_edge("generate", END)
# Compile and run
app = workflow.compile()
# Execute
result = app.invoke({
"query": "What are the latest AWS Lambda optimization techniques?"
})
print(result["answer"])
Where CRAG Helps#
The evaluator is what changes the outcome. A document that fails grading never reaches the prompt, so the model cannot ground an answer in it. That matters most on queries where retrieval confidence is low and on questions that fall outside the knowledge base entirely, which is exactly where a plain pipeline invents an answer with full confidence.
Every document costs a grading call, and a low-confidence route adds a web-search round trip on top. Sentence-level refinement multiplies the count: the refine_knowledge node sends one call per sentence, so five retrieved documents of twenty sentences each add about a hundred small calls before generation starts. A CRAG query therefore runs several times the latency and tokens of a single-pass query, which is why the grader is the first model to shrink when that bill hurts. Grading a paragraph or a fixed-size strip in one call cuts the count without dropping the filter.
The web-search branch also widens the attack surface. Search results enter the generation prompt as plain text, and a page can carry instructions aimed at the model, the indirect prompt injection case. Mark web content as untrusted data in the prompt, run it through the same grader as retrieved documents, and keep tool access away from the generation step.
GraphRAG: Knowledge Graphs for Multi-Hop Reasoning#
Traditional RAG retrieves based on semantic similarity to the query. This works for single-hop questions but fails when answers require connecting information across multiple documents. GraphRAG solves this through knowledge graph construction and graph-based retrieval.
When Traditional RAG Fails#
Complex queries that GraphRAG handles better:
- “Which AWS services integrate with both Lambda and DynamoDB?”
- “What are the security implications of serverless database patterns?”
- “Summarize all best practices mentioned across the documentation”
These require understanding relationships and synthesizing information across documents.
GraphRAG Architecture#
Microsoft’s GraphRAG indexes a corpus in three phases:
Phase 1: Knowledge Graph Construction
- Extract entities (services, concepts, technologies)
- Extract relationships (integrates_with, depends_on, alternative_to)
- Extract claims (factual statements)
- Build directed graph
Phase 2: Community Detection
- Apply Leiden algorithm for hierarchical clustering
- Create community summaries at each level
- Build hierarchical index
Phase 3: Retrieval
- Global search: Use community summaries for broad questions
- Local search: Traverse graph for specific relationship queries
- Hybrid: Combine graph traversal with vector similarity
GraphRAG vs Text2Cypher#
Two different techniques travel under the graph-RAG label. GraphRAG in Microsoft’s sense is the pipeline above: it clusters the graph into communities and writes a summary for each one, and its global search answers corpus-wide questions from those summaries. Text2Cypher is narrower: an LLM translates the question into a Cypher query, the database runs it, and the rows become the context. It answers relationship questions about specific entities, the local-search case, and it never sees a community summary.
The code below is Text2Cypher on top of an LLM-extracted graph. For global questions, use the graphrag package from Microsoft, which builds the communities and summaries itself.
Implementation Pattern#
import os
from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_neo4j import GraphCypherQAChain, Neo4jGraph
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o", temperature=0)
# Ingestion: a writer account builds the graph
writer_graph = Neo4jGraph(
url="bolt://localhost:7687",
username="graph_writer",
password=os.environ["NEO4J_WRITER_PASSWORD"],
)
transformer = LLMGraphTransformer(
llm=llm,
allowed_nodes=["Service", "Concept", "Technology"],
allowed_relationships=["INTEGRATES_WITH", "DEPENDS_ON", "ALTERNATIVE_TO"],
)
# The LLM reads every document to extract nodes and relationships
graph_documents = transformer.convert_to_graph_documents(documents)
writer_graph.add_graph_documents(
graph_documents,
baseEntityLabel=True, # adds an indexed __Entity__ label to every node
include_source=True, # links nodes to their source document via MENTIONS
)
# Query time: a separate read-only account (e.g. the built-in reader role of Neo4j Enterprise)
reader_graph = Neo4jGraph(
url="bolt://localhost:7687",
username="graph_reader",
password=os.environ["NEO4J_READER_PASSWORD"],
)
qa_chain = GraphCypherQAChain.from_llm(
llm=llm,
graph=reader_graph,
validate_cypher=True,
return_intermediate_steps=True,
# Required opt-in: the chain runs model-written Cypher as-is
allow_dangerous_requests=True,
)
# Multi-hop query
response = qa_chain.invoke({
"query": "Which serverless services integrate with DynamoDB and support event-driven architectures?"
})
print(response["result"])
print("Cypher Query:", response["intermediate_steps"][0]["query"])
GraphCypherQAChain refuses to start without allow_dangerous_requests=True, because whatever Cypher the model writes runs against the database unchanged. A prompt that coaxes out a DELETE or SET statement executes with the connection’s rights. The flag is an acknowledgement and protects nothing on its own; the control is the database account. Give the query-time connection read-only rights (Neo4j Enterprise Edition, AuraDB Business Critical and AuraDB Virtual Dedicated Cloud ship a built-in reader role for this; Community Edition has no role-based access control, so it cannot enforce a read-only account) and keep the writer credentials in the ingestion job.
Performance Trade-offs#
Costs:
- Preprocessing: entity and relationship extraction runs LLM calls over every document, which dominates the ingestion bill
- Storage: a graph database to operate alongside the vector store
- Complexity: someone on the team has to own Cypher and the graph schema
- Security: Text2Cypher executes model-written queries, so the read-only account is mandatory
Benefits:
- Multi-hop recall, because traversal follows relationships that similarity search cannot see
- Global questions (“summarize everything about X”) answered from community summaries, which a top-k window can never assemble; this needs Microsoft GraphRAG’s community step, which the Text2Cypher example does not build
- Traceable answers, since the traversal path doubles as the citation trail
When to Use GraphRAG#
Good fit:
- Rich relationship domains (medical, legal, enterprise knowledge)
- Multi-hop reasoning requirements
- Holistic understanding needs across large corpora
- Available preprocessing budget
Poor fit:
- Simple FAQ systems
- Primarily single-hop queries
- Limited preprocessing resources
- Small document collections (<1000 documents)
AWS Bedrock Knowledge Bases: Production Implementation#
AWS Bedrock Knowledge Bases packages most of the patterns above as a managed service. Hybrid search, hierarchical chunking, and reranking arrive as configuration flags on a pipeline someone else operates.
Architecture Options#
Vector Store Choices:
- Amazon OpenSearch Serverless: Most common for production RAG, supports hybrid search
- Amazon Aurora PostgreSQL: With pgvector extension, good for existing PostgreSQL users
- Amazon Neptune Analytics: For GraphRAG patterns
- Third-party: MongoDB Atlas, Pinecone, Redis Enterprise Cloud
Two API Patterns#
1. RetrieveAndGenerate API (Fully Managed)
This handles the entire RAG pipeline:
import boto3
bedrock_agent_runtime = boto3.client('bedrock-agent-runtime')
response = bedrock_agent_runtime.retrieve_and_generate(
input={
'text': 'How do I optimize Lambda cold starts?'
},
retrieveAndGenerateConfiguration={
'type': 'KNOWLEDGE_BASE',
'knowledgeBaseConfiguration': {
'knowledgeBaseId': 'KB123456',
'modelArn': 'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-sonnet-4-5-20250929-v1:0',
'retrievalConfiguration': {
'vectorSearchConfiguration': {
'numberOfResults': 10, # Increase from default 5
'overrideSearchType': 'HYBRID' # Hybrid search
}
}
}
}
)
answer = response['output']['text']
citations = response['citations']
# Citations include source attribution
for citation in citations:
print(f"Source: {citation['retrievedReferences'][0]['location']}")
2. Retrieve API (Custom Control)
For more control over the pipeline:
# Retrieve documents
retrieve_response = bedrock_agent_runtime.retrieve(
knowledgeBaseId='KB123456',
retrievalQuery={
'text': 'How do I optimize Lambda cold starts?'
},
retrievalConfiguration={
'vectorSearchConfiguration': {
'numberOfResults': 20,
'overrideSearchType': 'HYBRID'
}
}
)
# Process retrieved chunks
retrieved_docs = retrieve_response['retrievalResults']
for doc in retrieved_docs:
content = doc['content']['text']
score = doc['score']
source = doc['location']['s3Location']
print(f"Score: {score:.3f} - Source: {source['uri']}")
# Now you control:
# - Custom reranking logic
# - Document filtering
# - Prompt construction
# - Model selection for generation
Advanced Chunking Configuration#
# Hierarchical chunking (parent-child pattern)
chunking_config = {
'chunkingStrategy': 'HIERARCHICAL',
'hierarchicalChunkingConfiguration': {
'levelConfigurations': [
{
'maxTokens': 1500 # Parent chunk size
},
{
'maxTokens': 300 # Child chunk size
}
],
'overlapTokens': 60
}
}
# Alternative: Semantic chunking
chunking_config = {
'chunkingStrategy': 'SEMANTIC',
'semanticChunkingConfiguration': {
'maxTokens': 300,
'bufferSize': 0,
'breakpointPercentileThreshold': 95
}
}
# Advanced: Custom Lambda chunking function
chunking_config = {
'chunkingStrategy': 'NONE', # Disable default chunking
'customChunkingConfiguration': {
'lambdaArn': 'arn:aws:lambda:us-east-1:123456789:function:custom-chunker'
}
}
Reranking Integration#
retrieve_config = {
'vectorSearchConfiguration': {
'numberOfResults': 50, # High recall
'overrideSearchType': 'HYBRID',
'rerankingConfiguration': {
'type': 'BEDROCK_RERANKING_MODEL',
'bedrockRerankingConfiguration': {
'numberOfResults': 10, # Precision after reranking
'modelConfiguration': {
'modelArn': 'arn:aws:bedrock:us-east-1::foundation-model/cohere.rerank-v3-5:0'
}
}
}
}
}
The reranker ARN names the same region as the knowledge base and generation model above. Reranking runs in a short list of regions: Cohere Rerank 3.5 is available in us-east-1, while Amazon Rerank 1.0 is not, so check AWS’s supported-regions table before you change either value.
Production Optimization Tips#
Settings that carry the most weight in a Bedrock deployment:
- Increase numberOfResults: Bedrock returns up to 5 chunks by default, which is often too few for complex queries; 10-15 helps there, but every extra chunk lands in the generation prompt and raises input tokens and latency. With a reranker, retrieve wide and let it trim to the final count
- Enable Hybrid Search: Improves named entity and abbreviation retrieval
- Implement Reranking: Changes which documents fill the top-k slots on technical queries
- Choose Appropriate Chunking: Hierarchical for technical docs, semantic for narrative
- Monitor Token Usage: Track embedding and generation costs separately
- Use Customer-Managed KMS: For sensitive data encryption
- Cache Strategically: Cache embeddings and common query results
Infrastructure as Code (CDK)#
A knowledge base on OpenSearch Serverless needs more than a collection and a role. Four pieces are easy to miss, and leaving out any one of them breaks either the deploy or the first ingestion job:
- Encryption policy. A collection is created only when a KMS key is associated with it, through a matching encryption policy or the collection’s own encryption settings.
- Network and data access policies. The network policy decides where the collection is reachable from. The data access policy decides which IAM principals may create indexes and read or write documents.
- The vector index. For a vector store you set up yourself, the index must exist before the knowledge base is created, and
CfnCollectiondoes not create it.CfnIndex(AWS::OpenSearchServerless::Index) does, and it runs with the principal CloudFormation deploys with, so that role needs a data access rule as well. - Role permissions. The knowledge base role calls the embedding model (
bedrock:InvokeModel) and the collection API (aoss:APIAccessAll) besides reading the bucket.
The stack below wires all four. It follows the shape of AWS’s own OpenSearch Serverless quickstart template, including public network access, which suits a first deploy; move the collection behind a VPC endpoint for production.
import json
from aws_cdk import (
aws_bedrock as bedrock,
aws_iam as iam,
aws_opensearchserverless as opensearch,
aws_s3 as s3,
)
collection_name = "rag-vectors"
index_name = "bedrock-knowledge-base-index"
embedding_model_arn = (
f"arn:aws:bedrock:{self.region}::foundation-model/amazon.titan-embed-text-v2:0"
)
# Role CloudFormation deploys with (default CDK bootstrap); CfnIndex runs as it
deploy_role_arn = (
f"arn:aws:iam::{self.account}:role/"
f"cdk-hnb659fds-cfn-exec-role-{self.account}-{self.region}"
)
# S3 bucket for documents
docs_bucket = s3.Bucket(
self, "DocsBucket",
versioned=True,
encryption=s3.BucketEncryption.S3_MANAGED,
)
# IAM role for the knowledge base
kb_role = iam.Role(
self, "KBRole",
assumed_by=iam.ServicePrincipal("bedrock.amazonaws.com"),
)
# 1. Encryption policy: must exist before the collection
encryption_policy = opensearch.CfnSecurityPolicy(
self, "VectorEncryptionPolicy",
name=f"{collection_name}-enc",
type="encryption",
policy=json.dumps({
"Rules": [{"ResourceType": "collection", "Resource": [f"collection/{collection_name}"]}],
"AWSOwnedKey": True, # or "KmsARN": "<key arn>" for a customer-managed key
}),
)
# 2a. Network policy (public endpoint; prefer a VPC endpoint in production)
network_policy = opensearch.CfnSecurityPolicy(
self, "VectorNetworkPolicy",
name=f"{collection_name}-net",
type="network",
policy=json.dumps([{
"Rules": [{"ResourceType": "collection", "Resource": [f"collection/{collection_name}"]}],
"AllowFromPublic": True,
}]),
)
# 2b. Data access policy: the deploy role manages the index, the KB role reads and writes
data_access_policy = opensearch.CfnAccessPolicy(
self, "VectorDataAccessPolicy",
name=f"{collection_name}-data",
type="data",
policy=json.dumps([
{
"Rules": [
{
"ResourceType": "index",
"Resource": [f"index/{collection_name}/*"],
"Permission": [
"aoss:CreateIndex", "aoss:DescribeIndex",
"aoss:UpdateIndex", "aoss:DeleteIndex",
],
},
],
"Principal": [deploy_role_arn],
},
{
"Rules": [
{
"ResourceType": "collection",
"Resource": [f"collection/{collection_name}"],
"Permission": ["aoss:DescribeCollectionItems"],
},
{
"ResourceType": "index",
"Resource": [f"index/{collection_name}/*"],
"Permission": [
"aoss:DescribeIndex", "aoss:ReadDocument", "aoss:WriteDocument",
],
},
],
"Principal": [kb_role.role_arn],
},
]),
)
vector_collection = opensearch.CfnCollection(
self, "VectorCollection",
name=collection_name,
type="VECTORSEARCH",
)
vector_collection.node.add_dependency(encryption_policy)
vector_collection.node.add_dependency(network_policy)
# 3. Vector index: field names must match the KB field mapping below
vector_index = opensearch.CfnIndex(
self, "VectorIndex",
collection_endpoint=vector_collection.attr_collection_endpoint,
index_name=index_name,
settings=opensearch.CfnIndex.IndexSettingsProperty(
index=opensearch.CfnIndex.IndexProperty(knn=True),
),
mappings=opensearch.CfnIndex.MappingsProperty(
properties={
"embedding": opensearch.CfnIndex.PropertyMappingProperty(
type="knn_vector",
dimension=1024, # Titan Text Embeddings V2 default
method=opensearch.CfnIndex.MethodProperty(
name="hnsw", engine="faiss", space_type="l2",
),
),
"text": opensearch.CfnIndex.PropertyMappingProperty(type="text"),
"metadata": opensearch.CfnIndex.PropertyMappingProperty(
type="text", index=False,
),
},
),
)
vector_index.node.add_dependency(data_access_policy, vector_collection)
# 4. Role permissions beyond the bucket
docs_bucket.grant_read(kb_role)
kb_role.add_to_policy(iam.PolicyStatement(
actions=["bedrock:InvokeModel"],
resources=[embedding_model_arn],
))
kb_role.add_to_policy(iam.PolicyStatement(
actions=["aoss:APIAccessAll"],
resources=[vector_collection.attr_arn],
))
# Bedrock Knowledge Base
kb = bedrock.CfnKnowledgeBase(
self, "RAGKnowledgeBase",
name="production-rag-kb",
role_arn=kb_role.role_arn,
knowledge_base_configuration=bedrock.CfnKnowledgeBase.KnowledgeBaseConfigurationProperty(
type="VECTOR",
vector_knowledge_base_configuration=bedrock.CfnKnowledgeBase.VectorKnowledgeBaseConfigurationProperty(
embedding_model_arn=embedding_model_arn
)
),
storage_configuration=bedrock.CfnKnowledgeBase.StorageConfigurationProperty(
type="OPENSEARCH_SERVERLESS",
opensearch_serverless_configuration=bedrock.CfnKnowledgeBase.OpenSearchServerlessConfigurationProperty(
collection_arn=vector_collection.attr_arn,
vector_index_name=index_name,
field_mapping=bedrock.CfnKnowledgeBase.OpenSearchServerlessFieldMappingProperty(
vector_field="embedding",
text_field="text",
metadata_field="metadata"
)
)
)
)
# The index and the role's policy must exist before the KB is created
kb.node.add_dependency(vector_index)
kb.node.add_dependency(kb_role)
# Data source (S3)
data_source = bedrock.CfnDataSource(
self, "S3DataSource",
name="s3-docs",
knowledge_base_id=kb.attr_knowledge_base_id,
data_source_configuration=bedrock.CfnDataSource.DataSourceConfigurationProperty(
type="S3",
s3_configuration=bedrock.CfnDataSource.S3DataSourceConfigurationProperty(
bucket_arn=docs_bucket.bucket_arn
)
)
)
Evaluation with RAGAS: Measuring RAG Quality#
With RAG systems, improvement requires measurement. The RAGAS framework scores retrieval and generation quality with an LLM acting as the judge. Faithfulness and answer relevancy need only the question, the answer and the retrieved context; context precision and context recall also need a reference answer written by a person.
Core Metrics#
Retrieval Metrics:
- Context Precision: Are relevant chunks ranked higher than irrelevant ones?
- Context Recall: Did we retrieve all necessary information?
- Context Relevancy: How much of retrieved content is actually relevant?
Generation Metrics:
- Faithfulness: Is the answer factually grounded in the retrieved context?
- Answer Relevancy: Does the answer address the question?
Implementation#
The example pins ragas==0.4.4. That release marks the older evaluate() entry point and the metric imports from ragas.metrics as deprecated, so the code uses the metric classes from ragas.metrics.collections. Each one takes a judge model and scores a single sample through ascore, or a list of samples through abatch_score.
# pip install "ragas==0.4.4" openai
import asyncio
from statistics import mean
from openai import AsyncOpenAI
from ragas.embeddings.base import embedding_factory
from ragas.llms.base import llm_factory
from ragas.metrics.collections import (
AnswerRelevancy,
ContextPrecision,
ContextRecall,
Faithfulness,
)
client = AsyncOpenAI()
judge = llm_factory("gpt-4o-mini", client=client)
embeddings = embedding_factory("openai", model="text-embedding-3-small", client=client)
samples = [
{
"user_input": "What are AWS Lambda cold start optimization techniques?",
"response": "Lambda cold starts can be optimized using provisioned concurrency, which keeps functions initialized, and SnapStart for Java functions which reduces initialization time.",
"retrieved_contexts": [
"Provisioned concurrency keeps Lambda functions initialized and ready to respond.",
"SnapStart reduces cold start times for Java functions by caching initialized state.",
"Function optimization like reducing package size improves cold start performance.",
],
"reference": "Provisioned concurrency, SnapStart, and function optimization reduce Lambda cold starts",
},
{
"user_input": "How does DynamoDB handle partition keys?",
"response": "DynamoDB uses partition keys to distribute data across partitions. High-cardinality partition keys ensure even distribution and optimal performance.",
"retrieved_contexts": [
"Partition keys determine how DynamoDB distributes data across partitions.",
"Choose high-cardinality partition keys to ensure even data distribution.",
"Low-cardinality keys create hot partitions and throttling.",
],
"reference": "Partition keys distribute data; high cardinality ensures even distribution",
},
]
# Each metric reads only the fields its ascore() signature names
METRICS = {
"context_precision": (ContextPrecision(llm=judge), ("user_input", "reference", "retrieved_contexts")),
"context_recall": (ContextRecall(llm=judge), ("user_input", "retrieved_contexts", "reference")),
"faithfulness": (Faithfulness(llm=judge), ("user_input", "response", "retrieved_contexts")),
"answer_relevancy": (
AnswerRelevancy(llm=judge, embeddings=embeddings),
("user_input", "response"),
),
}
async def score_samples(samples, metrics=METRICS):
"""Average each metric over the samples."""
report = {}
for name, (metric, fields) in metrics.items():
inputs = [{field: sample[field] for field in fields} for sample in samples]
results = await metric.abatch_score(inputs)
report[name] = mean(result.value for result in results)
return report
scores = asyncio.run(score_samples(samples))
for name, value in scores.items():
print(f"{name}: {value:.3f}")
There is no single RAGAS score to print. Each metric answers a different question, so track them side by side and let the weakest one decide where to work.
Every number above is an LLM verdict, and that has a price. Per sample, context precision makes one judge call per retrieved chunk, context recall makes one, faithfulness makes two (split the answer into statements, then judge those statements against the context), and answer relevancy makes three by default plus embedding calls. The two samples above, with three chunks each, cost about 18 judge calls. That is why the judge is a small model and why production scoring runs on a sample of traffic.
Production Monitoring#
Live traffic has no reference answers, so the monitor keeps to the metrics that work without one: faithfulness, answer relevancy, and ContextUtilization, the reference-free variant of context precision that judges each chunk against the generated answer.
import wandb
from ragas.metrics.collections import ContextUtilization
PRODUCTION_METRICS = {
"faithfulness": METRICS["faithfulness"],
"answer_relevancy": METRICS["answer_relevancy"],
"context_utilization": (
ContextUtilization(llm=judge),
("user_input", "response", "retrieved_contexts"),
),
}
wandb.init(project="rag-production-monitoring")
def monitor_rag_quality(samples):
"""Score a sample of production queries and alert on drops"""
scores = asyncio.run(score_samples(samples, PRODUCTION_METRICS))
wandb.log(scores)
# Alert on quality degradation
if scores["faithfulness"] < 0.7:
send_alert("Faithfulness dropped below threshold!")
if scores["context_utilization"] < 0.6:
send_alert("Context utilization degraded!")
return scores
# Each sample carries user_input, response and retrieved_contexts from your logs
daily_scores = monitor_rag_quality(get_sample_queries())
Metric Interpretation#
Sample scores and what each one tells you to change:
Context Precision (0.85)
- 85% of relevant documents appear in top positions
- Good ranking quality
- Lower scores indicate irrelevant results rank too highly
Context Recall (0.75)
- 75% of necessary information was retrieved
- Missing 25% of relevant content
- Increase numberOfResults or improve chunking
Faithfulness (0.92)
- 92% of answer claims are supported by context
- Low hallucination rate
- Below 0.7 indicates serious problems
Answer Relevancy (0.88)
- Answer addresses 88% of question intent
- Minimal tangential information
- Below 0.7 suggests answer drift
Production Trade-offs: The Iron Triangle#
Every RAG architecture decision balances three competing factors: latency, cost, and accuracy. Improving one usually spends the other two.
Latency Breakdown#
The instinct is to blame generation for a slow RAG endpoint. In a single-pass pipeline that is roughly true. In configurations that re-retrieve and re-grade, it stops being true: retrieval runs several times, and each round appends its documents to the prompt, so prefill grows with every pass. Generation ends up a minority share of the wall clock.
Measure that split before tuning anything. If retrieval and prefill dominate, swapping the generation model changes very little.
Strategy-Specific Trade-offs#
| Strategy | Latency | Cost | Where the gain lands |
|---|---|---|---|
| Basic vector search | Baseline | Baseline | Baseline |
| Hybrid search | One extra keyword pass, concurrent on async paths | A second index to maintain | Exact terms, names, abbreviations |
| Cross-encoder reranking | One inference per candidate | Reranker model calls | Ordering of the top-k |
| Multi-query (RAG-Fusion) | One retrieval per rewrite | Rewrite plus retrieval per query | Ambiguous or underspecified queries |
| GraphRAG | Mostly paid at ingestion | LLM extraction over the whole corpus | Multi-hop and global questions |
| Parent-child retrieval | Docstore lookup after the vector hit | Corpus stored twice | Context coherence |
| Self-RAG/CRAG | Grading pass plus optional web search | One grading call per document | Faithfulness when retrieval is unreliable |
Optimization Strategies#
1. Caching Strategy
from functools import lru_cache
import hashlib
import time
# Cache embeddings
@lru_cache(maxsize=10000)
def get_embedding(text: str):
return embedding_model.embed(text)
# Cache retrieval results
class RAGCache:
def __init__(self, ttl_seconds=3600):
self.cache = {}
self.ttl = ttl_seconds
def get_cache_key(self, query: str, k: int):
return hashlib.md5(f"{query}:{k}".encode()).hexdigest()
def get(self, query: str, k: int):
key = self.get_cache_key(query, k)
if key in self.cache:
result, timestamp = self.cache[key]
if time.time() - timestamp < self.ttl:
return result
return None
def set(self, query: str, k: int, result):
key = self.get_cache_key(query, k)
self.cache[key] = (result, time.time())
# Usage
cache = RAGCache(ttl_seconds=3600)
def cached_retrieval(query: str, k: int = 10):
cached_result = cache.get(query, k)
if cached_result is None:
result = retriever.invoke(query)
cache.set(query, k, result)
return result
return cached_result
2. Model Routing
class AdaptiveRAG:
def __init__(self):
# Use cheap models for auxiliary tasks
self.router_model = "gpt-4o-mini"
self.grader_model = "gpt-4o-mini"
# Use powerful model only for generation
self.generator_model = "claude-sonnet-4-6-20250217"
def classify_query_complexity(self, query: str):
"""Route with small model"""
prompt = f"Is this a simple or complex query? {query}"
response = openai.chat.completions.create(
model=self.router_model,
messages=[{"role": "user", "content": prompt}],
max_tokens=10
)
return response.choices[0].message.content
def grade_documents(self, query: str, docs: list):
"""Grade with small model"""
# Grading logic using self.grader_model
pass
def generate_answer(self, query: str, docs: list):
"""Generate with powerful model"""
# Generation logic using self.generator_model
pass
Routing and grading dominate the call count in this pipeline while generation dominates the token count. Putting the small model on the frequent calls is where the savings sit, and the answer the user reads still comes from the strong model.
Production Decision Framework#
Low Latency Priority (<1s response):
- Basic vector search or lightweight hybrid
- Avoid multi-query patterns and heavy reranking
- Aggressive caching
- Quantized embedding models
- HNSW indexing with tuned parameters
High Accuracy Priority (>90% faithfulness):
- Hybrid search + cross-encoder reranking
- CRAG for quality verification
- Hierarchical/parent-child chunking
- GraphRAG for multi-hop queries
- Accept higher latency and cost
Cost-Constrained:
- Smaller embedding models
- Limited numberOfResults (5-10)
- Avoid multi-query patterns
- Use cheap LLMs for routing/grading
- Aggressive caching
- Approximate indexing (IVF+PQ)
Balanced Approach (Most Common):
- Hybrid search with RRF
- Lightweight reranking
- Parent-child chunking
- Moderate numberOfResults (10-15)
- Selective caching
- Mid-tier models (Claude Haiku, GPT-4o-mini)
The Default and Its Limits#
Hybrid search with parent-child chunking is the configuration to reach for first. It repairs the two failures that hurt earliest, missed exact terms and fragmented context, and it needs no infrastructure beyond a keyword index. Everything else is an override, and each one has a condition attached. Cross-encoder reranking earns its inference budget when precision on domain-specific queries is the binding constraint. CRAG earns its extra passes when retrieval quality is unpredictable and a confident wrong answer is expensive. GraphRAG earns its ingestion bill only when questions genuinely span documents and the corpus is large enough to hold real relationships; below a thousand documents, or with mostly single-hop questions, it takes more than it returns.
Stand up RAGAS evaluation before any of these, because without a faithfulness and context-precision baseline every escalation looks like progress.
References#
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (opens in new tab) - Original RAG paper by Lewis et al. (2020), NeurIPS
- LangChain Retrieval Documentation (opens in new tab) - Official LangChain guide to building RAG systems
- LlamaIndex RAG - Introduction (opens in new tab) - LlamaIndex framework guide for retrieval and indexing
- Contextual Retrieval - Anthropic (opens in new tab) - Anthropic research on contextual chunking; 49-67% failure reduction
- OpenAI Vector Embeddings Guide (opens in new tab) - Embedding model documentation including Matryoshka truncation
- LangChain EnsembleRetriever source (opens in new tab) - Weighted RRF with
c=60; sequentialinvoke, concurrentainvoke - OpenSearch normalization processor (opens in new tab) - Hybrid query score normalization,
min_maxby default - Knowledge base query configuration - Amazon Bedrock (opens in new tab) - Default of five source chunks and the vector stores that support hybrid search
- Supported Regions and models for reranking - Amazon Bedrock (opens in new tab) - Region table for Amazon Rerank 1.0 and Cohere Rerank 3.5
- Prerequisites for your own vector store - Amazon Bedrock (opens in new tab) - The vector index and field mappings must exist before the knowledge base
- Service role for Amazon Bedrock Knowledge Bases (opens in new tab) - Model invocation, data source and
aoss:APIAccessAllpermissions - Using CloudFormation to create OpenSearch Serverless collections (opens in new tab) - Sample template with encryption, network and data access policies
- AWS::OpenSearchServerless::Index (opens in new tab) - CloudFormation resource that creates the vector index
- GraphCypherQAChain source - langchain-neo4j (opens in new tab) - The
allow_dangerous_requestsopt-in and the narrowly scoped credentials note - Built-in roles - Neo4j Operations Manual (opens in new tab) - The read-only
readerrole, documented for Enterprise Edition, AuraDB Business Critical and AuraDB Virtual Dedicated Cloud - Introduction - Neo4j Operations Manual (opens in new tab) - Edition comparison; role-based access control is an Enterprise Edition feature
- Microsoft GraphRAG (opens in new tab) - Leiden community detection, community summaries, global and local search
- Ragas metrics (opens in new tab) - Definitions of context precision, context recall, faithfulness and response relevancy
Related posts
What a Bedrock Knowledge Base really is, which data sources and vector stores are first-class, and why the console default rarely fits a small corpus.
aws · aws-bedrock · rag +3
Preparing data for RAG systems: document parsing, token-based chunking, contextual enrichment with prompt caching, embedding choice and keeping the index in sync
rag · llm · embeddings +2
An implementation-focused glossary for developers navigating the AI/LLM landscape - from tokens to agents, RAG to fine-tuning, with code examples.
llm · ai-agents · rag +6
A RAG agent on Bedrock Knowledge Bases and OpenSearch Serverless with TypeScript CDK: automated ingestion, a retrieval test, and how to narrow IAM and lock the chat URL.
aws-bedrock · aws-cdk · rag +3
A practical 6-level framework for enterprise AI integration: when to use ChatGPT, RAG, MCP agents, or fine-tuning, with a focus on PII and finance compliance.
ai-adoption-strategy · rag · mcp +2