Sitemap
AWS Tip

Best AWS, DevOps, Serverless, and more from top Medium writers .

Getting Started with Amazon Bedrock AgentCore Knowledge Bases

18 min readOct 12, 2026

--

Press enter or click to view image in full size

Introduction

AI agents are only as good as the data they can access. A chatbot trained on general knowledge cannot answer questions about your company’s internal documentation, product manuals, or policies. Retrieval-Augmented Generation (RAG) solves this by letting agents search your private data and ground their responses in real, verifiable information.

Amazon Bedrock Knowledge Bases is a fully managed service that implements RAG pipelines for AI agents. It handles the entire lifecycle: ingesting documents, converting them into vector embeddings, storing them in a searchable index, and retrieving relevant passages when an agent needs them.

This article is part of a series I am writing about different aspects of Amazon Bedrock AgentCore. Here, we focus on how to use Knowledge Bases to give agents access to your private data. For a broader understanding of AgentCore and its components, check out the other articles in this series:

Knowledge Base Types

Amazon Bedrock offers three types of Knowledge Bases, each designed for different data patterns and operational requirements:

Bedrock Managed Knowledge Base is the fully managed option. AWS handles the entire RAG pipeline: ingestion, vector store, embedding model, reranking, and retrieval optimization. It comes with built-in embedding and reranking models at no extra cost, supports 7 native data source connectors (S3, SharePoint, Confluence, Web Crawler, Google Drive, OneDrive, Custom), and integrates natively with AgentCore Gateway. It also supports Agentic Retrieval, a multi-step retrieval capability that reasons across your data to answer complex queries. This is the recommended option for most use cases.

Unstructured Vector Store Knowledge Base (Customer-managed) gives you full control over the vector store infrastructure. You provision and manage your own vector database (Amazon OpenSearch Serverless, Aurora, Neptune, Pinecone, Redis Enterprise Cloud, MongoDB Atlas, or Amazon S3 Vectors). You choose the embedding model and search strategy. This option is best when you have specific infrastructure requirements or need direct access to the vector database. It does not support Agentic Retrieval or native AgentCore Gateway integration.

Structured Data Store Knowledge Base (Customer-managed) connects to tabular and relational data through the Amazon Redshift query engine. Instead of vector embeddings, it converts natural language queries into SQL to retrieve data from Amazon Redshift or AWS Glue Data Catalog (Lake Formation). This option is ideal for scenarios where your data is structured (tables, databases) and you need to answer analytical questions over it.

Comparison Summary

  • Managed KB: Zero infrastructure management, built-in embedding/reranking, Agentic Retrieval, Gateway integration. Best for most RAG use cases.
  • Unstructured Vector Store KB: Full control over vector DB, custom search strategies, broader vector store options. Best for custom infrastructure requirements.
  • Structured Data Store KB: SQL-based retrieval from Redshift/Glue, no vector embeddings needed. Best for tabular/relational data analytics.

In this article, we focus exclusively on the Bedrock Managed Knowledge Base. We will create an S3 bucket with car-related documents, build a Managed KB that indexes those documents, query it locally with Python, and then integrate it with our car dealership Harness through AgentCore Gateway.

Prerequisites

  • AWS account with credentials configured
  • IAM user or role with permissions to create S3 buckets, IAM roles, and use Amazon Bedrock AgentCore
  • Python 3.12 installed
  • AWS Region where AgentCore is available (us-east-1, us-west-2, ap-southeast-2, or eu-central-1)
  • A Harness already created (from the first article in this series)

Step 1: Create an S3 Bucket with Car Documents

We need source documents for the Knowledge Base to ingest. In this step, we create an S3 bucket and upload text files about cars that connect to our used car dealership scenario.

1.1 Create the S3 Bucket

1.1.1. Navigate to Amazon S3 > Buckets in the AWS Console

1.1.2. Click Create bucket

1.1.3. In the Bucket name field, enter a unique name (example: agentcore-kb-cars-<your-account-id>)

