Prompt

How do I add memory to an AI agent?

Artificial Intelligence / AI Agents2 observationsLast seen Nov 16, 2026

Latest observation

Nov 16, 2026OpenAI APIWeb search: off

Adding “memory” to an AI agent usually means giving it a way to store, retrieve, and use past information across interactions. There are a few common patterns, and the right one depends on what kind of memory you want.

1) Start with the main kinds of memory

Short-term memory

Used within a single conversation or task.

  • Examples: the last few messages, current goals, intermediate steps
  • Usually stored in the prompt/context window

Long-term memory

Persisted across sessions.

  • Examples: user preferences, prior decisions, facts learned over time
  • Usually stored in a database, vector store, or key-value store

Working memory

Temporary scratch space for reasoning.

  • Examples: plans, state variables, task progress
  • Often represented as structured JSON or a state object

2) Common implementation approach

A. Store memories externally

Don’t rely only on the model’s context window. Save information in:

  • Relational DB for structured facts
  • Key-value store for preferences and session data
  • Vector database for semantic recall of past notes/messages
  • Document store for chat logs, summaries, artifacts

B. Retrieve relevant memories before responding

When the agent gets a new input:

  1. Parse the input
  2. Search memory for relevant items
  3. Add the retrieved items to the prompt
  4. Generate the response
  5. Optionally write new memory back

C. Summarize over time

For long chats, keep:

  • full raw history externally
  • a rolling summary in the prompt
  • key facts extracted into structured memory

3) A practical memory architecture

A simple and effective setup:

  • Conversation buffer: last N messages
  • Session summary: compact summary of the conversation so far
  • User profile memory: preferences, stable facts
  • Episodic memory: important past events, stored with timestamps
  • Semantic memory: embeddings for retrieval by similarity

4) Example flow

When user says: “Remember that I prefer Python and concise answers.”

  1. Detect this is a memory-worthy statement
  2. Extract structured fact:
    {
      "type": "preference",
      "key": "language",
      "value": "Python"
    }
    
  3. Store it in persistent memory
  4. Later, when answering, retrieve it and adapt response style

5) What to store

Good memory candidates:

  • User preferences
  • Stable identity facts
  • Project goals
  • Decisions already made
  • Important constraints
  • Frequently used resources

Avoid storing:

  • Everything verbatim
  • Sensitive data unless necessary and permitted
  • Highly transient or low-value chatter

6) Memory writing rules

Only write to memory when:

  • The user explicitly says to remember something
  • A fact is clearly persistent and useful
  • A decision or preference should affect future behavior

A good rule: save durable, actionable, user-relevant information.


7) Memory retrieval rules

Before each response:

  • Search by semantic similarity
  • Filter by recency and relevance
  • Prefer structured memory for stable facts
  • Use summaries for broad context
  • Don’t overload the prompt with too much memory

8) Minimal pseudo-architecture

def agent_respond(user_message, user_id):
    memories = retrieve_relevant_memories(user_id, user_message)
    recent_chat = get_recent_messages(user_id)
    summary = get_session_summary(user_id)

    prompt = build_prompt(
        summary=summary,
        recent_chat=recent_chat,
        memories=memories,
        user_message=user_message
    )

    response = llm_generate(prompt)

    new_memories = extract_memories(user_message, response)
    save_memories(user_id, new_memories)

    update_summary(user_id, user_message, response)

    return response

9) If you want the simplest version

The easiest memory system is:

  • Store chat history in a database
  • Keep a running summary
  • Retrieve the most relevant old messages with embeddings
  • Insert retrieved items into the prompt

That gets you a useful “memory” without making the system too complex.


10) Important caveats

  • Context window is not memory — it’s only short-term input
  • Memory can be wrong — let users correct it
  • Privacy matters — be careful with personal data
  • Too much memory hurts performance — retrieve selectively

If you want, I can also show you:

  1. a Python example using a vector database,
  2. a LangChain/LlamaIndex implementation, or
  3. a production-grade memory design for an AI assistant.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (2 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.