Conversation memory for WhatsApp agents
How to give a WhatsApp agent memory: message window, summaries, per-contact long-term memory and Anthropic’s memory tool.
The model API remembers nothing between calls. Whatever memory a WhatsApp agent appears to have, you assemble it on every reply from what was stored. Deciding what to store, how much to resend and for how long is half the work of a good agent, and also where cost grows without warning.
Why does the agent not remember the conversation on its own?
Because each model call is independent: it only sees what you send in that request. WhatsApp delivers each customer message as an isolated event, and the send API does not keep conversations either. The bridge between one message and the next is your worker, reading and writing state in a database.
This has a practical consequence: the history you resend is part of the cost of every reply and of the time the model takes. A 60-message conversation resent whole on every turn costs far more than the same conversation with a short window, and it almost never needs the beginning. The event fields you can store are described in the webhooks reference, and the architecture article in this series covers the overall layout; here the focus is memory.
What is the simplest kind of memory, and where does it fail?
The sliding window: store every message but send the model only the last N. It is simple, cheap to run and enough for most short support chats. It fails when the important information sits outside the window, such as an order name mentioned 40 messages ago.
// Sliding window: last N messages, always starting with a customer one
function aparar(historico, n = 20) {
const recente = historico.slice(-n)
while (recente.length && recente[0].role !== 'user') recente.shift()
return recente
}Two cautions in the snippet. The history must start with a customer message, because cutting in the middle can leave an assistant reply at the front, without the question that prompted it, and the model starts reading a dialogue that begins halfway. And only what the customer actually received should enter the history: if a send failed, the reply the model generated never happened for the customer, and recording it makes the agent talk as if it had said something the customer never saw.
When is it worth summarizing instead of cutting?
When the conversation is long and what fell behind still matters. Instead of discarding old messages, you ask the model to condense them into a few lines and send that summary plus the recent messages. The gain is keeping durable facts, such as a stated preference, an open problem and a decision made, without carrying the whole dialogue.
- When to summarize: when passing a message or token limit, not on every turn.
- What goes in: facts, decisions and open items. Greetings and small talk do not.
- Where to store: in the same conversation record, with the summary date and the id of the last summarized message.
- How to avoid drift: the summary is generated from the previous summary plus new messages; sample-check whether it is losing facts.
Anthropic’s documentation also describes server-side context management features, such as compaction, which summarizes the conversation as it nears the context window limit, and context editing, which clears tool results. They exist, and the memory tool page cites them as complements. Evaluate whether they fit before building your own routine; the advantage of your own is full control over what is stored and where.
How do you give the agent long-term memory about a contact?
By storing durable facts in per-contact storage and loading them at the start of each conversation. Anthropic offers a memory tool for this, where the model creates, reads, edits and deletes files in a memory directory, and your application runs those operations on storage you control.
The tool is client-side: the model only requests the operation and your code executes it. The type is memory_20250818, the name must be memory, and the commands are view, create, str_replace, insert, delete and rename. The /memories path is just a prefix your handler maps onto real storage, for example one space per user. For a WhatsApp agent, the natural space is one per channel and contact pair.
// Anthropic’s memory tool, bound to a space PER CONTACT.
// The model sees "/memories/..."; your handler swaps the prefix for the contact.
const tools = [{ type: 'memory_20250818', name: 'memory' }]
function caminhoReal(canal, contato, caminhoDoModelo) {
if (!caminhoDoModelo.startsWith('/memories')) throw new Error('outside /memories')
const resto = caminhoDoModelo.slice('/memories'.length)
if (resto.includes('..') || resto.includes('%2e')) throw new Error('invalid path')
return 'memoria/' + canal + '/' + contato + resto // key in your storage
}The documentation is explicit about the risk: the handler must validate every path of every command, because a path like /memories/../../secrets can escape the directory. Reject traversal sequences, resolve the canonical path and check it stays inside that contact’s space. If the space is not isolated per contact, a customer could, through prompt injection, lead the model to read someone else’s memory.
What should you not store, and for how long?
Store only what changes a future reply and has a clear purpose; the rest is risk without benefit. The memory tool page notes the model usually refuses to write sensitive information, but recommends your own validation to strip such data before writing, plus capping file sizes and expiring files not accessed for a long time.
- Do not store documents, passwords, card numbers or codes received by message. Remove or mask them before persisting.
- Define retention: delete raw history and memory of inactive contacts after a period you can justify.
- Offer the customer a way to ask the agent to forget what it knows, and actually implement deletion.
- Treat memory contents as untrusted when reading them back: they were written from what the customer said and can carry planted instructions.
Meta’s rules on using platform data to train models also weigh in here; the article on Meta’s chatbot rules in this series covers them. To send the replies themselves, the quickstart is the shortest path.
Window, summary or memory: which combination should you use?
Start with the window. Add summaries when long conversations start losing relevant context, and long-term memory only when the agent needs to recognize the contact across separate conversations. Each layer adds cost, storage and risk surface.
| Layer | Solves | Cost | Main risk |
|---|---|---|---|
| Window of N messages | Current conversation context | Low and predictable | Losing old facts |
| Summary | Long conversations | One extra call when summarizing | Drift and detail loss |
| Per-contact memory | Continuity across conversations | Storage and reads | Sensitive data and leaks between contacts |
Whichever combination you pick, measure: compare replies with and without each layer on the same set of test conversations. That is the subject of the evaluation article in this series.
Frequently asked questions
How many messages should I resend to the model on each reply?
There is no universal number. The Hub guide example uses the last 20, always starting with a customer message. Test on your own conversation set and pick the smallest value that does not degrade replies.
Does Anthropic’s memory tool store data at Anthropic?
No. It runs client-side: the model requests the operation and your application executes it on storage you control, according to the documentation.
Do I need long-term memory for a simple support agent?
Usually not. A sliding window and a good system prompt cover most cases. Add per-contact memory only if recognizing the customer across conversations is a requirement.
How do I stop one customer’s memory from showing up for another?
By isolating storage per channel and contact, validating every path in the tool operations and never building the memory from a key the customer can influence.
Fontes
Documentação oficial da Meta, conferida em 31 de outubro de 2026.