# Short-term and long-term memory in AI agents

URL: https://longtermmemoryai.dev/short-term-vs-long-term
Last updated: 2026-11-07
Published by Glen (https://tryglen.com), shared memory for coding agents.

> Short-term memory in an AI agent is its context window: the prompt, files and tool output it can see right now, gone when the session ends. Long-term memory is stored outside the model and survives across sessions, machines and people. A useful setup moves knowledge between them on its own: written down as each turn finishes, and read back when a later prompt needs it.

## Short-term memory is the context window

Everything an agent reasons over in a session sits in its context window: your prompt, the files it read, the commands it ran and their output. The window is large but finite, and agents compact or drop older turns as it fills. When you close the session, all of it is gone.

Engineers notice this most on long tasks. An agent figures out on turn 12 that a test is flaky because of a shared fixture, then on turn 80, after a compaction, rediscovers it from scratch. Next morning's session never knew.

## Long-term memory sits outside the model

*Short-term and long-term memory in an AI coding agent*

|  | Short-term (context window) | Long-term (memory store) |
| --- | --- | --- |
| Lifetime | One session | Months, across sessions |
| Scope | One agent, one task | One person, or a whole organization |
| Size limit | The model's context length | Bounded by storage, not by the model |
| What gets in | Whatever the agent reads this session | What was recorded or written earlier |
| Cost | Tokens on every turn | A lookup, plus tokens for what is recalled |

The built-in long-term layers are deliberately small because they load whole. Claude Code's auto memory loads the first 200 lines or 25 KB of its index per session, and Codex reads `AGENTS.md` files up to 32 KiB by default. Both live on one machine or in one repo.

## Moving knowledge from one to the other

The interesting part is the hand-off. If writing to long-term memory waits for the model to decide something matters, most of the session is lost. If reading waits for the model to think of searching, memory stays unused.

1. **At the end of each turn**, Glen's Stop hook records what happened. In Claude Code that includes tool calls, tool results and thinking blocks; in Codex, the prompt and final reply.
2. **Before the next prompt reaches the model**, the UserPromptSubmit hook sends the prompt with repo and branch, and Glen injects the memories that match.
3. **When the agent needs more**, it can run `glen search` for a written answer with a citation behind each claim, or `glen code-search` to follow git blame back to the conversation that wrote a line.

> **Injected or on demand:** Claude Code, Codex and Pi get recall before the model reads the prompt. Cursor and Grok Build hooks can't add context, so recall there happens when the agent runs `glen search`.

> **Stop rediscovering what a past session already knew** Glen records each turn as it ends and recalls it when the next prompt needs it. Start free with 15,000 memories. Install: `npx @tryglen/cli@latest install`. Start free at https://tryglen.com

## Why long-term memory has to be selective

Long-term memory competes with the task for room in the short-term window. Recall that pastes in everything crowds out the code the agent should be reading. Glen recalls by relevance to the prompt, so a month of a team's sessions costs the agent only the few memories that bear on the change at hand. For more on budgeting the window itself, see [context window memory](https://contextmemory.app).

Does the extra recall pay for itself? In Glen's own replay of 100 merged PRs, agents with memory used 29% fewer tokens and finished 21% faster at equal or better quality. That replay used per-prompt injection, so agents that only search on demand may see different numbers. [The full method](https://tryglen.com/blog/replaying-100-prs) is published.

## Frequently asked questions

### What is short-term memory in AI?

The context window: everything the model can see during the current session. It is cleared when the session ends and trimmed as it fills.

### Is the context window the same as memory?

It is working memory for one session. Anything you want an agent to know next week has to be stored outside the model and loaded back into the window later.

### Does a bigger context window remove the need for long-term memory?

No. A bigger window holds more of today's session, but it still empties when the session ends, and it never contains a teammate's sessions.
