Bedrock Agents are a great to accomplish use-cases requiring interactions with documents based knowledge base. But what if there are too many documents from different domains and we want to restrict agent(s) to use limited documents while fetching from knowledge base. To configure an agent in Amazon Bedrock to filter documents by metadata (e.g., agent_id
), below are the two main options I could find as of now :
1. Using the AWS Management Console (GUI)
The Bedrock console allows you to set metadata filters when testing a knowledge base, but not directly in the agent creation wizard.
However, you can instruct the agent to always use a filter by:
A. During Testing (Manual Filter)
- Go to Amazon Bedrock Console.
- Select Knowledge bases.
- Choose your knowledge base and click Test knowledge base.
- Click the Configurations (gear) icon.
- Expand Filters.
- Add a filter:
- Key:
agent_id
- Operator:
=
- Value: (e.g.,
agent_1
)
- Key:
- Enter your query and run the test. Only documents with that
agent_id
will be retrieved.
This is for testing and validation.
B. Instructing the Agent (Production Use)
Currently, the Bedrock console does not provide a direct field in the agent builder to set a persistent metadata filter for all agent queries.
Workarounds:
-
Add clear instructions in the agent’s prompt (e.g., “Only answer using documents where
agent_id
isagent_1
.”). This is not a hard technical filter, but can help guide the model. - Enforce filtering in your application code (if you use the SDK or API).
2. Using the SDK or API (Recommended for Production)
If you want to enforce metadata filtering programmatically (recommended for production), you can set the filter in your retrieval API call.
Example (Python, boto3):
import boto3
bedrock_agent_runtime = boto3.client("bedrock-agent-runtime")
response = bedrock_agent_runtime.retrieve_and_generate(
knowledgeBaseId="your-kb-id",
retrievalConfiguration={
"vectorSearchConfiguration": {
"filter": {
"equals": {
"key": "agent_id",
"value": "agent_1"
}
}
}
},
input={"text": "Your query here"}
)
Source: AWS Blog on Metadata Filtering
Summary Table
Method | How to Set Filter | Where/How |
---|---|---|
Console (Test Only) | Add filter in Test KB UI | Bedrock Console > Knowledge Base > Test |
Console (Agent) | Add in agent instructions | Bedrock Console > Agent > Instructions |
SDK/API (Production) | Set filter in API call | Application code using Bedrock SDK |
References
- Amazon Bedrock Knowledge Bases now supports metadata filtering (AWS Blog)
- Metadata filtering for tabular data with Amazon Bedrock Knowledge Bases (AWS Blog)
In summary:
- For testing: Use the filter UI in the Bedrock console.
- For production: Use the SDK/API to enforce metadata filtering per agent.
- Direct persistent filter in agent config via GUI is not yet available, so use application logic or API for strict enforcement.
Top comments (0)