Base Agent¶
The BedrockAgent class serves as the foundation for all agents in the Bedrock Swarm framework. It provides core functionality for model interaction, tool management, and memory handling.
Class Documentation¶
Base class for Bedrock-powered agents.
Each agent has: 1. A specific role/domain of expertise 2. A set of tools it can use 3. A memory system for context 4. A Bedrock model for processing
| PARAMETER | DESCRIPTION |
|---|---|
model_id
|
Bedrock model ID
TYPE:
|
name
|
Name of the agent
TYPE:
|
role
|
Description of agent's role/expertise
TYPE:
|
tools
|
Optional list of tools available to the agent
TYPE:
|
memory
|
Optional memory system (defaults to SimpleMemory)
TYPE:
|
system_prompt
|
Optional system prompt
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
InvalidModelError
|
If model ID is not supported |
Source code in src/bedrock_swarm/agents/base.py
Attributes¶
last_token_count: int
property
¶
Get the token count from the last request.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of tokens used in last request
TYPE:
|
Functions¶
_initialize_model() -> BedrockModel
¶
_build_prompt(message: str) -> str
¶
Build the complete prompt including context and conversation history.
This method builds a comprehensive prompt that includes: 1. System prompt and role context 2. Available tools and their schemas 3. Recent conversation history 4. Response format instructions 5. Current message
Source code in src/bedrock_swarm/agents/base.py
generate(message: str) -> AgentResponse
¶
Generate a response to a message.
This method: 1. Records the incoming message in memory 2. Generates a response using the model 3. Records the response and any tool calls in memory 4. Returns the processed response
| PARAMETER | DESCRIPTION |
|---|---|
message
|
Message to respond to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AgentResponse
|
Response containing either tool calls or direct message |
Source code in src/bedrock_swarm/agents/base.py
_format_prompt(message: str, history: List[Message]) -> str
¶
Format the prompt with message history.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
Current message
TYPE:
|
history
|
Message history
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted prompt string |
Source code in src/bedrock_swarm/agents/base.py
Core Features¶
The base agent provides:
- Model Integration
- Model initialization
- Request formatting
- Response processing
-
Token management
-
Tool Management
- Tool registration
- Tool validation
- Tool execution
-
Error handling
-
Memory System
- Context management
- Message history
- State persistence
- Memory cleanup
Usage Examples¶
from bedrock_swarm.agents import BedrockAgent
from bedrock_swarm.tools import CalculatorTool
# Create agent
agent = BedrockAgent(
name="calculator",
model_id="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
tools=[CalculatorTool()],
system_prompt="You are a calculation specialist."
)
# Process message
response = agent.generate("Calculate 15 * 7")
print(response)
# Check memory
history = agent.memory.get_messages()
print(history)
# Get token usage
print(agent.last_token_count)
Tool Integration¶
Agents can use multiple tools:
from bedrock_swarm.tools import CalculatorTool, TimeTool
# Create agent with multiple tools
agent = BedrockAgent(
name="utility_agent",
model_id="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
tools=[
CalculatorTool(),
TimeTool()
]
)
# Tools are available in prompts
response = agent.generate(
"What will be 2 * 5 in 3 hours?"
)
Memory Management¶
The agent includes memory management:
# Access memory system
memory = agent.memory
# Store message
memory.add_message(
role="user",
content="Hello!",
metadata={"timestamp": "2024-02-29"}
)
# Get recent messages
recent = memory.get_recent(n=5)
# Clear memory
memory.clear()
Error Handling¶
The agent handles various errors:
-
Model Errors
-
Tool Errors
-
Memory Errors
Implementation Details¶
The agent implementation includes:
- Request Processing
- Message formatting
- Tool integration
- Memory context
-
Response handling
-
Tool Management
- Tool registration
- Schema validation
- Execution handling
-
Result processing
-
Memory Integration
- Message storage
- Context retrieval
- State management
- Cleanup handling
Agent Configuration¶
Agents can be configured with:
agent_config = {
"name": "specialized_agent",
"model_id": "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
"system_prompt": "You are a specialized agent...",
"tools": [CalculatorTool()],
"memory": CustomMemory(),
"model_config": {
"temperature": 0.7,
"max_tokens": 1000
}
}
agent = BedrockAgent(**agent_config)
Best Practices¶
-
Tool Organization
-
Memory Usage
-
Error Recovery