1.1.4. Select the same region where you will create the Knowledge Base (e.g., us-east-1)

1.1.5. Leave all remaining settings at their defaults

1.1.6. Click Create bucket

1.2 Create the Documents

The companion repository contains sample markdown files about cars in the demos/ directory:

  • suv-vs-sedan-guide.md — Comprehensive comparison between SUVs and sedans for American families, including pricing, fuel economy, and recommendations by budget range.
  • car-maintenance-tips.md — Maintenance guidelines for used vehicle owners covering oil changes, tires, brakes, battery, fluids, seasonal preparation, and electric/hybrid-specific maintenance.
  • financing-guide.md — Used car financing guide with interest rates by credit score, loan term recommendations, down payment strategies, and total cost of ownership calculations.
  • powertrain-comparison-guide.md — Detailed comparison of combustion (ICE), battery electric (BEV), standard hybrid (HEV), and plug-in hybrid (PHEV) vehicles, including cost analysis over 5 years.

Clone the repository to get these files:

git clone https://github.com/biagolini/PythonAwsBedrockAgentCoreKnowledgeBases.git

The documents are located in the demos/ folder. We use markdown format to enable future exploration of advanced chunking strategies that leverage document structure (headings, lists, sections).

1.3 Upload the Documents to S3

1.3.1. Navigate to your newly created bucket in the S3 console

1.3.2. Click Upload

1.3.3. Upload all the markdown files from the demos/ folder of the companion repository

1.3.4. Click Upload to confirm

The files can be placed directly in the root of the bucket. If you prefer, you can organize them into folders (prefixes). The S3 connector supports optional inclusion and exclusion prefix filters to scope ingestion to specific paths. For this tutorial, we keep all files in the root for simplicity.

Press enter or click to view image in full size
Amazon S3 console showing a bucket containing the uploaded markdown files used as data source for the Knowledge Base
S3 bucket after uploading the sample markdown documents from the demos folder that will be ingested into the Managed Knowledge Base

Step 2: Create the Managed Knowledge Base

Now we create a Bedrock Managed Knowledge Base and connect it to our S3 bucket. The console wizard handles the creation of the Knowledge Base and the data source in a single flow.

2.1 Navigate to Knowledge Base

2.1.1. Open the Amazon Bedrock AgentCore console

2.1.2. In the left navigation pane, under Built-in tools, click Knowledge Base

2.1.3. Click the Create Managed Knowledge Base button

Press enter or click to view image in full size
Amazon Bedrock AgentCore console showing the left navigation pane with Knowledge Base selected under Built-in tools and the Create Managed Knowledge Base button
Starting point for creating a Managed Knowledge Base in the AgentCore console under Built-in tools

2.2 Configure Knowledge Base Details

2.2.1. In the Knowledge Base name field, enter car_kb

2.2.2. Expand Additional configurations to review the default settings:

  • Description: Enter Used car dealership knowledge base with vehicle guides and financing info
  • Embedding model type: Keep Managed (default). AWS provides a built-in embedding model optimized for accuracy and performance at no extra cost.
  • IAM permissions: Keep Create and use a new service role (recommended). The console will create a role with the necessary permissions to access your S3 bucket and manage the knowledge base.
  • Encryption: Keep the default AWS managed key.
Press enter or click to view image in full size
Create Managed Knowledge Base form showing the Knowledge Base name field, description, embedding model type set to Managed, and IAM permissions section
Configuration of the Knowledge Base name, description, and additional settings including the managed embedding model and automatic IAM role creation

2.3 Configure the Data Source

2.3.1. Under Data source, in the Data source name field, enter car-documents-s3

2.3.2. In the Data source type dropdown, select Amazon S3

2.3.3. In the S3 URI field, enter the path to your bucket:

s3://<your-bucket-name>/

Replace <your-bucket-name> with the actual bucket name you created in Step 1 (e.g., agentcore-kb-cars-<your-account-id>).

