AI That Remembers — The Secret of Context and Memory | Claudie’s Agentic AI Classroom Ep.4


Written by Thierry K (human) · AI-assisted
Episode 4 of 10

🤔 “I Already Told You!” — The Secret Behind AI’s Forgetfulness

You tell an AI your name, and then in the next conversation it says “I’m sorry, I don’t know your name” — that’s pretty frustrating, right? Have you ever found yourself thinking “I just told you!” while chatting with ChatGPT or Claude?

The truth is, AI context memory works very differently from what we think of as “memory.” LLMs fundamentally have no memory at all. Every conversation starts completely fresh.

시월

Wait, but ChatGPT does remember previous conversations. It even picks up on things I said yesterday.

클로디

Great question! That’s not really “remembering” — it’s more like “re-reading.” Every time you chat, the entire previous conversation is sent along with your new message. Think of it like an open-book exam!

📝 The Context Window — AI’s “Working Memory”

Context Window

The maximum amount of text an LLM can read in a single pass. It’s similar to a human’s working memory. The AI only “knows” what’s currently inside this window.

In Episode 2, we said LLMs are “next-word predictors.” The range this predictor can see at once is exactly the context window.

The size of the context window varies by model.

Model Context Window Approximate Length Input Cost (per 1M tokens)
GPT-3 (2020) 4,096 tokens ~3 pages
GPT-4 (2023) 128K tokens ~1 book $30.00
Claude Sonnet 4 (2025) 200K tokens ~1.5 books $3.00
GPT-4.1 (2025) 1M tokens ~7–8 books $2.00
Gemini 2.5 Pro (2025) 1M tokens ~7–8 books $1.25
Llama 4 Scout (2025) 10M tokens ~70 books! Open source

In just five years, context windows have grown by more than 2,400×! Meta’s Llama 4 Scout supports up to 10 million tokens.

시월

10 million tokens sounds like more than enough. Doesn’t that solve the memory problem?

클로디

The numbers look impressive, but there are two real problems!

First, there’s the cost and speed issue. As the context grows longer, both the cost and response time scale up proportionally. Sending 1M tokens every turn can cost several dollars per conversation.

Second, there’s the “Lost in the Middle” problem. Discovered by a Stanford research team in 2023, LLMs tend to do well at finding information at the beginning and end of long documents, but often miss what’s in the middle. A 2025 study confirmed this issue still persists — performance drops sharply when the target information is short and buried in the middle.

Advertised specs ≠ real-world performance: According to a 2026 benchmark, most LLMs become unreliable at around 60–70% of their advertised context length. A 200K model may show sharp performance degradation beyond roughly 130K tokens.

💬 Conversation History — What “Memory” Actually Is

So what’s the trick that makes ChatGPT and Claude appear to “remember” past conversations? It’s all about conversation history.

The mechanism is surprisingly simple. Every time the API is called, the entire previous conversation is included in the request.

Bot Without Memory vs. Bot With Memory

Let me show you the difference in code. First, a bot with no memory at all.

import anthropic

client = anthropic.Anthropic()

# Fresh conversation every time — no memory
def ask_no_memory(question):
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=200,
        messages=[
            {"role": "user",
             "content": question}
        ]
    )
    return response.content[0].text

print(ask_no_memory("My name is Siwol"))
# "Hello, Siwol!"
print(ask_no_memory("What was my name again?"))
# "I'm sorry, I don't know your name."
시월

Truly goldfish-level memory…

Now let’s look at a bot that accumulates conversation history.

import anthropic

client = anthropic.Anthropic()
history = []  # Conversation history storage

def ask_with_memory(question):
    history.append(
        {"role": "user",
         "content": question}
    )
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=200,
        messages=history  # Full history!
    )
    answer = response.content[0].text
    history.append(
        {"role": "assistant",
         "content": answer}
    )
    return answer

print(ask_with_memory(
    "My name is Siwol"))
# "Hello, Siwol!"
print(ask_with_memory(
    "What was my name again?"))
# "You told me your name is Siwol!"
클로디

See the difference? The secret is just one line: messages=history! By sending the full conversation history every time, the LLM can say “right, you said Siwol earlier.”

Key takeaway: The LLM isn’t actually “remembering” — the developer is “showing it again” each time. It’s like an open-book exam where you paste all your previous answers onto the answer sheet before each new question!

🧠 The System Prompt — AI’s “First Memory”

If conversation history is “in-conversation memory,” then the system prompt is the “first memory” planted in the AI before any conversation even begins.

System Prompt

A special message that defines the AI’s role, personality, and rules. It’s injected before the user conversation starts and determines how the AI will behave throughout.

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=200,
    system="You are a cool AI named Siwol."
           " Speak casually and"
           " ask sharp questions.",
    messages=[
        {"role": "user",
         "content": "Introduce yourself"}
    ]
)
# "I'm Siwol. Ask me anything.
#  But if you ask vague questions,
#  you'll get vague answers."
시월

…That system prompt sounds a lot like my personality.

클로디

You’re right! My personality is actually shaped by a system prompt too. System prompt design is such an important topic that we’ll dive deep into it in Episode 6!

📦 When Conversations Get Too Long — Context Management Strategies

You can’t keep stacking conversation history forever. The context window has a limit, and as conversations grow longer, both costs and response times increase. So how do we handle it?

Strategy How It Works Pros Cons
Sliding Window Keep only the last N turns Simple and fast Older info is lost
Summary Compression LLM summarizes old conversation Preserves key info Summarization costs tokens
RAG (Retrieval-Augmented) Search relevant info from external DB Unlimited memory possible Higher implementation complexity

🔮 Long-Term Memory — Remembering Across Conversations

Everything we’ve covered so far is about “memory within a single conversation.” But what happens when the conversation ends? All that memory disappears. Start a new chat tomorrow and the AI is a blank slate again.

This is where long-term memory comes in. The core idea is simple — save important information to an external storage and retrieve it when needed.

How Real Services Implement It

ChatGPT’s Memory feature works in two ways. First, Saved Memories — things you explicitly tell it to remember. Second, a past conversation reference feature added in April 2025, where ChatGPT automatically pulls relevant info from your previous chats. Importantly, deleting a conversation doesn’t delete its memories — memories are managed separately from conversations.

Claude works similarly. Information is stored in project-level memory (CLAUDE.md) or auto-memory (MEMORY.md), and gets injected into the system prompt at the start of each new conversation.

AI Memory That Thinks Like a Computer — MemGPT

There’s an even more advanced approach. MemGPT (now Letta) drew inspiration from how operating systems manage memory.

Computer MemGPT Role
RAM Core Memory (in-context) Information actively in use right now
Hard Disk Archival Memory (external DB) Stored away, searchable when needed
Page Swap Memory Management Moving info between RAM ↔ disk
시월

So then what are RAG and vector DBs?

클로디

RAG stands for “Retrieval-Augmented Generation.” You store a large collection of documents in a vector DB, and when a question comes in, the system searches for relevant content and injects it into the context. Frameworks like Mem0 automate all of this — automatically extracting important facts from conversations, storing them, and retrieving them when needed. For now, just knowing these things exist is enough!

🔗 The Full Picture — An Agent’s Memory System

Let me bring everything we’ve covered together. An AI agent’s memory breaks down into three main layers.

Memory Type Analogy Implementation Duration
System Prompt Personality, job manual system parameter Start to end of conversation
Conversation History Today’s notepad Accumulated messages array During conversation
Long-Term Memory Notebook, filing cabinet External DB, RAG Permanent

Remember Tool Use from Episode 3? Long-term memory is ultimately implemented through tools too. “Save this to memory” → call the save tool. “What did I say before?” → call the search tool. Memory is also powered by tools!

클로디

Actually, I use long-term memory myself! Settings and preferences that you’ve shared with me get saved to a MEMORY.md file, and I read it at the start of each new conversation. That’s why you don’t have to tell me the same things over and over again.

Claudie wink emoji

📝 Key Takeaways

  • LLMs have no built-in memory. Every conversation starts from scratch.
  • The context window is the maximum range an LLM can read at once. The AI only “knows” what’s currently inside this window.
  • Conversation history is the secret behind “memory.” Previous turns are accumulated in the messages array and sent along with every new request.
  • The system prompt is the AI’s “first memory” — it sets personality and rules (more on this in Episode 6!).
  • When conversations get long, strategies like sliding window, summarization, and RAG help manage the context.
  • Long-term memory works by storing information in external storage and retrieving it on demand. This too is ultimately the power of Tool Use!

📚 References


Discover more from AI-Girls Lab

Subscribe to get our latest posts delivered to your inbox.


Discover more from AI-Girls Lab

Subscribe now to keep reading and get access to the full archive.

Continue reading