Introduction

Welcome back to Putting Bedrock Models to Action with Strands Agents! In our previous lessons, you've successfully built intelligent agents and enhanced them with calculation and custom tools. Now, in this third unit, we're taking a significant leap forward by connecting your agents to Amazon Bedrock Knowledge Bases, unlocking the power of enterprise-scale information retrieval and context-aware responses.

This lesson transforms your AWS Technical Assistant from a tool-equipped agent into a comprehensive knowledge system capable of searching through vast document repositories and providing detailed, citation-backed responses. We'll explore the retrieve tool from strands_tools, master advanced retrieval parameters, and implement sophisticated search configurations that allow precise targeting of specific information within your knowledge bases.

By the end of this unit, your agent will seamlessly combine its existing tool capabilities with powerful document retrieval, enabling it to answer complex questions about your organization's documentation, policies, and technical specifications. This integration represents the foundation of modern Retrieval-Augmented Generation (RAG) systems, where AI reasoning meets curated knowledge repositories.

Understanding Knowledge Base Integration

Knowledge Base integration revolutionizes agent capabilities by providing access to curated document collections that extend far beyond the training data of foundation models. When agents connect to knowledge bases, they gain the ability to retrieve relevant information from your organization's documentation, technical manuals, policy documents, and other structured knowledge sources in real time during conversations.

The retrieve tool operates through semantic similarity search, where your queries are converted into vector representations and matched against pre-indexed document embeddings stored in your knowledge base. This approach enables sophisticated information discovery that goes beyond simple keyword matching, finding conceptually relevant content even when exact terms don't appear in the source documents.

Note that we will be reusing the same Bedrock Knowledge Base we developed in the previous course.

Setting Up Your Enhanced Agent

Let's begin by configuring our agent with both familiar tools and the new knowledge base retrieval capability, building upon the foundational setup patterns you've mastered in previous lessons.

import os
from strands import Agent
from strands.models import BedrockModel
from strands_tools import calculator, retrieve

# Define the Bedrock model ID
model_id = "us.anthropic.claude-sonnet-4-20250514-v1:0"

# Read guardrail info from environment variables
guardrail_id = os.getenv("GUARDRAIL_ID")
guardrail_version = os.getenv("GUARDRAIL_VERSION", "DRAFT")

# Read knowledge base and region from environment variables
KNOWLEDGE_BASE_ID = os.getenv("KNOWLEDGE_BASE_ID")
REGION = os.getenv("AWS_REGION")

As you recall from previous units, we're maintaining the same model configuration and guardrail setup patterns that ensure secure, compliant AI operations. The new additions here include the retrieve import from strands_tools and the environment variables for KNOWLEDGE_BASE_ID and REGION, which specify which knowledge base your agent should connect to and in which AWS region it's deployed.

Creating Your Knowledge-Enhanced Agent

With our configuration variables established, we can now create an agent that combines mathematical capabilities with powerful document retrieval functionality.

# Create a Bedrock model with guardrail configuration
model = BedrockModel(
    model_id=model_id,
    guardrail_id=guardrail_id,
    guardrail_version=guardrail_version
)

# Define the system prompt for AWS Technical Assistant
system_prompt = "You are an AWS Technical Assistant. Provide clear, accurate information about AWS services."

# Create an agent with both calculator and retrieve tools
agent = Agent(
    model=model,
    system_prompt=system_prompt,
    tools=[calculator, retrieve]
)

This agent configuration now includes the retrieve tool alongside the familiar calculator, creating a hybrid system capable of both computational tasks and knowledge base searches. The agent automatically gains awareness of the retrieval capabilities and will intelligently decide when to search your knowledge base versus when to perform calculations, depending on the context of user queries.

The seamless integration of multiple tools demonstrates the power of the Strands Agents framework, where each tool becomes part of the agent's reasoning toolkit without requiring complex orchestration code on your part.

Performing Basic Knowledge Base Retrieval

Now let's explore the fundamental retrieval operation using your knowledge base to search for information about a specific topic.

# Basic search for AWS documentation with default knowledge base
results = agent.tool.retrieve(text="Nimbus Assist")
print(f"[{results['toolUseId']}] Status: {results['status']}, Response:")
print(results['content'][0]['text'])

This basic retrieval call searches for documents related to "Nimbus Assist" using default parameters. The agent.tool.retrieve() method provides direct access to the retrieval functionality, allowing you to perform targeted searches and examine the raw results before they're processed into conversational responses.

[tooluse_retrieve_461902154] Status: success, Response:
Retrieved 10 results with score >= 0.4:

Score: 0.7522
Document ID: Unknown
Content: # File: docs/brd-nimbus-assist.md
---
title: Business Requirements — Nimbus Assist (AI Support Agent)
owner: cx-platform@techco
status: draft
last-updated: 2025-07-14
tags: [BRD, Support, Bedrock, RAG]
---

## Summary
Nimbus Assist reduces human ticket volume by answering customer questions using curated internal knowledge. It must deliver fast, accurate, citeable answers and hand off seamlessly to humans.

## Problems
- 32% of tickets are "known answers" buried in wikis/Runbooks.
- Agents spend 18–25 minutes searching per ticket.

## Users & Use Cases
- **Customers**: self-service answers in-app and on the help center.
- **Agents**: suggested replies inside Zendesk.
- **Compliance**: exported citations for audits.

[...]

The output reveals the successful retrieval of relevant documentation, where the first document has a similarity score of 0.7522, indicating high relevance to the query. The default configuration returned 10 results with scores above 0.4, providing comprehensive coverage of available information while maintaining reasonable quality thresholds. Note that the full output is truncated.

Advanced Retrieval Configuration

Let's enhance our retrieval capabilities by specifying custom parameters that provide more precise control over search behavior and result quality.

# Advanced search with custom parameters for AWS services
results = agent.tool.retrieve(
    text="Nimbus Assist",
    numberOfResults=3,
    score=0.7,
    knowledgeBaseId=KNOWLEDGE_BASE_ID,
    region=REGION,
)
print(f"[{results['toolUseId']}] Status: {results['status']}, Response:")
print(results['content'][0]['text'])

This advanced configuration demonstrates precise control over retrieval behavior: numberOfResults=3 limits the response to the top 3 most relevant documents, while score=0.7 establishes a higher relevance threshold that filters out lower-quality matches. The explicit knowledgeBaseId and region parameters ensure your queries target the correct knowledge base deployment for optimal performance and accuracy.

[tooluse_retrieve_380146500] Status: success, Response:
Retrieved 2 results with score >= 0.7:

Score: 0.7522
Document ID: Unknown
Content: # File: docs/brd-nimbus-assist.md
---
title: Business Requirements — Nimbus Assist (AI Support Agent)
owner: cx-platform@techco
status: draft
last-updated: 2025-07-14
tags: [BRD, Support, Bedrock, RAG]
---

## Summary
Nimbus Assist reduces human ticket volume by answering customer questions using curated internal knowledge. It must deliver fast, accurate, citeable answers and hand off seamlessly to humans.

## Problems
- 32% of tickets are "known answers" buried in wikis/Runbooks.
- Agents spend 18–25 minutes searching per ticket.

## Users & Use Cases
- **Customers**: self-service answers in-app and on the help center.
- **Agents**: suggested replies inside Zendesk.
- **Compliance**: exported citations for audits.

[...]

Notice that with the higher score threshold, only 2 results met the 0.7 relevance criteria, demonstrating how parameter tuning directly impacts result quality and quantity. This precision control allows you to balance comprehensive coverage and high-confidence results based on your specific application requirements.

Full Agent Integration with Conversational Responses

Finally, let's witness the complete integration by allowing your agent to autonomously use its retrieval capabilities within a natural conversation flow.

print('Agent response with retrieve tool:')
# Send a message to the agent
agent("What is Nimbus Assist?")

This conversational query triggers the agent's autonomous decision-making process, where it determines that knowledge base retrieval is necessary to provide an accurate and comprehensive response about Nimbus Assist. The agent seamlessly orchestrates the retrieval operation behind the scenes while maintaining natural conversation flow.

Agent response with retrieve tool:
Based on the retrieved documentation, **Nimbus Assist** is an AI-powered support agent designed to reduce human ticket volume by automatically answering customer questions using curated internal knowledge sources.

## Key Details:

**Purpose:**
- Reduces human support ticket volume by providing fast, accurate, and citeable answers
- Addresses the problem that 32% of support tickets are "known answers" buried in wikis and runbooks
- Eliminates the 18-25 minutes agents currently spend searching for answers per ticket

**Target Users:**
- **Customers**: Self-service answers through in-app widgets and help center
- **Support Agents**: Suggested replies integrated into Zendesk
- **Compliance Teams**: Exportable citations for audit purposes

**Technical Architecture:**
- Built using **Amazon Bedrock Knowledge Bases** with Retrieval-Augmented Generation (RAG)
- Uses **OpenSearch Serverless** for vector indexing
- Sources include S3 documents, product manuals, and runbooks (Markdown/PDF/HTML formats)
- Powered by configurable Bedrock-hosted language models
- Includes guardrails for PII protection and content safety

**Key Features:**
- Provides answers with 1-3 citations for verification
- Multilingual support (English primary, Spanish/German fallback)
- Admin console for source curation and evaluation dashboards
- Multi-tenant SaaS architecture with data isolation

**Success Targets:**
- ≥35% deflection rate on Tier-0/Tier-1 support topics
- ≥4.3/5 answer helpfulness rating
- ≤2% unresolved issues due to hallucinations
- P50 latency ≤1200ms, P95 ≤2500ms

The system is currently in development with plans for a beta release on the internal help center, followed by general availability across all help center channels and agent assist for Tier-1 support queues.
Conclusion and Next Steps

You've successfully transformed your agent into a sophisticated knowledge-retrieval system capable of accessing and reasoning over enterprise documentation repositories. The integration of the retrieve tool with advanced parameter configuration provides the foundation for building production-ready RAG applications that can serve as intelligent knowledge assistants, technical documentation systems, and customer support automation platforms.

This mastery of knowledge base integration represents a crucial milestone in your journey toward building enterprise-scale AI systems that combine conversational intelligence with organizational knowledge. The upcoming practice exercises will challenge you to further explore retrieval optimization and discover the full potential of knowledge-enhanced agent systems.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal