Use DynamoDB as the database for an agent
Store agent sessions and runs in DynamoDB using AWS credentials from the environment.
"""Use DynamoDb as the database for an agent.
Set the following environment variables to connect to your DynamoDb instance:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_REGION
Run `uv pip install boto3` to install dependencies."""
from agno.agent import Agent
from agno.db import DynamoDb
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
db = DynamoDb()
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
db=db,
name="DynamoDB Agent",
description="An agent that uses DynamoDB as a database",
add_history_to_context=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# The Agent sessions and runs will now be stored in DynamoDB
agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno boto3 openaiExport environment variables
export AWS_ACCESS_KEY_ID="your_aws_access_key_id_here"
export AWS_REGION="your_aws_region_here"
export AWS_SECRET_ACCESS_KEY="your_aws_secret_access_key_here"
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the code above as dynamo_for_agent.py, then run:
python dynamo_for_agent.pyFull source: cookbook/06_storage/dynamodb/dynamo_for_agent.py