# Capture, store, recall: how memory for AI agents works

URL: https://memoryforaiagents.com/how-it-works
Last updated: 2026-11-07
Published by Glen (https://tryglen.com), shared memory for coding agents.

> Agent memory works in three stages. Capture records each turn, ideally through the agent's hooks so nothing depends on the model choosing to save. Store keeps those records for the organization alongside other sources, and recall returns the relevant memories, either injected before the model reads a prompt or on demand through a search.

## 1. Capture: hooks, not save tools

Most memory tools expose a save call and hope the model uses it. Glen's plugins attach to lifecycle hooks the agent fires anyway, and each hook runs one `glen` CLI command. The model never has to think about memory for capture to happen.

*What Glen hooks into, per agent*

| Agent | Hooks Glen uses | What gets recorded |
| --- | --- | --- |
| Claude Code | SessionStart, UserPromptSubmit, Stop, PostToolUse on Bash | Prompt, reply, tool calls and results, thinking blocks, repo, branch, commits |
| Codex | The same four, in `~/.codex/hooks.json` | Prompt and final reply |
| Cursor | sessionStart, beforeSubmitPrompt, afterAgentResponse, afterShellExecution | Each prompt and response |
| Grok Build | Plugin installed by `glen install --agent grok` | Sessions, with recall through `glen search` |
| Pi | Package installed by `pi install npm:@tryglen/pi` | Sessions, with recall each turn |

The PostToolUse hook on Bash links every `git commit` to the session that made it. That link is what later lets `glen code-search` go from a line of code back to the conversation behind it.

## 2. Store: one organization memory

Captured turns go to a hosted memory owned by your organization. Integrations add public Slack channels, GitHub (which can include private repos), tickets, docs and meeting transcripts. You don't manage an index or a schema; you install plugins and connect sources.

Two things happen to the store over time. Decisions get reversed, and Glen aims to follow the one that stuck. And some memories turn out wrong: `glen forget` stops one being recalled, and `glen restore` reverses that.

## 3. Recall: injected or searched

- **Injected.** In Claude Code and Codex, UserPromptSubmit sends the prompt with repo and branch, and matching memories join the context before the model reads it. Pi recalls at the start of every turn.
- **Searched.** Cursor and Grok Build can't add context from a hook, so a rule tells the agent to run `glen search`. Any agent or person can do the same.
- **Traced.** `glen code-search <path:start-end> "<question>"` follows git blame to the agent sessions that wrote those lines.

> **Read the sources:** `glen search` returns a written answer with a citation for every claim, in about 10 seconds. A language model writes that answer, so it can be stale or wrong. Check the cited sources before acting on it.

> **See capture and recall on your own sessions** Install the plugin, import the last 30 days with glen import, and ask a question. Free to 15,000 memories. Install: `npx @tryglen/cli@latest install`. Start free at https://tryglen.com

## The same pipeline over MCP

Agents without a Glen plugin can reach the memory through the remote MCP server at `https://mcp.tryglen.com/mcp` (Streamable HTTP, OAuth 2.1 or a personal API key). It exposes 22 tools, among them `glen_search`, `glen_code_search`, `glen_forget` and `glen_import`, with a 60 second request limit and a 4.5 MB request size limit. Setup per client is covered on [memorymcp.dev](https://memorymcp.dev).

Does the pipeline pay for its round trips? Glen's [replay of 100 merged PRs](https://tryglen.com/blog/replaying-100-prs) found 29% fewer tokens and 21% faster runs with memory, quality at parity or better. Recall in that test was injected before the first turn.

## Frequently asked questions

### How does an AI agent retrieve memories?

Either the memory is injected into context before the model reads the prompt, or the agent calls a search tool when it decides it needs past context. Glen does injection where the agent's hooks allow it and search everywhere.

### Does agent memory slow every prompt down?

Each hook is a network round trip. In Glen's PR replay, runs with memory were still 21% faster overall with that time counted.

### What does Glen record from Codex?

The prompt and the final reply for each turn, plus repo and branch, through hooks that glen install registers in ~/.codex/hooks.json.

### Can I delete a wrong memory?

Run glen forget on it and it stops being recalled. It is reversible with glen restore, so a mistaken forget can be undone.
