Amazon DynamoDB for AI Applications: Storing Conversations, Sessions and Results
Learn how to store conversations, sessions and results for an AI app in Amazon DynamoDB. Design a key schema, add automatic expiry with TTL, read and write from Python, feed history to Amazon Bedrock and keep access tightly limited.
A simple table design for chat history and AI results, with expiry built in, safe permissions and the pitfalls that catch people out.
A model on its own has no memory. Every request starts blank, which means your app has to remember the conversation, the session and the results. That's a database job, and Amazon DynamoDB is a natural fit. It's serverless, it scales without you managing servers, and it's very quick at the kind of lookup an AI app does constantly: "give me the last ten messages for this session."
In this article we'll design a small table for chat history, add automatic expiry, read and write it from Python, feed the history back to Amazon Bedrock, and lock down access.
What you need
- An AWS account and the AWS CLI configured
- Python with boto3
- About forty minutes
Check the current DynamoDB documentation and pricing page before you begin, since limits and prices change.
Why DynamoDB suits this job
AI apps tend to have a simple access pattern:
- Fetch the recent messages for one session, in order
- Add a new message
- Eventually delete old data
DynamoDB handles exactly that kind of key-based lookup with steady speed, and you only pay for what you use if you choose on-demand capacity. It's less suited to open-ended questions across all your data, such as "find every message that mentions refunds." For that kind of search you'd use another tool alongside it, such as Amazon OpenSearch Service or Athena.
Step 1: Design the table
DynamoDB tables are built around keys, so decide how you'll look data up before you create anything.
For chat history, a good design is:
- Partition key:
session_id. All the messages in one conversation share it.
- Sort key:
ts, a timestamp in milliseconds. It keeps messages in time order within a session.
Each message is one item with attributes like these:
Attribute | Example | Purpose |
|---|---|---|
session_id | s-8f2a | Partition key |
ts | 1790000000000 | Sort key (milliseconds) |
role | user or assistant | Who said it |
text | "Where is order 1001?" | The message |
user_id | u-42 | Who the session belongs to |
expires_at | 1792600000 | When DynamoDB may delete it |
With this layout, "get the latest messages for a session" is a single, fast query.
A few limits to design around:
- An item can be at most 400 KB. For very long outputs, documents or images, store the content in Amazon S3 and keep only a pointer in DynamoDB.
- A partition key with a huge number of items is fine here, since sessions are small. But avoid keys where a few values get all the traffic.
Step 2: Create the table
aws dynamodb create-table \ --table-name chat-sessions \ --attribute-definitions \ AttributeName=session_id,AttributeType=S \ AttributeName=ts,AttributeType=N \ --key-schema \ AttributeName=session_id,KeyType=HASH \ AttributeName=ts,KeyType=RANGE \ --billing-mode PAY_PER_REQUESTPAY_PER_REQUEST is on-demand capacity: no capacity planning, and you pay per read and write. It's a good default for a new app with unpredictable traffic. If your traffic becomes steady and high, compare it against provisioned capacity on the pricing page.DynamoDB encrypts data at rest by default.
Step 3: Add automatic expiry
Conversations often contain personal information, and you rarely need them forever. DynamoDB's Time to Live (TTL) feature can delete items for you after a time you choose.
aws dynamodb update-time-to-live \ --table-name chat-sessions \ --time-to-live-specification Enabled=true,AttributeName=expires_atTwo things to know about TTL:
- The attribute must hold a Unix timestamp in seconds, not milliseconds. Mixing these up is a common mistake, and an expiry set in milliseconds lands thousands of years in the future.
- Deletion isn't instant. Items are typically removed some time after they expire, not at the exact second. So if an expired item must never be shown, filter on
expires_atwhen you read, as well as relying on TTL.
Step 4: Turn on backups
Point-in-time recovery lets you restore the table to an earlier moment, which protects against accidental deletes and bad writes.
aws dynamodb update-continuous-backups \ --table-name chat-sessions \ --point-in-time-recovery-specification PointInTimeRecoveryEnabled=trueIt has its own cost, so check the pricing page. For anything you'd be unhappy to lose, it's worth it.
Step 5: Read and write from Python
import timeimport boto3from boto3.dynamodb.conditions import Key
table = boto3.resource("dynamodb").Table("chat-sessions")
def save_message(session_id, role, text, user_id, ttl_days=30): now = time.time() table.put_item( Item={ "session_id": session_id, "ts": int(now * 1000), # milliseconds, sort key "role": role, "text": text, "user_id": user_id, "expires_at": int(now) + ttl_days * 86400, # seconds, for TTL } )
def last_messages(session_id, limit=10): response = table.query( KeyConditionExpression=Key("session_id").eq(session_id), ScanIndexForward=False, # newest first Limit=limit, ) return list(reversed(response["Items"])) # back to oldest firstNotice we ask for the newest messages first and then reverse them. That's how you fetch "the latest N" efficiently. It reads only those items, not the whole conversation.
One boto3 quirk to remember: DynamoDB doesn't accept Python floats. If you store decimal numbers, such as a confidence score, wrap them in
Decimal , or the call will raise an error.Step 6: Feed the history back to the model
The stored messages become the conversation you send to Amazon Bedrock with the Converse API.
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")MODEL_ID = "YOUR_MODEL_ID"
def reply(session_id, user_id, user_text): save_message(session_id, "user", user_text, user_id)
history = last_messages(session_id, limit=10) messages = [ {"role": item["role"], "content": [{"text": item["text"]}]} for item in history ]
response = bedrock.converse(modelId=MODEL_ID, messages=messages) answer = response["output"]["message"]["content"][0]["text"]
save_message(session_id, "assistant", answer, user_id) return answerA few things to watch:
- The conversation must make sense to the model. Converse expects it to start with a user message and to alternate between user and assistant. If you trim history to the last N messages, make sure you don't start on an assistant message.
- More history costs more. Every message you send is billed as input tokens, and it all has to fit in the model's context window. Sending ten messages instead of a hundred is a cost control, as well as a size limit.
- Long conversations can be summarised. Store a short running summary alongside recent messages, and send that instead of the whole history.
Step 7: Find a user's sessions
What if you need all the sessions for one user? Your main key doesn't support that. A global secondary index (GSI) does: a second way to look up the same data.
Add one with
user_id as its partition key and ts as its sort key, and you can query "everything this user has said" without scanning the table. Scans read every item, so they're slow and expensive on a large table. Design an index for any lookup you'll do often.Step 8: React to new data with Streams
DynamoDB Streams records every change to your table, in order. You can connect it to an AWS Lambda function that runs whenever a message is added. That's useful for things like:
- Sending finished results to another system
- Counting usage per user
- Running a safety or quality check on new assistant messages
It's an advanced step, so skip it until you need it. It's good to know it exists.
Step 9: Limit who can touch the table
Your app's role should be able to do only what it does. For this design, that's three actions on one table and its index:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": ["dynamodb:PutItem", "dynamodb:Query", "dynamodb:GetItem"], "Resource": [ "arn:aws:dynamodb:us-east-1:123456789012:table/chat-sessions", "arn:aws:dynamodb:us-east-1:123456789012:table/chat-sessions/index/*" ] } ]}No scans, no deletes, no other tables. Replace the Region and account number with your own. This follows the least-privilege approach from the IAM article earlier in the AWS series.
If each user should only reach their own data, DynamoDB supports fine-grained access conditions based on the key. It's worth reading about if you're building a multi-user app.
Privacy: store less, keep it shorter
Conversation data is some of the most sensitive data your app holds.
- Don't store what you don't need. If you only need the last ten messages, don't keep ten thousand.
- Set an expiry on everything, as in Step 3.
- Keep secrets and card numbers out. Don't log them, and consider filtering them before you store.
- Restrict access to the table with IAM, and turn on the logging you need.
- Check your obligations. If you handle personal data, make sure your retention matches your privacy rules, and know how to delete one user's data if asked.
What it costs
With on-demand capacity you pay for the reads and writes you make, plus storage, plus extras like backups, streams and indexes. Chat history is small and cheap at low volume. The costs that surprise people are scans on large tables, very large items, and indexes that duplicate lots of data. Add the table to your budget alerts and tag it, as in the cost article.
Common mistakes
- Using milliseconds for TTL. It must be seconds.
- Scanning when you should query. Design a key or index for each lookup.
- Storing huge text in items. Use S3 for large content.
- Passing floats. Use
Decimal.
- Sending the entire history to the model every time. It's costly and may exceed the context window.
- Never expiring anything. Old conversations are a liability.
- Giving the app broad DynamoDB permissions.
Clean up
When you've finished experimenting, delete the table:
aws dynamodb delete-table --table-name chat-sessionsIf you turned on backups, check that any retained backups are removed too, and look at your bill after a day or two.
A checklist
- Does your key design match how you'll look data up?
- Is TTL on, with the expiry in seconds?
- Is point-in-time recovery on for data you care about?
- Does the app's role allow only the actions it needs, on one table?
- Are large items stored in S3, not DynamoDB?
- Do you limit how much history you send to the model?
- Have you decided how long conversations are kept?
Where we go next
Next in the series, we'll connect more pieces of the stack. We'll look at Amazon EventBridge and Step Functions, which let you run AI tasks in response to events and coordinate steps reliably.
DynamoDB limits, features, pricing and console steps change often. Check the current Amazon DynamoDB documentation before you follow any step here.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article