Build agentic retrieval on a Managed Amazon Bedrock Knowledge Base with CDK
Learn how to build a fully observable, agentic retrieval system on Amazon Bedrock Managed Knowledge Bases using AWS CDK in Python.

Building Agentic Retrieval on a Managed Amazon Bedrock Knowledge Base with CDK
Introduction
Enterpriseāgrade RetrievalāAugmented Generation (RAG) often falls short when answers require multiple sources, dynamic source selection, or iterative refinement. By placing a reasoning agent in the loop, Amazon Bedrockās Managed Knowledge Base (KB) and AgentCore services enable agentic retrieval: multiāturn planning, toolādriven fetching, and grounded, cited answers. This guide walks through a singleācommand AWS CDK (Python) deployment that provisions two managed KBs, an AgentCore gateway, CloudWatch observability dashboards, and evaluation pipelines.
---
1. Why Agentic Retrieval Matters
| Traditional RAG | Agentic Retrieval | |-----------------|-------------------| | One embedāandāfetch step | Agent decides if and what to fetch | | Fixed index, no source routing | Semantic routing across multiple KBs | | No feedback loop | Iterative retrieval until answer is complete | | Limited traceability | Full telemetry for each reasoning step |
When a question spans multiple corpora or requires a second look after an initial result, a single retrieval can produce incomplete or misleading answers. An agent can:
- Select the appropriate KB based on the query.
- Retrieve relevant chunks, optionally reāranking them.
- Iterate if the partial answer is insufficient.
- Synthesize a final response with citations.
2. Managed Knowledge Base vs. DIY Vector Store
| Feature | Managed KB (Bedrock) | DIY (OpenSearch, etc.) | |---------|----------------------|------------------------| | Ingestion & indexing | Fully managed, autoāscaled | Userāprovisioned, capacity planning required | | Embedding & reranking | Builtāin models, no extra cost | Must configure and pay for separate models | | AgenticRetrieveStream API | Available outāofātheābox | Not supported | | Gateway integration | Native AgentCore connector | Custom integration needed |
The Managed KB removes the operational burden of a vector database, making it the natural foundation for agentic retrieval. It also unlocks the AgentCore Gateway, which exposes each KB as a Model Context Protocol (MCP) tool for the reasoning agent.
3. CDK Architecture Overview
``` +-------------------+ +-------------------+ +-------------------+ | Amazon S3 Bucket | ---> | Managed KB #1 | ---> | AgentCore Runtime | +-------------------+ +-------------------+ +-------------------+ ^ | | v +-------------------+ +-------------------+ +-------------------+ | Amazon S3 Bucket | ---> | Managed KB #2 | ---> | AgenticRetrieve | +-------------------+ +-------------------+ +-------------------+ ```
- Data sources ā Two S3 buckets hold separate corpora (e.g., product docs vs. policy manuals). CDK creates ingestion jobs that feed each Managed KB.
- AgentCore ā Deploys a Gateway that registers each KB as an MCP tool and a Runtime that hosts the reasoning agent.
- AgenticRetrieveStream ā The API call the agent uses to perform multiāturn retrieval and receive a cited answer.
- Observability ā Sevenālayer CloudWatch dashboards capture: request latency, token usage, KB hit/miss ratios, agent decision paths, reranking scores, and evaluation metrics.
- Evaluation ā Two Lambdaābacked jobs provide onādemand testāset scoring and continuous drift monitoring, publishing results to a dedicated dashboard.
4. StepābyāStep Deployment
- Clone the starter repo (contains `cdk.json`, `requirements.txt`, and `app.py`).
- Configure environment variables ā set `AWSACCOUNT`, `AWSREGION`, and optional `KBNAMEPREFIX`.
- Run `cdk bootstrap` to prepare the account for asset deployment.
- Deploy with a single command:
```bash cdk deploy --all ``` CDK resolves dependencies, creates the two Managed KBs, provisions the AgentCore Gateway, sets up CloudWatch dashboards, and wires the Lambda evaluation functions.
- Test the endpoint:
```python import boto3, json client = boto3.client('bedrock-agent-runtime') resp = client.invokeagent( AgentId='my-agent', Input='How does our refund policy differ for premium customers?' ) print(json.dumps(resp, indent=2)) ``` The response includes `answer`, `citations`, and a `traceid` you can look up in the dashboards.
5. Observability & Evaluation Built In
- Telemetry layers ā request metadata, agent decision tree, KB query logs, reranking scores, model inference metrics, latency breakdown, and final answer quality.
- Dashboards ā preāpopulated widgets show perāKB hit rates, average turns per query, and error rates.
- Evaluation Lambda ā pulls a curated test set from S3, runs `AgenticRetrieveStream` against each query, compares generated citations to ground truth, and writes scores to a CloudWatch metric.
- Continuous monitoring ā a scheduled rule triggers the evaluation job nightly, alerting via SNS if drift exceeds a configurable threshold.
---
Closing Thoughts
By leveraging Amazon Bedrockās Managed Knowledge Base and AgentCore, you can move from a brittle singleāshot RAG pipeline to a robust, multiāturn agentic retrieval system without managing vector stores or custom orchestration layers. The CDKādriven approach ensures reproducible infrastructure, automatic ordering of resources, and outāofātheābox observability that turns a blackābox AI service into an auditable, productionāready component. Deploy the stack, monitor the dashboards, and iterate on your corporaāyour enterprise agents will answer complex questions with confidence and traceability.
Source
Read original sourceWhy we picked this
Core AI content describing a new agentic retrieval system using Amazon Bedrock and CDK, relevant to developers building AI infrastructure.