Skip to main content
Glama
0gm-memory

0gm-memory

Official
by 0gfoundation
README.md
# 0gm-memory

**Long-term memory for coding agents.** Your agent's past sessions are saved to 0GM-2.0's in-model memory
(0GM-CMSA), and when it needs history it gets the exact past messages back, quoted word for word with their dates.

[![CI](https://github.com/0gfoundation/0gm-memory/actions/workflows/ci.yml/badge.svg)](https://github.com/0gfoundation/0gm-memory/actions/workflows/ci.yml)
[![Licence: MIT](https://img.shields.io/badge/licence-MIT-blue.svg)](https://github.com/0gfoundation/0gm-memory/blob/main/LICENSE)
[![Python](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue.svg)](https://github.com/0gfoundation/0gm-memory/blob/main/pyproject.toml)

Claude Code · Codex · OpenCode · Hermes Agent · Cursor · Cline · any MCP client

<!-- mcp-name: io.github.0gfoundation/0gm-memory -->

- **Agents forget.** Every new session starts blank, so decisions, reverted attempts and their reasons get lost.
- **This remembers for them.** Finished sessions are saved automatically, secrets stripped on your machine first, into
  a wallet that holds up to 120M tokens of memory.
- **It returns evidence, not a summary.** `ask_memory` hands back dated, verbatim quotes, so the agent reasons over
  what was actually said.

[Quick start](#quick-start) · [Install per agent](#install-per-agent) · [How it works](#how-it-works) ·
[Results](#results) · [Privacy](#privacy-and-data) · [FAQ](#faq)

## In 15 seconds

```mermaid
sequenceDiagram
    autonumber
    participant A as Coding agent
    participant P as 0gm-memory (your machine)
    participant W as 0GM-CMSA memory
    A->>P: session ends (hook or plugin)
    P->>P: redact secrets, buffer locally
    P->>W: save once the buffer passes about 70K tokens
    Note over A,W: days later, in a fresh session
    A->>P: ask_memory("why is the dev server on 47831?")
    P->>W: find the relevant past messages
    W-->>P: verbatim quotes with session dates
    P->>P: check each date against a local index
    P-->>A: [2026-11-24] User: "the Zebra scanner bridge hard-codes 8080 ..."
```

The same question in a fresh session, in the invented warehouse project (`pallet-ledger`) used for the real-session
tests:

<table>
<tr><th>Without memory</th><th>With 0gm-memory</th></tr>
<tr valign="top"><td>

**You:** Why is the dev server on port 47831 and not 8080?

**Agent:** `PORT` defaults to 47831 in `config.py`, but nothing in the code says why. It may have been chosen to
avoid a conflict. Shall I change it back to 8080?

</td><td>

**You:** Why is the dev server on port 47831 and not 8080?

**Agent** *(calls `ask_memory`)*:

> [2026-11-24] User: "Move the dev server off port 8080. Reason: the Zebra scanner bridge on the dock PCs hard-codes
> 8080, so 'pallet-ledger serve' collides with it on every dock PC. We decided on port 47831."

It moved because the scanner bridge on the dock PCs owns 8080; changing it back would break every dock PC.

</td></tr>
</table>

The quote is verbatim from a real Claude Code session on 24 Nov 2026; the answers are shortened.

## What each agent gets

| Agent | Ask (`ask_memory`) | Auto-save | How sessions are saved | Tested in a real session |
|---|---|---|---|---|
| Claude Code | yes | yes | `SessionEnd` and `PreCompact` hooks | yes, 24 Nov 2026 |
| Codex CLI | yes | yes, once its hooks are trusted | `SessionEnd` and `PreCompact` hooks on the rollout | yes, 24 Nov 2026 |
| OpenCode | yes | yes | plugin on the session idle hook; `save --opencode` for past sessions | yes, 24 Nov 2026 |
| Hermes Agent | yes (`zgm_ask_memory`) | yes | memory provider, after every turn | yes, 24 Nov 2026 |
| Cursor | yes | no, ask only | its chat history is not in a format this package reads | no (manifests checked only) |
| Cline | yes | no, ask only | none | no |
| Other MCP clients | yes | no, ask only | `0gm-memory save FILE` by hand, for a supported transcript format | no |

Sessions saved from one agent can be asked about from any other agent on the same key.

## Quick start

You need [uv](https://docs.astral.sh/uv/) (or pipx) and an API key from [pc.0g.ai](https://pc.0g.ai) for a wallet
that will hold only this project's memory. uv fetches Python 3.10 or later if needed.

```sh
uv tool install 0gm-memory              # or: pipx install 0gm-memory
export ZGM_MEMORY_KEY=<key from pc.0g.ai>
0gm-memory status                        # free, no network: shows the wallet and an empty buffer
```

Or run it without installing: `uvx 0gm-memory status`.

Connect your agent. For Claude Code:

```sh
claude mcp add 0gm-memory -e ZGM_MEMORY_KEY="$ZGM_MEMORY_KEY" -- 0gm-memory serve
```

and merge [`integrations/claude-code/hooks/hooks.json`](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/claude-code/hooks/hooks.json) into
`.claude/settings.json` so sessions are saved. Already have history? `0gm-memory save --claude` (or `--codex`,
`--opencode`) saves this folder's past sessions. Then ask your agent about something you decided earlier.

## Install per agent

Each block assumes the command is installed as in the quick start. To run it without installing, use
`uvx 0gm-memory` in place of `0gm-memory` (the save hooks are faster with the installed command).

<details>
<summary><b>Claude Code</b>: MCP server and save hooks</summary>

```sh
claude mcp add 0gm-memory -e ZGM_MEMORY_KEY="$ZGM_MEMORY_KEY" -- 0gm-memory serve
```

Merge [`hooks/hooks.json`](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/claude-code/hooks/hooks.json) into `.claude/settings.json`. To keep the key
per project, put `{ "env": { "ZGM_MEMORY_KEY": "<key>" } }` in `.claude/settings.local.json` (not committed).

Or install the plugin, which sets up both in one go: `/plugin marketplace add 0gfoundation/0gm-memory`, then
`/plugin install 0gm-memory@0gm-memory`. Details, timeouts and safety nets:
[integrations/claude-code](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/claude-code/README.md).
</details>

<details>
<summary><b>Codex CLI</b>: MCP server and save hooks</summary>

```sh
codex mcp add 0gm-memory --env ZGM_MEMORY_KEY="$ZGM_MEMORY_KEY" -- 0gm-memory serve
```

Merge [`hooks.json`](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/codex/hooks.json) into `~/.codex/hooks.json` and **trust the hooks**: Codex skips
untrusted hooks silently, and `codex exec` never asks. How to trust them, and the `env_vars` list for
`config.toml`: [integrations/codex](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/codex/README.md).
</details>

<details>
<summary><b>OpenCode</b>: MCP server and a save plugin</summary>

In the project's `opencode.json`:

```json
{ "mcp": { "servers": { "0gm-memory": { "type": "local", "command": ["0gm-memory", "serve"],
  "environment": { "ZGM_MEMORY_KEY": "{env:ZGM_MEMORY_KEY}" }, "timeout": { "execution": 300000 } } } } }
```

Copy [`plugins/0gm-memory.js`](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/opencode/plugins/0gm-memory.js) to `.opencode/plugins/` to save
sessions, and run `0gm-memory save --opencode` once for past ones. Details:
[integrations/opencode](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/opencode/README.md).
</details>

<details>
<summary><b>Hermes Agent</b>: memory provider</summary>

```sh
hermes plugins install 0gfoundation/0gm-memory/integrations/hermes
hermes memory setup     # choose 0gm-memory, paste the key
```

The provider imports the `0gm-memory` package, which Hermes installs from PyPI as a declared dependency. See [integrations/hermes](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/hermes/README.md).
</details>

<details>
<summary><b>Cursor</b>: ask only</summary>

In `.cursor/mcp.json`:

```json
{ "mcpServers": { "0gm-memory": { "command": "0gm-memory", "args": ["serve"],
  "env": { "ZGM_MEMORY_KEY": "${env:ZGM_MEMORY_KEY}" } } } }
```

Or use the one-click [Add to Cursor](cursor://anysphere.cursor-deeplink/mcp/install?name=0gm-memory&config=eyJjb21tYW5kIjoidXZ4IiwiYXJncyI6WyIwZ20tbWVtb3J5Iiwic2VydmUiXSwiZW52Ijp7IlpHTV9NRU1PUllfS0VZIjoiJHtlbnY6WkdNX01FTU9SWV9LRVl9In19)
link and the Cursor plugin. Details: [integrations/cursor](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/cursor/README.md).
</details>

<details>
<summary><b>Cline</b>: ask only</summary>

In `cline_mcp_settings.json` (or ask Cline to install this repository; it follows [llms-install.md](https://github.com/0gfoundation/0gm-memory/blob/main/llms-install.md)):

```json
{ "mcpServers": { "0gm-memory": { "command": "0gm-memory", "args": ["serve"],
  "env": { "ZGM_MEMORY_KEY": "<key>" }, "timeout": 300 } } }
```
</details>

<details>
<summary><b>Any other MCP client, Docker</b></summary>

```json
{ "mcpServers": { "0gm-memory": { "command": "0gm-memory", "args": ["serve"],
  "env": { "ZGM_MEMORY_KEY": "<key>" } } } }
```

`"command": "uvx", "args": ["0gm-memory", "serve"]` needs no install step. Docker:
[integrations/docker](https://github.com/0gfoundation/0gm-memory/blob/main/integrations/docker/README.md).
</details>

## How it works

```mermaid
flowchart LR
    A[Session ends<br/>or compacts] -->|hook or plugin| B[Read transcript]
    B --> C[Drop memory lookups,<br/>redact secrets]
    C --> D[(Local buffer)]
    D -->|over ~70K tokens| E[One multi-turn call<br/>to the 0G router]
    E --> F[(0GM-CMSA memory)]
    G[ask_memory] --> H[0GM-2.0<br/>quotes past messages]
    F -.-> H
    H --> I[Dates checked<br/>against local index]
    I --> J[Dated verbatim quotes,<br/>plus buffer matches]
```

0GM-2.0 writes memory when its server compacts a conversation, which it does by itself once a plain multi-turn call
passes 65,536 tokens. So the package buffers sessions locally and sends them as one call at about 70K estimated tokens, and
the server archives them into the wallet. Each message carries its session date, and every quote that comes back is
checked against a local index of what was saved, so a wrong date is corrected and marked. Until a session is saved,
`ask_memory` also searches the local buffer and lists those matches separately. Calls to the memory tools, and answers
that only restate memory, are never saved back, so old answers do not echo into new ones.

## Tools and commands

| MCP tool | What it does | Cost and time |
|---|---|---|
| `ask_memory(query)` | Returns verbatim quotes of the relevant past messages with their session dates, or `NO RELEVANT MESSAGES`. It does not answer; the agent reasons over the quotes | A few cents a call (about $0.07 measured on 0GM-2.0). Seconds on a small wallet, up to about 100 s on one of millions of tokens |
| `save_session(transcript_path)` | Redacts and buffers a transcript; saves to the wallet once the buffer is big enough. Re-saving adds only new messages | Free while buffering; see [Cost](#cost) |
| `memory_status()` | What is buffered and saved, memories used of 64, last save, spend | Free, no network |

| Command | What it does |
|---|---|
| `0gm-memory serve` | Run the MCP server on stdio |
| `0gm-memory ask "question"` | The same as `ask_memory` |
| `0gm-memory save FILE...` | Save transcripts (Claude Code, Codex, an OpenCode export, or JSONL of role and content) |
| `0gm-memory save --claude` / `--codex` / `--opencode` | Save this folder's past sessions from that agent. `--since YYYY-MM-DD` and `--session ID` narrow it; messages already saved are skipped |
| `0gm-memory status [--json]` | Buffer, saves, spend and the hook log path. No network |
| `0gm-memory hook` | For agent hooks: reads the hook JSON and saves in the background |
| `0gm-memory forget --local` / `--wallet` | Delete this machine's buffer and logs, or every memory in the wallet (asks for the wallet id) |

Exit codes: 0 ok, 1 bad input, 2 configuration (for example no key), 3 the router call failed (`save` keeps the buffer
and sends it again next time). With no network a call fails at once; rate limits and server errors are retried with
backoff.

## Recommended project rule

Agents call `ask_memory` when you mention past work, but not by themselves before a routine change such as "move the
port back to 8080". To make them check first, add this line to the project's `CLAUDE.md` or `AGENTS.md`:

```text
Before changing configuration, dependencies or an earlier decision, call ask_memory to check for a past decision; if it conflicts, stop and ask the user before changing it.
```

## Configuration

| Variable | Default | What it does |
|---|---|---|
| `ZGM_MEMORY_KEY` | | API key for this project's wallet |
| `ZGM_MEMORY_KEY_SERVICE` | | macOS: name of a keychain item holding the key |
| `ZG_API_KEY` | | Used if nothing above is set |
| `ZGM_MEMORY_DISABLE` | | `1` turns everything off: nothing is read or sent |
| `ZGM_MEMORY_HOME` | platform data folder | Local state: `~/Library/Application Support/0gm-memory`, `~/.local/share/0gm-memory` or `%LOCALAPPDATA%\0gm-memory` |
| `ZGM_MEMORY_FLUSH_TOKENS` | `70000` | Estimated tokens per save. Larger saves mean fewer, bigger saves |
| `ZGM_MEMORY_ASK_TIMEOUT`, `ZGM_MEMORY_SAVE_TIMEOUT` | `300`, `600` | Seconds per call |
| `ZGM_MEMORY_MAX_RETRIES` | `4` | Retries on 429 and 5xx, with backoff |
| `ZGM_MEMORY_TOOL_RESULT_CHARS` | `1500` | How much of each tool result is kept |
| `ZGM_MEMORY_NO_ENTROPY_REDACTION` | | `1` keeps random-looking strings that match no known secret format |
| `OPENCODE_DB` | `$XDG_DATA_HOME/opencode/opencode.db` | OpenCode's database, read by `save --opencode` and the plugin |
| `ZGM_MEMORY_OPENCODE_BIN` | `opencode` | OpenCode command, used only if its database cannot be read |
| `ZGM_MEMORY_BASE_URL` | `https://router-api.0g.ai/v1` | Router |
| `ZGM_MEMORY_MODEL` | `0gm-2.0` | Model id; the default is one constant (`DEFAULT_MODEL` in `src/zgm_memory/config.py`) |

The key is read in this order: `ZGM_MEMORY_KEY`, `ZGM_MEMORY_KEY_SERVICE`, a key entered in plugin settings,
`ZG_API_KEY`. It is only sent in the `Authorization` header and never logged; local state is filed under a one-way hash
of it.

**Upgrading from `0gm-msa-memory`.** The package was renamed. The old names keep working for now:

- Every `ZGM_MEMORY_*` variable falls back to its old name `MSA_MEMORY_*` (and `ZGM_MEMORY_KEY_SERVICE` to
  `MSA_KEY_SERVICE`); when both are set, the new name wins.
- On first run, local state in the old `0gm-msa-memory` data folder is copied to the new `0gm-memory` folder. The old
  folder is left untouched; delete it once you are happy.
- A `.msamemoryignore` file is still read; `.0gmmemoryignore` wins when both are present.
- `0gm-msa-memory` still runs as a deprecated alias of the `0gm-memory` command.

## Results

Measured on test wallets. Small samples and single runs, so read them as indications, not rankings. Methods and raw
results: [FINDINGS](https://github.com/0gfoundation/0gm-msa-demos/blob/main/docs/FINDINGS.md) and
[`eval/beam/`](https://github.com/0gfoundation/0gm-msa-demos/tree/main/eval/beam) in the demos repository.

**BEAM at 10M tokens** (Mem0's long-conversation benchmark): 40 questions, one run, questions answered correctly.

```mermaid
xychart-beta
    title "BEAM-10M: correct answers out of 40 (one run)"
    x-axis ["0GM-2.0 (preview)", "This plug-in", "GPT-5.1 + RAG", "Mem0 + GPT-5", "Perfect context"]
    y-axis "Correct (of 40)" 0 --> 40
    bar [22, 22, 21, 19, 34]
```

| Setup | Correct of 40 |
|---|---|
| 0GM-2.0 (preview), a plain question to the wallet | 22 |
| This plug-in (quoting lookup, v2) | 22 |
| GPT-5.1 with embedding retrieval (RAG) | 21 |
| Mem0 with GPT-5 | 19 |
| Ceiling: the right evidence placed in the prompt | 34 |

The first four are within noise of each other on 40 questions, and the gap to the ceiling shows that finding the
evidence is still the hard part. What this package offers is not a higher score but nothing to host, and answers that
come back as dated quotes.

**Real agent sessions** (24 Nov 2026, invented project, test wallet):

| Check | Result |
|---|---|
| Fresh Claude Code session, memory tools only, three questions about past decisions | 3 of 3 answered with verbatim, correctly dated quotes (34 s and $0.022 for the lookup) |
| Fresh Codex, OpenCode and Hermes sessions | Each recalled decisions from earlier sessions, from the wallet, with dates |
| Synthetic history spanning 17 Sep to 24 Nov | Quotes carried the right session dates |
| Fake keys, passwords and `.env` contents planted in sessions | 0 found on disk or in any request |
| Time | A lookup took 6 to 45 s on a small wallet; a save took 18 to 31 s |

## Privacy and data

- **What is sent**, after redaction: user and assistant message text, tool calls (cut to 400 characters), tool results
  (cut to 1,500) and your `ask_memory` questions. They go to 0G's router (`router-api.0g.ai`) and are stored in your
  wallet. Nothing is sent while sessions are only buffered.
- **What is never sent:** thinking blocks, subagent side chains, system reminders, calls to the memory tools, and the
  output of tool calls that touched `.env`, key or credential files.
- **Redaction** runs on your machine before anything is buffered, and again on the exact request body: API keys and
  tokens in known formats, private keys, secret-looking assignments, passwords said in a sentence and random-looking
  strings. It is a strong filter, not a guarantee; see [SECURITY.md](https://github.com/0gfoundation/0gm-memory/blob/main/SECURITY.md).
- **Opt out, or add rules**, per project with a `.0gmmemoryignore` file in the project or any parent folder:

  ```text
  *                # save nothing from this project
  Acme Corp        # or: redact this string everywhere
  re:CUST-\d{6}    # and this pattern
  path:legal/**    # and drop tool output for these files
  ```

Retention and how to wipe everything: [PRIVACY.md](https://github.com/0gfoundation/0gm-memory/blob/main/PRIVACY.md).

## Cost

Measured on 4 Dec 2026 with 0GM-2.0: a save (about 70K tokens of sessions) cost $0.28, and a question $0.07.

| A typical week | Saves | Questions | About |
|---|---|---|---|
| Busy: 1M tokens of sessions | 14 | 30 | $6 |
| Light: 300K tokens of sessions | 4 | 10 | $2 |

0GM-2.0 pricing on 0G Private Computer: memory writes are $3.00 per million newly written tokens, including the first
30 days of storage; stored memory renews at $3.00 per million tokens per 30 days, and using it resets the 30 days.
Unused memory expires after 30 days. Check [pc.0g.ai](https://pc.0g.ai) for current prices. `memory_status` shows what has
been spent from its state folder (Hermes keeps its own folder, so it reports its own share).

## Limits

- **A save needs about 70K tokens.** The most recent sessions wait in the local buffer until there is enough;
  `ask_memory` still finds them there.
- **Up to 120M tokens of memory per wallet.** If a save is ever refused with `429 "Automatic session quota reached"`,
  raise `ZGM_MEMORY_FLUSH_TOKENS` so each save carries more, and open an issue.
- **Memories expire after 30 days without use**, under 0GM-2.0's memory pricing. See [PRIVACY.md](https://github.com/0gfoundation/0gm-memory/blob/main/PRIVACY.md).
- **Slow on big wallets.** Seconds on a small wallet; 55 to 101 s per lookup on wallets of 1M to 12M tokens.
- **Recall is not complete.** Ask one specific question per call: a lookup that bundled five questions missed a fact
  that a specific question then found.
- **No per-fact delete.** You can wipe a whole wallet, not one session.
- **One wallet per project.** Everything in a wallet can turn up in any answer (see the [FAQ](#faq)).
- **Backfills cost per save.** `save --claude` on a long history saves once per ~70K tokens.

## FAQ

**What is a wallet?** The account that holds the memory. You create it with an API key from pc.0g.ai and pay for
use with credit on that key; nothing else is needed.

**Why one wallet per project?** With two long conversations in one wallet, 7 of 20 answers about the first brought in
material from the second; loaded one per wallet, the same 40 questions scored 0.664 against 0.593. The package keeps a
separate buffer per key, but only separate wallets keep memories apart.

**Is this RAG?** No index is built: the model reads its own memory. On the benchmark above it scored about the same as
embedding retrieval. The trade is different: nothing to host, and evidence comes back as dated quotes.

**What happens if a save fails?** Nothing is lost: the buffer is kept and sent again with the next save. `save` exits
with 3, and `memory_status` counts the failure.

**Can I delete memories?** `0gm-memory forget --wallet` deletes every memory in the wallet (it asks you to confirm
with the wallet id). `forget --local` deletes this machine's buffer and logs.

**Does it work offline?** Buffering does; saving and asking need the router.

**Will it slow my agent down?** The save hooks return in about 0.1 s and do the work in the background. A lookup takes
seconds, and runs only when the agent asks.

## Contributing

From source:

```sh
git clone https://github.com/0gfoundation/0gm-memory && cd 0gm-memory
uv run --extra dev pytest          # unit tests, no network
uv run --extra dev ruff check .
ZGM_MEMORY_KEY=... uv run python scripts/live_e2e.py all --out e2e-results   # live, costs under $0.50
```

Issues and pull requests are welcome; see [CONTRIBUTING.md](https://github.com/0gfoundation/0gm-memory/blob/main/CONTRIBUTING.md) and the
[Code of Conduct](https://github.com/0gfoundation/0gm-memory/blob/main/CODE_OF_CONDUCT.md). Security reports: [SECURITY.md](https://github.com/0gfoundation/0gm-memory/blob/main/SECURITY.md).

## Licence

[MIT](https://github.com/0gfoundation/0gm-memory/blob/main/LICENSE)

TDQS

A4.1/5.0

Scored across 3 tools

Disambiguation5/5

The three tools map cleanly onto read (ask_memory), write (save_session), and inspect (memory_status) operations with no functional overlap. The descriptions explicitly differentiate them, including noting that ask_memory does not reason and that memory_status is free and local.

Naming Consistency4/5

ask_memory and save_session follow a consistent verb_noun pattern, but memory_status breaks it with a noun_noun form. It is still readable and clearly part of the same family, so only a minor deviation.

Tool Count4/5

Three tools is on the lean side but covers the core memory lifecycle (search, ingest, status) without redundancy. Each tool earns its place; nothing feels like filler.

Completeness3/5

Search, save, and status cover the main loop, but there is no way to delete or forget memories, list saved sessions/entries, or force a flush of the buffer to wallet memory. These are notable gaps that could cause dead ends for agents managing memory hygiene.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues