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 openaiPass 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.
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
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | Database ID. Derived deterministically from explicit client or constructor region/key values when omitted. |
db_client | Optional[Any] | - | The DynamoDB client to use. |
region_name | Optional[str] | - | AWS region name. |
aws_access_key_id | Optional[str] | - | AWS access key ID. |
aws_secret_access_key | Optional[str] | - | AWS secret access key. |
session_table | Optional[str] | - | The name of the session table. |
runs_table | Optional[str] | None | Storage name for individual runs. Defaults to agno_runs, or <session_table>_runs when a custom session name is supplied. |
memory_table | Optional[str] | - | The name of the memory table. |
metrics_table | Optional[str] | - | The name of the metrics table. |
eval_table | Optional[str] | - | The name of the eval table. |
knowledge_table | Optional[str] | - | The name of the knowledge table. |
traces_table | Optional[str] | - | The name of the traces table. |
spans_table | Optional[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