Press enter or click to view image in full size
Data source section of the Knowledge Base creation form showing the data source name field, Amazon S3 selected as type, and the S3 URI configuration
Configuring the Amazon S3 data source connector by providing the bucket URI where the car dealership documents are stored

2.4 Configure Parsing and Chunking (Optional)

2.4.1. Expand Content parsing and chunking to review the defaults:

  • Parsing strategy: Keep Managed parser (default). This automatically determines the optimal parsing strategy for each file type.
  • Text chunking strategy: Keep Default chunking (recommended). This splits text into fixed-size chunks optimized for retrieval.

For this tutorial, the defaults work well with our text files. For production scenarios with complex documents (PDFs with tables, images, or multi-column layouts), you might consider fixed-size chunking with custom parameters.

2.5 Create the Knowledge Base

2.5.1. Click Create Knowledge Base

2.5.2. Wait for the knowledge base and data source to be created. This typically takes 2–5 minutes. The status will show Creating and then change to Available once ready.

Press enter or click to view image in full size
Amazon Bedrock AgentCore console showing the Knowledge Base status as Creating while AWS provisions the resources
The Knowledge Base creation process takes 2–5 minutes while AWS provisions the managed vector store and ingestion pipeline

2.6 Sync the Data Source

After the knowledge base is created, you need to sync (ingest) the documents from S3 into the knowledge base.

2.6.1. On the knowledge base details page, navigate to the Data sources section

2.6.2. Select the car-documents-s3 data source

2.6.3. Click the Sync button to start the ingestion process

2.6.4. Wait for the sync to complete. The status will change to Syncing and then back to Available. For three small text files, this should take less than a minute.

Press enter or click to view image in full size
Knowledge Base details page showing the data source section with the Sync button and the sync status indicating documents are being ingested
Triggering a data source sync to ingest the uploaded S3 documents into the Knowledge Base vector store

After the sync completes, your documents are indexed and the knowledge base is ready to be queried.

2.7 Test the Knowledge Base in the Console

2.7.1. On the knowledge base details page, look for the Test section or button

2.7.2. Before running the test, review the Retrieval and response generation settings. Select the model you want to use for response generation. For this tutorial, I selected Amazon Nova Micro to test the stack with a minimal model and verify that the retrieval pipeline works correctly even with a very small model. You can choose a larger model for better response quality.

2.7.3. Enter a test query such as:

What are the main differences between SUVs and sedans for families?

2.7.4. Click Run and observe the generated response. At the bottom of the response, notice the Trace details button.

Press enter or click to view image in full size
Test Knowledge Base panel showing Configurations on the left with Agentic retrieval with answer generation selected, Nova Micro model, and Maximum agentic iterations set to 5, and on the right the Preview with the generated response and the Trace details button highlighted with an orange arrow
Testing the Knowledge Base with a query about SUVs vs sedans, showing the Agentic Retrieval configuration and the Trace details button that reveals the retrieval process internals

2.7.5. Click the Trace details button (or toggle the Details switch in the top-right corner of the Test panel). This opens the trace view where you can inspect how the retrieval was performed.

Inside the trace details, you will see two main sections:

  • Speculative retrieval — This is an initial retrieval that runs before the planning step to reduce latency. For a single knowledge base, the service immediately retrieves results using your raw query while the foundation model is still analyzing and planning sub-queries. This optimization means results are already available when the planning step completes, significantly reducing total response time.
  • Iteration — Each iteration represents one planning-and-retrieval cycle. The foundation model decomposes your query into sub-queries, executes them against the knowledge base, and evaluates whether the retrieved results are sufficient to answer the original question. If not, it plans and executes additional iterations (up to the configured maximum). For simple queries, a single iteration is usually enough.

For more details on how Agentic Retrieval works, see the AWS documentation.

2.7.6. Expand the Iteration section to see the individual chunks retrieved. Each chunk shows a portion of text from your documents. Click Metadata associated with this chunk to see the document ID and source file name (e.g., suv-vs-sedan-guide.md).

