Short-term memory for an AI agent is whatever sits in the context window during the current run. When the window fills, older turns get replaced with a summary, a step called compaction, and on Anthropic's API that step can now run on the server.
A hand-written sliding window with a scoring function for each message, which this page used to teach, is the wrong place to start on an API that does compaction for you.
Short-term memory is the only part of AI agent memory the model reads directly, and anything stored elsewhere has to be loaded into the context window first.
Once a product writes the summary for you, the risk is a bad summary. Claude Code 2.1.323, released on November 12, fixed a bug in which running /compact a second time could end up "summarizing away a message that Claude had not answered yet".
What is short-term memory in an AI agent?
Short-term memory is the working state of one run: the instructions, the conversation so far, tool results and any notes the agent has written to itself, all held as tokens in the context window. It ends when the run ends, unless something copies it out.
So short-term memory has a fixed budget, the size of the window. Every tool result and every reply spends from the same window, and how models use a long one is the subject of the context window guide.
Anything meant to outlive the run belongs to long-term memory, which has its own decisions about what to write and when to replace it. Context windows vs memory draws that line, and AI memory management for LLMs covers the storage side.
What happens when short-term memory fills up?
Something has to leave the window, and there are two ways to choose it: drop the oldest turns, or replace them with a summary. Dropping is simple and loses whatever was said early, which is often where the instructions were.
The script below builds a 40-message conversation whose first message is a rule, then fits it into a 400-token budget twice.
"""Fit a conversation into a token budget, with and without a pinned instruction."""
import tiktoken
encoding = tiktoken.get_encoding("o200k_base")
def tokens(text):
return len(encoding.encode(text))
def window(messages, budget, pinned=()):
"""Keep pinned messages, then the newest others that still fit."""
kept = [m for m in messages if m["turn"] in pinned]
used = sum(tokens(m["text"]) for m in kept)
for message in reversed(messages):
if message["turn"] in pinned:
continue
cost = tokens(message["text"])
if used + cost > budget:
break
kept.append(message)
used += cost
return sorted(kept, key=lambda m: m["turn"]), used
rule = "The production database is a read replica. Never write to it."
messages = [{"turn": 1, "text": rule}]
for turn in range(2, 41):
messages.append({"turn": turn, "text": f"Turn {turn}: the user asks about report {turn} and the agent "
f"answers with a summary of its columns, filters and row counts."})
total = sum(tokens(m["text"]) for m in messages)
print(f"conversation: {len(messages)} messages, {total} tokens")
for label, pinned in (("newest first", ()), ("turn 1 pinned", (1,))):
kept, used = window(messages, budget=400, pinned=pinned)
turns = [m["turn"] for m in kept]
print(f"{label:<14} keeps {len(kept):>2} messages, {used} tokens, oldest turn {turns[0]}, "
f"rule present: {rule in [m['text'] for m in kept]}")
With tiktoken installed, python3 window_demo.py gives two result lines, one without the rule and one with it.
conversation: 40 messages, 1066 tokens
newest first keeps 14 messages, 378 tokens, oldest turn 27, rule present: False
turn 1 pinned keeps 15 messages, 391 tokens, oldest turn 1, rule present: True
The plain window keeps the newest fourteen messages, the oldest of them turn 27, and the rule is gone. Pinning turn 1 costs thirteen tokens and keeps it, which is the whole case for deciding what is exempt before deciding what to drop.
What does compaction keep, and what does it lose?
Compaction keeps a model-written summary of the older turns and loses whatever the summary leaves out. Anthropic's compaction documentation says "Compaction replaces the older turns of a conversation with a summary that Claude writes on the server, so you need no summarization code of your own."
The documentation gives a second reason to use compaction, saying it keeps the active context small "because response quality degrades as a conversation grows".
Compaction has two modes, on demand and at a token threshold, and the page says both "are in beta". Its own advice is "Use on-demand compaction wherever it is available".
On demand, your code runs "A compaction loop that requests the summary and swaps it in". At a token threshold, compaction fires when "input tokens reach the trigger you set" and takes "One parameter on your ordinary requests".
Either way the summarizer is no longer code you write. I think that is the right default, and Claude Code's changelog is a useful record of how the step fails.
The /compact fix above is one case. Another, in version 2.1.326 from November 16, fixed compaction on Bedrock, Vertex and Foundry "sometimes replying to one of your messages instead of summarizing, or leaving Claude's last reply out of the summary".
Both bugs lost recent material, the part a summary is supposed to protect. A summary is a lossy copy made by a model, so anything that must survive word for word needs to live outside it.
How do you keep an instruction from being lost?
Put the instruction where compaction doesn't reach, and keep the raw history so a summary can be checked. Standing instructions belong in the system prompt or an instruction file, which is reloaded whole on every request.
The compaction page lists two options for everything else in the conversation. "Compaction that keeps recent turns" leaves the last few turns word for word, and a custom summarization prompt is for the case "when the default summary drops something a later turn needs."
For facts that come up mid-conversation, the habits worth copying come from replies posted on November 9 to a Hacker News thread titled "How do you manage your AI agents' context across clients and projects?" A commenter there wrote "I let agents read their own pre-compaction history often", which treats the transcript as the record and the summary as an index to it.
Another commenter ends each session by asking the agent to "prep a handoff so we can resume from here in a fresh session". A handoff note is short-term memory written down on purpose, by the model before the full context is gone.
A third practice in the thread carries more risk: "at the end ask the agent to summarize what it learned and write it down." Anything written that way has crossed into long-term memory, and a note that was wrong when it was written will be loaded as fact next time.
The commenter who writes those notes also keeps decisions as records with a state such as approved or rejected. That commenter and the one who asks for a handoff each link a tool they built.
How does short-term memory relate to long-term memory?
Short-term memory is what the model can see now, and long-term memory is what the application chooses to store and bring back later. The handoff note and the compaction summary are the two places where short-term memory turns into long-term memory.
That crossing is where to spend review effort. Stored state differs from retrieved documents in ways RAG vs memory sets out, and the state of agent memory surveys the products that manage it.
Long-term memory comes in more than one kind, and the types of agent memory differ in how they're read back.
FAQ
Is the context window the same thing as short-term memory?
The window is the container and short-term memory is its contents during one run. A larger window holds more, and it doesn't decide what stays when the limit is reached.
Do I still need my own summarization code?
No, where the API offers compaction. Anthropic's documentation says the summary is written on the server "so you need no summarization code of your own", and what remains yours is deciding what must never be summarized.
How do I add memory to an AI agent?
Start with short-term memory, since the agent already has it: pin the instructions, turn on compaction and keep the transcript. Add long-term storage when a fact has to survive the session, and decide then who is allowed to write it.