AmazonBedrockAgent
The AmazonBedrockAgent is a specialized agent class in the Agent Squad that integrates directly with Amazon Bedrock agents.
Creating an AmazonBedrockAgent
Section titled “Creating an AmazonBedrockAgent”Here are various examples showing different ways to create and configure an AmazonBedrockAgent:
Python Package
Section titled “Python Package”If you haven’t already installed the AWS-related dependencies, make sure to install them:
pip install "agent-squad[aws]"Basic Examples
Section titled “Basic Examples”1. Minimal Configuration
const agent = new AmazonBedrockAgent({ name: 'My Bank Agent', description: 'A helpful and friendly agent that answers questions about loan-related inquiries', agentId: 'your-agent-id', agentAliasId: 'your-agent-alias-id'});agent = AmazonBedrockAgent(AmazonBedrockAgentOptions( name='My Bank Agent', description='A helpful and friendly agent that answers questions about loan-related inquiries', agent_id='your-agent-id', agent_alias_id='your-agent-alias-id'))2. Using Custom Client
import { BedrockAgentRuntimeClient } from "@aws-sdk/client-bedrock-agent-runtime";const customClient = new BedrockAgentRuntimeClient({ region: 'us-east-1' });const agent = new AmazonBedrockAgent({ name: 'My Bank Agent', description: 'A helpful and friendly agent for banking inquiries', agentId: 'your-agent-id', agentAliasId: 'your-agent-alias-id', client: customClient});import boto3custom_client = boto3.client('bedrock-agent-runtime', region_name='us-east-1')agent = AmazonBedrockAgent(AmazonBedrockAgentOptions(name='My Bank Agent',description='A helpful and friendly agent for banking inquiries',agent_id='your-agent-id',agent_alias_id='your-agent-alias-id',client=custom_client))3. With Tracing Enabled
const agent = new AmazonBedrockAgent({ name: 'My Bank Agent', description: 'A banking agent with tracing enabled', agentId: 'your-agent-id', agentAliasId: 'your-agent-alias-id', enableTrace: true});agent = AmazonBedrockAgent(AmazonBedrockAgentOptions( name='My Bank Agent', description='A banking agent with tracing enabled', agent_id='your-agent-id', agent_alias_id='your-agent-alias-id', enable_trace=True))4. With Streaming Enabled
const agent = new AmazonBedrockAgent({ name: 'My Bank Agent', description: 'A streaming-enabled banking agent', agentId: 'your-agent-id', agentAliasId: 'your-agent-alias-id', streaming: true});agent = AmazonBedrockAgent(AmazonBedrockAgentOptions( name='My Bank Agent', description='A streaming-enabled banking agent', agent_id='your-agent-id', agent_alias_id='your-agent-alias-id', streaming=True))5. Complete Example with All Options
import { AmazonBedrockAgent } from "agent-squad";import { BedrockAgentRuntimeClient } from "@aws-sdk/client-bedrock-agent-runtime";const agent = new AmazonBedrockAgent({ // Required fields name: "Advanced Bank Agent", description: "A fully configured banking agent with all features enabled", agentId: "your-agent-id", agentAliasId: "your-agent-alias-id", // Optional fields region: "us-west-2", streaming: true, enableTrace: true, client: new BedrockAgentRuntimeClient({ region: "us-west-2" }),});import boto3from agent_squad.agents import AmazonBedrockAgent, AmazonBedrockAgentOptions
custom_client = boto3.client('bedrock-agent-runtime', region_name='us-west-2')
agent = AmazonBedrockAgent(AmazonBedrockAgentOptions( # Required fields name='Advanced Bank Agent', description='A fully configured banking agent with all features enabled', agent_id='your-agent-id', agent_alias_id='your-agent-alias-id',
# Optional fields region='us-west-2', streaming=True, enable_trace=True, client=custom_client))Option Explanations
Section titled “Option Explanations”name: (Required) Identifies the agent within your system.description: (Required) Describes the agent’s purpose or capabilities.agentId/agent_id: (Required) The ID of the Amazon Bedrock agent you want to use.agentAliasId/agent_alias_id: (Required) The alias ID of the Amazon Bedrock agent.region: (Optional) AWS region for the Bedrock service. If not provided, uses the default AWS region.client: (Optional) Custom BedrockAgentRuntimeClient for specialized configurations.enableTrace/enable_trace: (Optional) When set to true, enables tracing of the agent’s steps and reasoning process.streaming: (Optional) Enables streaming for the final response. Defaults to false.
Adding the Agent to the Orchestrator
Section titled “Adding the Agent to the Orchestrator”To integrate the AmazonBedrockAgent into your Agent Squad, follow these steps:
- First, ensure you have created an instance of the orchestrator:
import { AgentSquad } from "agent-squad";
const orchestrator = new AgentSquad();from agent_squad.orchestrator import AgentSquad
orchestrator = AgentSquad()- Then, add the agent to the orchestrator:
orchestrator.addAgent(agent);orchestrator.add_agent(agent)- Now you can use the orchestrator to route requests to the appropriate agent, including your Amazon Bedrock agent:
const response = await orchestrator.routeRequest( "What is the base rate interest for 30 years?", "user123", "session456");response = await orchestrator.route_request( "What is the base rate interest for 30 years?", "user123", "session456")Knowledge Base citations
Section titled “Knowledge Base citations”When your Bedrock agent is backed by a Knowledge Base, the Amazon Bedrock API returns source citations alongside the response text. AmazonBedrockAgent captures these citations and makes them available on the ConversationMessage returned by route_request.
The citations field is undefined / None when the agent has no Knowledge Base or when the response contains no attribution data, so existing code that does not use citations is completely unaffected.
Accessing citations
Section titled “Accessing citations”const response = await orchestrator.routeRequest( "What is the base rate interest for 30 years?", "user123", "session456");
// The message is on response.output for a non-streaming responseconst message = response.output as ConversationMessage;
if (message.citations && message.citations.length > 0) { for (const citation of message.citations) { const text = citation.generatedResponsePart?.textResponsePart?.text; for (const ref of citation.retrievedReferences ?? []) { const uri = ref.location?.s3Location?.uri ?? ref.location?.webLocation?.url; console.log(`"${text}" — source: ${uri}`); } }}response = await orchestrator.route_request( "What is the base rate interest for 30 years?", "user123", "session456")
# The message is on response.output for a non-streaming responsemessage = response.output
if message.citations: for citation in message.citations: text = ( citation.get("generatedResponsePart", {}) .get("textResponsePart", {}) .get("text") ) for ref in citation.get("retrievedReferences", []): location = ref.get("location", {}) uri = ( location.get("s3Location", {}).get("uri") or location.get("webLocation", {}).get("url") ) print(f'"{text}" — source: {uri}')Citation structure
Section titled “Citation structure”Each entry in citations mirrors the Citation object returned by the Amazon Bedrock InvokeAgent API:
| Field | Type | Description |
|---|---|---|
| generatedResponsePart.textResponsePart.text | string | The portion of the agent’s answer this citation covers |
| generatedResponsePart.textResponsePart.span | object | { start, end } character offsets within the full response text |
| retrievedReferences | array | One entry per Knowledge Base source chunk used for this part of the answer |
| retrievedReferences[].content.text | string | The raw text retrieved from the Knowledge Base |
| retrievedReferences[].location | object | Where the source lives — s3Location.uri, webLocation.url, etc. |
| retrievedReferences[].metadata | object | Any custom metadata attached to the source document |
By leveraging the AmazonBedrockAgent, you can easily integrate pre-built Amazon Bedrock agents into your Agent Squad.