Press enter or click to view image in full size
Test panel with Details toggle enabled showing Speculative retrieval section, Iteration 1 expanded with Query 1, Chunk 1 containing text from the SUV vs Sedan guide, and the Metadata associated with this chunk section showing Doc ID and Filename suv-vs-sedan-guide.md
Detailed trace view showing how the Knowledge Base decomposed the query into iterations, retrieved relevant text chunks, and associated metadata including the source document filename

2.8 Note the Knowledge Base ID

After creation, copy the Knowledge Base ID from the details page. You will need this for the Gateway configuration and local testing. The ID follows a pattern like XXXXXXXXXX.

Press enter or click to view image in full size
Knowledge Base overview page showing the name car_kb, status Available, service role, embeddings model Managed, vector dimensions 1024, Knowledge Base ID highlighted with an orange border, data source section, observability metrics, and log delivery configuration
Knowledge Base details page after successful creation, with the Knowledge Base ID highlighted for use in API calls and Gateway configuration

Step 3: Query the Knowledge Base Locally

Before integrating with the Harness, let us test the Knowledge Base programmatically using the Bedrock Agent Runtime API. This demonstrates how any application can query your knowledge base directly.

3.1 Clone the Repository and Install Dependencies

git clone https://github.com/biagolini/PythonAwsBedrockAgentCoreKnowledgeBases.git
cd PythonAwsBedrockAgentCoreKnowledgeBases
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

On Windows, replace the activation command with .venv\Scripts\activate.

3.2 Configure Environment Variables

cp .env.example .env

Edit .env:

AWS_REGION=us-east-1
KNOWLEDGE_BASE_ID=<your-knowledge-base-id>

Replace <your-knowledge-base-id> with the Knowledge Base ID you copied in Step 2.8.

3.3 Run the Retrieve Script

The script kb_retrieve.py demonstrates a simple retrieval query against the knowledge base. It sends a natural language query and receives relevant passages with source references.

import os
import boto3
from dotenv import load_dotenv

load_dotenv()

region = os.getenv("AWS_REGION", "us-east-1")
kb_id = os.getenv("KNOWLEDGE_BASE_ID")

if not kb_id:
raise ValueError("KNOWLEDGE_BASE_ID not set in .env file")

client = boto3.client("bedrock-agent-runtime", region_name=region)

query = "What SUVs are good for families with a budget under $25,000?"

print(f"Query: {query}\n")

response = client.retrieve(
knowledgeBaseId=kb_id,
retrievalQuery={"text": query},
retrievalConfiguration={
"managedSearchConfiguration": {
"numberOfResults": 3
}
}
)

print("--- Retrieved Passages ---\n")
for i, result in enumerate(response["retrievalResults"], 1):
content = result["content"]["text"]
source = result.get("location", {}).get("s3Location", {}).get("uri", "Unknown")
score = result.get("score", 0)
print(f"Result {i} (score: {score:.4f}):")
print(f" Source: {source}")
print(f" Content: {content[:200]}...")
print()

Run the script:

python kb_retrieve.py

Expected output:

Query: What SUVs are good for families with a budget under $25,000?

--- Retrieved Passages ---
Result 1 (score: 0.7049):
Source: https://<your-bucket-name>.s3.amazonaws.com/suv-vs-sedan-guide.md
Content: For buyers in the $20,000-$30,000 range (used vehicles, 2-4 years old):
### Used Mid-Size SUVs
- Toyota RAV4: $22,000-$28,000
- Honda CR-V: $21,000-$27,000
- Mazda CX-5: $20,000-$26,000...
Result 2 (score: 0.6043):
Source: https://<your-bucket-name>.s3.amazonaws.com/suv-vs-sedan-guide.md
Content: ### Drawbacks
The main drawbacks of SUVs are higher fuel consumption (typically 20-28 MPG for mid-size, 15-22 MPG for full-size), higher purchase price, and increased maintenance costs due to larger...
Result 3 (score: 0.5548):
Source: https://<your-bucket-name>.s3.amazonaws.com/suv-vs-sedan-guide.md
Content: # SUV vs Sedan: A Complete Guide for American Families
When choosing between an SUV and a sedan, American families face a decision that impacts daily comfort, safety, and long-term costs...

