Usage

DynamoDB for Agent

Store agent sessions and history in DynamoDB with DynamoDb.

DynamoDb stores an Agent's sessions and run history in DynamoDB.

Usage

Start in a virtual environment and set the model key before running the example.

Set OpenAI Key

Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.

export OPENAI_API_KEY=sk-***

Install dependencies:

uv pip install agno boto3 openai

Pass a configured DynamoDB client as db_client. Otherwise, provide region_name, aws_access_key_id, and aws_secret_access_key directly or through the AWS_REGION, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY environment variables.

On first use, DynamoDb creates missing tables with provisioned capacity. The client or credentials must allow table creation and read/write operations.

dynamo_for_agent.py
from os import getenv

from agno.agent import Agent
from agno.db.dynamo import DynamoDb

AWS_ACCESS_KEY_ID = getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = getenv("AWS_SECRET_ACCESS_KEY")

db = DynamoDb(
    region_name="us-east-1",
    aws_access_key_id=AWS_ACCESS_KEY_ID,
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)

agent = Agent(db=db, add_history_to_context=True)

agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")

Parameters

ParameterTypeDefaultDescription
idOptional[str]-Database ID. Derived deterministically from explicit client or constructor region/key values when omitted.
db_clientOptional[Any]-The DynamoDB client to use.
region_nameOptional[str]-AWS region name.
aws_access_key_idOptional[str]-AWS access key ID.
aws_secret_access_keyOptional[str]-AWS secret access key.
session_tableOptional[str]-The name of the session table.
runs_tableOptional[str]NoneStorage name for individual runs. Defaults to agno_runs, or <session_table>_runs when a custom session name is supplied.
memory_tableOptional[str]-The name of the memory table.
metrics_tableOptional[str]-The name of the metrics table.
eval_tableOptional[str]-The name of the eval table.
knowledge_tableOptional[str]-The name of the knowledge table.
traces_tableOptional[str]-The name of the traces table.
spans_tableOptional[str]-The name of the spans table.

Run the Example

Save the code as dynamo_for_agent.py, complete the prerequisites above, then run:

python dynamo_for_agent.py