Building RAG with Bedrock Knowledge Bases and Retrieval APIs
This lesson explains how Amazon Bedrock Knowledge Bases provide a fully managed RAG workflow, from data sources to retrieval and cited answers, and the APIs applications use.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the components of a knowledge base.
- Choose between Retrieve and RetrieveAndGenerate.
- Integrate knowledge bases with agents and flows.
Components of a knowledge base
A knowledge base combines an embedding model, a vector store and one or more data sources. The vector store can be created automatically (for example an OpenSearch Serverless collection, an Aurora PostgreSQL cluster or an S3 vector bucket) or an existing store can be used, such as OpenSearch Service managed clusters, Aurora PostgreSQL, Neptune Analytics, Amazon S3 Vectors or supported third-party stores. Each data source has its own parsing and chunking configuration. A service role grants the knowledge base access to the sources, the embedding model and the store, and data can be encrypted with customer managed AWS KMS keys.
During ingestion, a sync job reads documents from the data source, parses them, splits them into chunks, embeds each chunk and writes vectors, text and metadata to the store. During retrieval, the query is embedded and the most similar chunks are returned with scores and source locations.
The following diagram shows ingestion and a query passing through a Bedrock knowledge base.

From document to answer
Retrieve and RetrieveAndGenerate
The Retrieve API returns the most relevant chunks for a query, with their text, score, source location and metadata. The application then builds its own prompt, which gives full control over the prompt, model and post-processing, and suits agents and custom pipelines. Options include the number of results, the search type (semantic or hybrid where the store supports it), metadata filters, implicit filtering and reranking.
The RetrieveAndGenerate API retrieves chunks and generates an answer with a chosen model in one call, returning the answer with citations that link each part of the answer to source chunks. It accepts a custom prompt template with a $search_results$ placeholder, a guardrail, inference parameters, query decomposition, and a sessionId that keeps conversation context across turns. RetrieveAndGenerateStream streams the answer.
import boto3
agent_rt = boto3.client("bedrock-agent-runtime")
r = agent_rt.retrieve_and_generate(
input={"text": "What is the notice period for contractors in Germany?"},
retrieveAndGenerateConfiguration={
"type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": "KB12345678",
"modelArn": "arn:aws:bedrock:eu-central-1:111122223333:inference-profile/eu.amazon.nova-pro-v1:0",
"retrievalConfiguration": {"vectorSearchConfiguration": {"numberOfResults": 6, "overrideSearchType": "HYBRID"}},
"generationConfiguration": {"guardrailConfiguration": {"guardrailId": "gr-abc123", "guardrailVersion": "3"}},
},
},
)
print(r["output"]["text"])
for c in r["citations"]:
print([ref["location"] for ref in c["retrievedReferences"]])The following diagram compares the two retrieval APIs.

Retrieve or RetrieveAndGenerate?
Integrating knowledge bases with agents and flows
A knowledge base is rarely used on its own; it supplies knowledge to other parts of an application. It can be associated with an Amazon Bedrock agent, which decides from the conversation when to query it, so that retrieval happens only when the question needs company knowledge. It can be a node in Amazon Bedrock Flows, where a workflow retrieves passages at a fixed step. It can be exposed as a tool through an MCP server or AgentCore Gateway, so that agents built with other frameworks can use it. Or it can be called directly from application code with the Retrieve API.
Because retrieval quality determines answer quality, knowledge bases should also be evaluated. Amazon Bedrock Evaluations includes RAG evaluation jobs that score a knowledge base's retrieval, for example context relevance and coverage, and its generated answers, for example correctness and faithfulness. The following table compares the two retrieval APIs.
| API | Returns | Choose when |
|---|---|---|
| Retrieve | Chunks with scores, sources and metadata | Custom prompts, agents, multi-step pipelines |
| RetrieveAndGenerate | Generated answer with citations, session context | Managed question answering with least code |
| RetrieveAndGenerateStream | Streamed answer with citations | Interactive chat with low perceived latency |
Least-code RAG with citations
A company needs an internal assistant that answers from documents in S3, shows sources and remembers context within a conversation, with the least code.
A knowledge base with an S3 data source and the RetrieveAndGenerate API, passing the returned sessionId on follow-up questions, provides answers, citations and conversation context. Using Retrieve and building prompts manually would add code without a stated benefit.
Which API returns only chunks, so the application can build its own prompt?
Show answer
Retrieve. RetrieveAndGenerate returns a generated answer with citations.
Exam Tips
Remember for the exam
- A knowledge base = embedding model + vector store + data sources.
- Retrieve returns chunks for custom prompts; RetrieveAndGenerate returns answers with citations.
- Pass sessionId to keep conversation context in RetrieveAndGenerate.
- Use Knowledge Bases from agents, Flows, MCP tools or application code.