The knowledge base successfully retrieved relevant passages about SUVs, pricing, and financing from our documents. The results include confidence scores and source references, allowing the agent to cite its sources.

Step 4: Create a Gateway with the Knowledge Base Connector

To connect the Knowledge Base to the Harness, we need to expose it through AgentCore Gateway as an MCP tool. This follows the same pattern used in the Gateway article and the Web Search article: create a Gateway, add a connector target, and attach the Gateway to the Harness.

Get Carlos Biagolini’s stories in your inbox

Join Medium for free to get updates from this writer.

The Knowledge Base connector (bedrock-knowledge-bases) exposes two tools:

  • AgenticRetrieveStream — Multi-step agentic retrieval with streaming. The service plans, retrieves across multiple steps, and returns a synthesized answer with citations.
  • Retrieve — Single hybrid search that returns the most relevant passages with source references.

4.1 Create the Gateway Execution Role

4.1.1. Navigate to IAM > Roles in the AWS Console

4.1.2. Click Create role

4.1.3. Select Custom trust policy and enter the following trust policy:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAgentCoreToAssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "bedrock-agentcore.amazonaws.com"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"aws:SourceAccount": "<your-account-id>"
},
"ArnLike": {
"aws:SourceArn": "arn:aws:bedrock-agentcore:<your-region>:<your-account-id>:gateway/*"
}
}
}
]
}

4.1.4. Click Next. Skip managed policies. Click Next.

4.1.5. In the Role name field, enter AgentCoreGatewayKnowledgeBase

4.1.6. Click Create role

4.1.7. After creation, open the role and click Add permissions > Create inline policy. Select the JSON tab and enter:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetGateway",
"Effect": "Allow",
"Action": "bedrock-agentcore:GetGateway",
"Resource": "arn:aws:bedrock-agentcore:<your-region>:<your-account-id>:gateway/*"
},
{
"Sid": "ValidateKnowledgeBase",
"Effect": "Allow",
"Action": "bedrock:GetKnowledgeBase",
"Resource": "arn:aws:bedrock:<your-region>:<your-account-id>:knowledge-base/<your-kb-id>"
},
{
"Sid": "RetrieveFromKnowledgeBase",
"Effect": "Allow",
"Action": "bedrock:Retrieve",
"Resource": "arn:aws:bedrock:<your-region>:<your-account-id>:knowledge-base/<your-kb-id>"
},
{
"Sid": "AgenticRetrieveStream",
"Effect": "Allow",
"Action": "bedrock:AgenticRetrieveStream",
"Resource": "*"
}
]
}

Replace <your-region>, <your-account-id>, and <your-kb-id> with your actual values.

4.1.8. Click Next, name the policy GatewayKnowledgeBaseAccess, and click Create policy

4.2 Create the Gateway

Follow the same Gateway creation process described in the Gateway article, with these specific configurations:

4.2.1. Navigate to Amazon Bedrock AgentCore > Gateways

4.2.2. Click Create Gateway

Step 1 — Define gateway details:

4.2.3. In the Gateway name field, enter KnowledgeBaseGateway

4.2.4. Under Permissions, select Use another role and choose AgentCoreGatewayKnowledgeBase

4.2.5. Click Next

Press enter or click to view image in full size
First step of the AgentCore Gateway creation wizard showing the Gateway name field and Permissions section with the option to use the AgentCoreGatewayKnowledgeBase role
Configuring the Gateway name and selecting the IAM execution role that grants permissions to access the Knowledge Base

Step 2 — Configure Inbound Identity:

4.2.6. Under Inbound Auth type, select No authorization (for this tutorial). Click Next.

Press enter or click to view image in full size
Second step of the Gateway creation wizard showing Inbound Auth type options with No authorization selected for this tutorial
Configuring inbound identity as No authorization for simplicity in this tutorial, with IAM and JWT options available for production use

