Amazon Bedrock AgentCore Python SDK
The Amazon Bedrock AgentCore Python SDK provides a higher-level abstraction for developing and deploying AI agents on Amazon Bedrock AgentCore. This SDK simplifies operations related to agent runtime, memory, authentication, and tools, offering framework-agnostic primitives. It is actively maintained with frequent releases, currently at version 1.6.0.
Warnings
- breaking Version 1.5.1 reverted the feature to emit OTEL (OpenTelemetry) attributes for AgentCore Evaluation support. If your application relies on these specific OTEL attributes for observability, this change will break that integration.
- gotcha AccessDeniedException is a common issue due to insufficient IAM permissions for the caller or the agent's execution role. This can occur during agent creation or runtime invocation.
- gotcha Using outdated `boto3` or `botocore` libraries can lead to `Unknown service: 'bedrock-agent-core-runtime'` errors when attempting to interact with Bedrock AgentCore APIs.
- gotcha The AWS region resolution order can be complex. If not explicitly passed as a parameter to SDK clients, the region is determined by `boto3_session`, `AWS_REGION` environment variable, `AWS_DEFAULT_REGION`, or falls back to `us-west-2`. Inconsistent region configuration can lead to resources not found or `AccessDeniedException` if the agent attempts to operate in an unintended region.
Install
-
pip install bedrock-agentcore
Imports
- BedrockAgentCoreApp
from bedrock_agentcore.agent import BedrockAgentCoreApp
- entrypoint
from bedrock_agentcore.agent import entrypoint
- MemorySessionManager
from bedrock_agentcore.memory import MemorySessionManager
Quickstart
import os
from bedrock_agentcore.agent import BedrockAgentCoreApp, entrypoint
# Initialize the AgentCore application
app = BedrockAgentCoreApp(
region_name=os.environ.get('AWS_REGION', 'us-east-1')
)
# Define your agent's entrypoint
@app.entrypoint()
def handler(event):
"""Handles incoming agent invocations."""
prompt = event.prompt
# In a real agent, you would process the prompt, use tools, or interact with memory.
response_content = f"Hello from your AgentCore agent! You said: {prompt}"
# The response must be a dictionary with a 'response' key for simple text responses
return {"response": response_content}
# To run the agent locally (for development and testing)
# Make sure to set AWS_REGION and have AWS credentials configured.
# The `app.run()` method starts a local HTTP server on port 8080.
# You can test it with `curl -X POST http://localhost:8080/invocations -H "Content-Type: application/json" -d '{"prompt": "What's up?"}'`
if __name__ == "__main__":
print("Starting Bedrock AgentCore agent locally. Access at http://localhost:8080/invocations")
print("Ensure AWS_REGION environment variable is set and AWS credentials are configured.")
app.run()