user-1, user-456, user-c). That session ID is the lookup key in the session store, and the stored value is the ordered list of messages (roles: user or assistant, message content, timestamps, metadata).
Example: a minimal in-memory session store (illustrative only — not for production):


- Truncation (sliding window): keep only the most recent N messages/turns.
- Summarization: compress older conversation segments into compact summaries that preserve important context.
- Hybrid strategies: combine selective truncation, summaries, and per-message indexing in a datastore.
Using DynamoDB for session storage
Amazon DynamoDB is a serverless, low-latency, highly scalable key-value and document store that often fits session-storage needs. However, DynamoDB’s query semantics (partition key + optional sort key) and item-size limits influence your table design.
A straightforward approach is to store an entire conversation as a single item keyed by
sessionId. On each request you fetch the item, append new messages, and write it back. This is easy to implement but may hit DynamoDB item size limits as history grows.

- Timestamp every message for ordering and auditability.
- Use TTL (time-to-live) to expire stale sessions automatically.
- Summarize older turns proactively to control token growth.
- Store messages as individual items (one message per row) instead of a single large blob to enable selective retrieval and smaller reads/writes.

Design your DynamoDB table keys and indexes around your access patterns. If you need to query by attributes other than
sessionId, add appropriate secondary indexes or adapt your schema to support efficient reads.- A single DynamoDB item has a maximum size of 400 KB. Storing an ever-growing session as one item can hit this limit.
- Per-message items (one row per message) enable partial retrieval and reduce the likelihood of large writes.
- Implement TTL and summarization to avoid unbounded growth and to keep latency predictable.
- If you need to store large message payloads (attachments, long transcripts), consider external blob storage (S3) and reference keys in DynamoDB.
Avoid unbounded growth in stored session history. Implement TTLs, summarization, or per-message partitioning to prevent exceeding DynamoDB item size limits and to keep retrieval/write latency predictable.
- Designing for context windows and token budgets when building conversational AI
- Approaches to incremental summarization for long-running conversations
- DynamoDB best practices for time-series and per-item growth patterns