Step 3 — Add targets:

4.2.7. Under Select a target protocol, select MCP target

4.2.8. In the Target name field, enter CarDealershipKB

4.2.9. In the Target description field, enter Car dealership knowledge base with vehicle guides, maintenance tips, and financing information

4.2.10. Under Target type, select Connectors

4.2.11. Under Pre-configured targets, select Knowledge Bases (KB)

4.2.12. Under Runtime retrieval type, keep Standard retrieval selected

4.2.13. In the Knowledge Base (KB) dropdown, select car_kb

4.2.14. Under Outbound Auth configurations, keep IAM Role selected

4.2.15. In the Tool description field, enter:

Search the car dealership knowledge base for information about vehicle comparisons (SUVs, sedans, electric, hybrid), maintenance tips, and financing guides. Use this tool when the customer asks about car features, pricing, maintenance schedules, or financing options.

This description helps the agent understand when to invoke the Knowledge Base tool. A well-written tool description leads to more accurate tool selection.

4.2.16. Under Additional configurations (optional), you can customize retrieval behavior. For this tutorial, we leave the defaults, but these options are available:

  • Guardrail — Attach a Bedrock Guardrail to filter inputs and generated responses (not applied to raw retrieval results from the KB).
  • Reranking — Enable a secondary reranking model to improve results relevance. Options are: no reranking, default (service-managed reranker), or custom (select a specific model).
  • Agent overrides — Allow the agent to override default configurations at call time, such as Source chunks count (how many chunks to retrieve) and User context (for access control filtering).

4.2.17. Click Next

Press enter or click to view image in full size
Third step of the Gateway creation wizard showing the full target configuration with MCP target protocol selected, Connectors as target type, Knowledge Bases KB as pre-configured target, Standard retrieval type, car_kb selected, IAM Role for outbound auth, tool description field, and Additional configurations section with Guardrail, Reranking, and Agent overrides options
Complete target configuration for the Knowledge Base connector showing the MCP protocol, Standard retrieval type, the car_kb knowledge base selected, and the tool description that helps the agent understand when to use this tool

Step 4 — Review and create:

4.2.14. Review your configuration and click Create

4.2.15. Wait for both the Gateway and the target status to reach Ready

4.3 Note the Gateway ARN

After creation, copy the Gateway ARN from the Gateway details page. You will need it when connecting to the Harness.

4.4 Test the Gateway Locally

Before integrating with the Harness, you can test the Gateway locally using the Strands Agents SDK. The script kb_gateway_agent.py connects to the Knowledge Base through the Gateway via MCP protocol, discovers the retrieval tools, and lets the agent answer questions grounded in your documents.

4.4.1. Add the Gateway ID to your .env file:

GATEWAY_ID=<your-gateway-id>

4.4.2. The script kb_gateway_agent.py connects to the Knowledge Base through the Gateway via MCP protocol, discovers the retrieval tools, and lets the agent answer questions grounded in your documents.

import os
import sys
import logging
import warnings
from dotenv import load_dotenv

logging.getLogger("asyncio").setLevel(logging.CRITICAL)
warnings.filterwarnings("ignore", category=DeprecationWarning)

load_dotenv()

region = os.getenv("AWS_REGION", "us-east-1")
gateway_id = os.getenv("GATEWAY_ID")

if not gateway_id:
print("Error: GATEWAY_ID not set in .env file")
sys.exit(1)

gateway_url = f"https://{gateway_id}.gateway.bedrock-agentcore.{region}.amazonaws.com/mcp"

print(f"Connecting to Gateway: {gateway_url}\n")

from strands import Agent
from strands.models.bedrock import BedrockModel
from strands.tools.mcp import MCPClient
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client

mcp_client = MCPClient(lambda: aws_iam_streamablehttp_client(
endpoint=gateway_url,
aws_region=region,
aws_service="bedrock-agentcore",
))

model = BedrockModel(
model_id="us.amazon.nova-pro-v1:0",
region_name=region,
)

