DEV Community

Cover image for Day 54: Giving an LLM Long-Term Memory with DynamoDB
Eric Rodríguez
Eric Rodríguez

Posted on

Day 54: Giving an LLM Long-Term Memory with DynamoDB

One of the biggest limitations of stateless serverless applications using LLMs (like Amazon Bedrock or OpenAI) is amnesia. Every API call starts from zero.

Today, I built a persistent memory engine for my AI Financial Agent so it remembers a user's behavior from previous months.

The Architecture:
I created a DynamoDB table with user_id (from Cognito JWT) as the Partition Key and month_year as the Sort Key.

Before calling Bedrock, my Python Lambda fetches last month's summary:

def get_user_memory(user_id, prev_month_str):
# DynamoDB get_item logic...
I then inject this into the LLM prompt:

Plaintext
PREVIOUS MONTH MEMORY:
"{past_memory}"
Analyze the current transactions and compare them to the past memory.

The crucial part? I force the LLM to output a memory_for_next_month string in its JSON response.

{
"score_feedback": "You spent 50€ less on eating out than last month. Good job.",
"memory_for_next_month": "User successfully cut back on dining expenses but increased tech spending."
}

Lambda intercepts this key and saves it back to DynamoDB using the current month's timestamp. It creates an infinite loop of context, turning a basic API wrapper into a continuous AI companion!

Top comments (0)