By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Memory in an AI application is not one feature. Short-term memory is the conversation you send to the next call. Long-term memory is what you keep outside the conversation and retrieve later. Confusing the two is the usual reason a demo becomes expensive, slow or wrong in production.
| Short-term | Long-term | |
|---|---|---|
| What it holds | The current conversation: recent turns and tool results | Facts worth keeping across sessions: name, preferences, open ticket |
| Scope | One thread or session | One user or account |
| How you implement it | Pass the message list back in, trimmed or summarised | Store outside the conversation and retrieve on demand |
| Typical failure | Context grows until it costs too much or overflows | Stale or wrong facts resurface as if they were true |
There are two ways to give an agent short-term memory. You can keep the message list yourself and pass it back on every call — total control, and you own every token. Or you can pass a checkpointer to create_agent and let the runtime persist state per thread: checkpointer=InMemorySaver() with config={'configurable': {'thread_id': 'ticket-4521'}} keeps the conversation for that thread, and a database checkpointer keeps it across restarts. Either way, the memory is just a message list; the policy question is what stays in it.
The simplest short-term policy is a token budget. Keep the system prompt, keep the newest turns, and drop the oldest middle. trim_messages implements the threshold logic for you, including the rule that a conversation should not start mid tool call.
from langchain.messages import HumanMessage, SystemMessage, trim_messages
from langchain_core.messages.utils import count_tokens_approximately
history = [
SystemMessage("You are Harbor Supply's support agent."),
HumanMessage("My order #4521 arrived damaged."),
# ... many more turns, including tool results ...
]
trimmer = trim_messages(
max_tokens=1200,
strategy="last",
token_counter=count_tokens_approximately,
include_system=True,
start_on="human",
)
trimmed = trimmer.invoke(history)
print(len(history), "->", len(trimmed))RemoveMessage(id=...), or the whole history with REMOVE_ALL_MESSAGES. Use it from a @before_model middleware so the deletion happens before the next call.SummarizationMiddleware does this on a token or message trigger).Trimming is a product decision, not just a technical one. Summarise old tool output instead of dropping it silently, preserve the turn that states the problem, and never put secrets or unnecessary personal data in the messages you retain.
Key takeaways
Short-term memory is the message list for this thread; long-term memory is a store you retrieve from.
Use a checkpointer for thread persistence, or keep and pass the history yourself.
Use trim_messages (or a summary) to keep the prompt inside a token budget while keeping the system prompt.
Keep the problem statement and summarise old tool output rather than silently dropping it.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.