system_prompt = (
"You are a helpful sales assistant for a used car dealership. "
"Use the knowledge base tools to find information about vehicles, "
"maintenance, and financing. Always cite your sources."
)

prompt = "What are the main differences between SUVs and sedans for families? What do you recommend for a budget of $25,000?"

print(f"Prompt: {prompt}\n")

try:
with mcp_client:
tools = mcp_client.list_tools_sync()
print(f"Discovered tools: {[t.tool_name for t in tools]}\n")
agent = Agent(model=model, tools=tools, system_prompt=system_prompt)
response = agent(prompt)
print("\n--- Agent Response ---")
print(response.message["content"][0]["text"])
except Exception as e:
print(f"\nError: {e}")
finally:
print("\nDone.")
sys.stdout.flush()
os._exit(0)

Run the script:

python kb_gateway_agent.py

The agent will connect to the Gateway, discover the Knowledge Base retrieval tools, search your documents, and generate a grounded response with information from the uploaded files.

Step 5: Connect the Gateway to the Harness

Now we attach the Knowledge Base Gateway to our car dealership Harness so the agent can search our documents and answer customer questions grounded in real data.

5.1 Edit the Harness

5.1.1. Navigate to Amazon Bedrock AgentCore > Harness

5.1.2. Click on your Harness name (car_agent) to open its details page

5.1.3. Click the Edit button

5.1.4. In the Tools section, click Add Gateway and select KnowledgeBaseGateway

5.1.5. Under Outbound Auth configurations, keep IAM role (default) selected. This allows the Harness to authenticate with the Gateway using its execution role.

5.1.6. Click Save Harness and wait for the status to return to Ready

After saving, the console may display a message suggesting that the Harness execution role needs to be updated with bedrock-agentcore:InvokeGateway permission on the Gateway ARN. If this message appears, follow the instructions provided by the console to update the role.

Press enter or click to view image in full size
Edit Harness page showing the Tools section with the KnowledgeBaseGateway added and the Outbound Auth configurations set to IAM role
Adding the Knowledge Base Gateway as a tool to the car_agent Harness, enabling the agent to search the knowledge base during conversations

5.2 Test with the Harness

5.2.1. Once the Harness is Ready, click the Test Harness button

5.2.2. Enter a prompt that should trigger a knowledge base search:

A customer is asking: I have a family of 4 and a budget of $25,000. Should I buy an SUV or a sedan? What are the pros and cons of each?

5.2.3. Observe the response. The agent uses the Knowledge Base to retrieve relevant information and generates a grounded answer with specific details from your documents (vehicle models, price ranges, pros and cons). Under the Agent trace section, you will see a clickable blue link labeled CarDealershipKB Retrieve, indicating that the agent invoked the Knowledge Base tool.

Press enter or click to view image in full size
Harness playground showing the user prompt about SUVs vs sedans, the Agent trace section with 1 step, the CarDealershipKB Retrieve link highlighted with an orange border, the agent thinking blocks, and the generated response with pros, cons, and budget-friendly models
The agent autonomously decided to search the Knowledge Base, retrieved relevant information, and composed a grounded response with specific vehicle models and price ranges from the documents

5.2.4. Click on the CarDealershipKB Retrieve link to open the tool invocation details panel. This panel has two tabs: Input and Output.

5.2.5. The Input tab shows the query that the agent sent to the Knowledge Base. Notice that the agent formulated its own retrieval query based on the user’s question. In this case, it sent a query like "pros and cons of SUVs and seda..." to the retrievalQuery.text field. This demonstrates that the agent autonomously decides what to search for based on the conversation context.

Press enter or click to view image in full size
CarDealershipKB Retrieve panel showing the Input tab with a JSON object containing retrievalQuery with text field set to pros and cons of SUVs and sedans
The Input tab reveals the query the agent formulated autonomously to search the Knowledge Base, demonstrating that the agent decides what to search based on the conversation context

