Building a three-layer agent memory: short-term, long-term, and session state. Strategies for storing, semantically retrieving, and pruning memory, plus token usage and state size constraints.

In episode 7 you could observe agent behavior through logs and replay. But there is still one capability we have not covered: memory. An agent that does not remember yesterday's conversation will ask the same things over and over. This episode builds the memory foundation in Hermes.
Here is the roadmap for this episode: first, defining the three memory layers — short-term, long-term, and session state; second, strategies for storing, retrieving, and pruning memory; third, constraints on token usage and state size.
Memory in Hermes is divided into three layers based on age and function:
The three work together. While a conversation is in progress, short-term memory is the bridge; when the session ends, important things are extracted into long-term memory.
Memory configuration lives in the agent profile. You set the short-term capacity, the long-term backend, and the session policy.
memory:
shortTerm:
maxTurns: 10
maxTokens: 4000
longTerm:
store: vector
embeddingModel: text-embedding-3-small
maxEntries: 500
ttlDays: 90
session:
timeoutMinutes: 30
pruneOnEnd: trueHere short-term is limited to 10 turns or 4000 tokens — whichever comes first. Long-term uses a vector store with a 90-day lifetime. The session will be pruned automatically after a 30-minute timeout.
Long-term memory is stored as text chunks with embeddings. When needed, retrieval is semantic: Hermes searches for the chunks most relevant to the user's question, not just a word match.
import { hermes } from "@hermes/agent";
await hermes.memory.save({
type: "fact",
key: "user.order.preference",
content: "Arman lebih suka tracking lewat WhatsApp",
sessionId: "ses_order-check",
});
const results = await hermes.memory.search(
"cara user ingin tracking order",
{ limit: 3 }
);hermes.memory.save stores an entry of type fact, while hermes.memory.search retrieves the most semantically relevant entries. This pattern is the foundation of personalization: the agent remembers user preferences across sessions without being asked again.
Warning
Long-term memory contains personal user data. Store sensitive entries encrypted and make sure retrieval is only for authorized sessions — the access control details are in episode 11.
Memory that grows without limit causes two problems: token costs balloon and retrieval gets slow. That is why pruning is a routine policy, not an incident. Some common strategies:
ttlDays; old entries are overwritten when maxEntries is reached.An example of pruning with summarization, run when the context starts to fill up:
import { hermes } from "@hermes/agent";
const summary = await hermes.memory.summarize(
"ses_order-check",
{ targetTokens: 500 }
);
await hermes.memory.prune("ses_order-check", {
keepLast: 3,
replaceOldestWith: summary,
});hermes.memory.summarize condenses a long conversation into a 500-token summary, and then hermes.memory.prune drops the old turns and replaces them with the summary. The result: context stays intact, tokens stay under control.
All the strategies above work under one governing rule: budget. Every provider has a context window limit; Hermes manages the entire context contents so it never exceeds it.
The key is proportion. From the total context window, allocate space for the system prompt, history summary, tool definitions, retrieval results, and the model's output space. If the total exceeds the limit, the kernel trims from the least important parts — usually the oldest history.
hermes memory stats --session ses_order-checkThis command shows input_tokens, output_tokens, the turn count, and the session state size. Make this routine: if the session state balloons, it is a sign that pruning or a fresh session is needed.
Info
State size is not just about tokens — there is also binary data and tool result caches. Store large tool results in the long-term store and let short-term hold only their summaries.
Memory in Hermes is composed of three layers: short-term for immediate context, long-term for cross-session knowledge, and session state for temporary data. You can now define it in the profile, store and retrieve entries semantically, prune with summaries, and keep the token budget under control.
Key takeaways:
hermes memory stats monitors the health of memory and session state.In episode 9 we continue to Multi-turn Conversation & Context Handling — managing conversation state across turns, context retrieval and prompt window management, and handling topic switches and conversation resets. See you there!