5.2.6. The Output tab shows the raw retrieval results returned by the Knowledge Base. This is the data the agent used to compose its response. The structure includes:

  • retrievalResults — An array of chunks retrieved from your documents, ordered by relevance score.
  • content.text — The actual text chunk extracted from the document.
  • documentId — The S3 URI identifying the source document.
  • location — The S3 location details (bucket and key).
  • metadata — Rich metadata including _document_title (file name), _source_uri (full S3 URL), _chunk_id, _file_type, _language_code, _created_at, _last_updated_at, and _data_source_id.
  • score — The relevance score for the chunk (e.g., 0.7226). Higher scores indicate stronger semantic match to the query.

This output shows exactly which passages from your documents the agent retrieved and how relevant each one was. The agent then uses these chunks to compose an informed, grounded response rather than hallucinating information.

Press enter or click to view image in full size
CarDealershipKB Retrieve panel showing the Output tab with a JSON object containing retrievalResults array with chunks including content text, documentId, s3Location, and metadata fields showing document title, source URI, chunk ID, file type, language code, timestamps, data source ID, and relevance score
The Output tab shows the raw retrieval results including text chunks from the documents, S3 source locations, rich metadata with document titles and timestamps, and relevance scores that indicate how well each chunk matches the query

Step 6: Clean Up

If you do not plan to continue using the resources created in this tutorial, remove them to avoid unnecessary charges. For pricing details, check the AgentCore pricing page.

To remove the resources:

  • Remove the Gateway from the Harness (edit the Harness and remove the Gateway tool)
  • Navigate to Amazon Bedrock AgentCore > Gateways, select KnowledgeBaseGateway, and delete it
  • Navigate to Amazon Bedrock AgentCore > Built-in tools > Knowledge Base, select car_kb, and delete it
  • Navigate to Amazon S3, select the bucket you created, empty it, and delete it
  • Navigate to IAM > Roles, delete the AgentCoreGatewayKnowledgeBase role

Conclusion

In this article, you learned how to use Amazon Bedrock Managed Knowledge Bases to ground your agent’s responses in private data:

  • Three KB types serve different needs: Managed KB for fully managed RAG with zero infrastructure, Unstructured Vector Store KB for custom vector database control, and Structured Data Store KB for SQL-based retrieval from relational data.
  • Managed KB handles the entire pipeline: you provide documents, and AWS manages ingestion, embedding, storage, and retrieval. The built-in embedding model and reranker are included at no extra cost.
  • The integration path for the Harness is always through AgentCore Gateway. You create a Gateway with the bedrock-knowledge-bases connector, which exposes Retrieve and AgenticRetrieveStream as MCP tools that the agent discovers and calls automatically.
  • Grounded responses include source citations, allowing users to verify the information the agent provides. This builds trust and accountability in production deployments.

The key takeaways are:

  • Knowledge Bases are the recommended way to give agents access to your private documents. The agent decides when to search based on the conversation context.
  • The Managed KB option eliminates all infrastructure management. No vector database to provision, no embedding model to select, no reranking to configure.
  • The Gateway pattern is consistent across all tool types in AgentCore. Whether it is Web Search, Lambda functions, or Knowledge Bases, the integration follows the same flow: create a Gateway target, attach to the Harness.
  • For production deployments, combine Knowledge Bases with other AgentCore tools (Memory for personalization, Code Interpreter for calculations, Web Search for current information) to build comprehensive agents.

References

Stay Connected

If you found this article helpful and want to learn more about AWS, cloud architecture, AI, infrastructure as code, and cloud security, follow the author for future content and tutorials:

Stay tuned for more practical walkthroughs and solutions for cloud infrastructure and DevOps challenges.

Happy building on AWS! 🚀☁️

--

--

Carlos Biagolini
Carlos Biagolini

Written by Carlos Biagolini

Cloud Architect | Writing about AWS, AI/ML, and hands-on cloud solutions | AWS Community Builder

AWS Tip
AWS Tip

Published in AWS Tip

Best AWS, DevOps, Serverless, and more from top Medium writers .