# Memory and files

> Give an agent a memory it keeps across conversations, share memories between agents, and give it files it works on in each one.

An agent remembers in two ways. Its **memory** is a set of named notes it keeps across every conversation: your refund policy, how a customer likes to be addressed, what yesterday's run already covered. Its **workspace** holds the files it works on in one conversation: a report it wrote, a spreadsheet you gave it. Your code reads and writes both.

## How memory works

Memory is a set of entries, each with a `name`, a one-line `description` and Markdown `content`. At the start of every turn the agent sees each entry's name and description; it reads a body when it needs one, and saves what it learns as it goes, with memory tools it always has (`memory__search`, `memory__read`, `memory__save` and `memory__delete`; your [policy](https://www.noclick.com/docs/guide/policy.md) calls them `memory.search` and so on). So the description is what makes an entry useful: it's how the agent knows the entry is there and what it's for.

Ask the agent to remember something, a code, a number or a name, and it saves it word for word before it answers. Any other secret, like a key a tool prints, it doesn't save.

## Seed what it should know

```python
import noclick

support = noclick.Agent.from_name("support")
support.memory.save(
    "refunds",
    "Refund within 30 days of purchase, to the original card. Gift cards are never refunded.",
    description="Our refund rules",
)
```

```ts
import { Agent } from "noclick";

const support = Agent.fromName("support");
await support.memory.save(
  "refunds",
  "Refund within 30 days of purchase, to the original card. Gift cards are never refunded.",
  { description: "Our refund rules" },
);
```

`save` creates the entry, or replaces the one with that name. A name is 1 to 64 lowercase letters, digits and `. _ -`; a description is at most 200 characters, and content 1 to 20,000. A new entry needs a description; saving an existing one without `description` keeps the one it has.

## Read what it remembers

```python
support = noclick.Agent.from_name("support")
for entry in support.memory.list():
    print(entry.name, entry.description, entry.updated_by, entry.updated_at)

refunds = support.memory.read("refunds")
print(refunds.version, refunds.content)

for entry in support.memory.search("refund policy"):
    print(entry.name, entry.content)
```

```ts
import { Agent } from "noclick";

const support = Agent.fromName("support");
for (const entry of await support.memory.list()) {
  console.log(entry.name, entry.description, entry.updatedBy, entry.updatedAt);
}

const refunds = await support.memory.read("refunds");
console.log(refunds.version, refunds.content);

for (const entry of await support.memory.search("refund policy")) {
  console.log(entry.name, entry.content);
}
```

`list` gives each entry without its content; `read` loads one in full. `search` looks where the agent looks and finds entries by their words: an entry matches when its name, description or content holds any word of the query (a word also finds the longer words it starts, so `refund` finds `refunds`), or its name or description holds the whole query. The more words an entry holds, the higher it ranks, and the name and description count more than the content. It returns the best 10 (`limit=` up to 100), with content. `updated_by` says who last changed an entry: `agent`, `api` or `console`.

## Correct an entry safely

The agent may change an entry between your read and your write. Pass the `version` you read and the save only lands if nothing changed since:

```python
support = noclick.Agent.from_name("support")
entry = support.memory.read("refunds")
try:
    support.memory.save("refunds", entry.content + "\nNo refunds on custom orders.", version=entry.version)
except noclick.Conflict:
    print("The agent changed it meanwhile; read it again.")
```

```ts
import { Agent, Conflict } from "noclick";

const support = Agent.fromName("support");
const entry = await support.memory.read("refunds");
try {
  await support.memory.save("refunds", `${entry.content}\nNo refunds on custom orders.`, { version: entry.version });
} catch (err) {
  if (!(err instanceof Conflict)) throw err;
  console.log("The agent changed it meanwhile; read it again.");
}
```

Without `version`, a save replaces whatever is there. `delete` takes a `version` the same way:

```python
support = noclick.Agent.from_name("support")
entry = support.memory.read("old-pricing")
support.memory.delete("old-pricing", version=entry.version)
```

```ts
import { Agent } from "noclick";

const support = Agent.fromName("support");
const entry = await support.memory.read("old-pricing");
await support.memory.delete("old-pricing", { version: entry.version });
```

## Undo a change

Every save keeps the version it replaces, so a change the agent made, or yours, can be undone. `versions` lists an entry's versions newest first: the one it holds now (`current`), then the earlier ones, each with who wrote it and when (`saved_at`). `get_version` reads one in full, and `restore` puts it back as a new version holding that one's content and description, so the version it replaces is kept too:

```python
support = noclick.Agent.from_name("support")
for version in support.memory.versions("refunds"):
    print(version.version, version.current, version.written_by.type, version.saved_at)
print(support.memory.get_version("refunds", 1).content)
support.memory.restore("refunds", 1)
```

```ts
import { Agent } from "noclick";

const support = Agent.fromName("support");
for (const version of await support.memory.versions("refunds")) {
  console.log(version.version, version.current, version.writtenBy.type, version.savedAt);
}
console.log((await support.memory.getVersion("refunds", 1)).content);
await support.memory.restore("refunds", 1);
```

An entry keeps its last 50 versions, the current one included; older ones go as new ones are saved. Each layer keeps its own history, a conversation's checkpoint keeps none, and forgetting an entry forgets its history with it. A named memory's entries keep theirs the same way (`noclick.Memory`'s `versions`, `get_version` and `restore`).

## Whose memory it is

A [user](https://www.noclick.com/docs/guide/users.md)'s own agent has one memory, the user's. An account agent, shared by every user who messages it, keeps two layers:

- **The account's.** Entries saved outside `as_user`. Every user's conversations read them; besides your code and the console, only the agent's conversations with your account itself (messages your code sends outside `as_user`) change them.
- **Each user's.** What the agent learns while talking to a user, and what you save for that user. No other user's conversations see it. In a user's conversation the agent reads the account's entries too, and a save of the same name makes the user's own copy.

```python
support = noclick.Agent.from_name("support")
support.memory.save("tone", "Warm, brief, no jargon.", description="How we write")   # every user
support.memory.save("plan", "Ana is on the Team plan, billed yearly.", description="Her plan", user="user_42")
print([entry.name for entry in support.memory.list(user="user_42")])   # ['plan']: one layer at a time
```

```ts
import { Agent } from "noclick";

const support = Agent.fromName("support");
await support.memory.save("tone", "Warm, brief, no jargon.", { description: "How we write" });   // every user
await support.memory.save("plan", "Ana is on the Team plan, billed yearly.", { description: "Her plan", user: "user_42" });
console.log((await support.memory.list({ user: "user_42" })).map((entry) => entry.name));   // ["plan"]: one layer at a time
```

`list`, `read`, `save` and `delete` act on one layer: the account's outside `as_user`, the user's inside. `search` looks where the agent looks, in the user's entries and the account's; each result's `user_id` says whose it is (`None` for the account's). A layer's entries go when the agent is deleted.

In a [channel](https://www.noclick.com/docs/guide/channels.md) of your account's own, a turn handed only words from outside your account's people (an app's event, an outside agent's post, a workspace guest's, or a post made in a person's or an agent's name by code) reads the agent's memory and doesn't write it. Where the words came from follows them: what an agent posts or starts on such a turn counts as outside words too, and so does the message of a schedule set by something whose own post would. A turn that also carries your own post, or a workspace admin's or member's, writes as usual, and so does one started by a schedule you set or by your other agents on their own. When the agent answers someone outside your account, its memory is read-only: an [inbox](https://www.noclick.com/docs/guide/inboxes.md)'s sender, a caller on a [phone number](https://www.noclick.com/docs/guide/phone-numbers.md) or a visitor to its [share link](https://www.noclick.com/docs/guide/sharing.md) can't make it write anything every user would then read. A [test run](https://www.noclick.com/docs/guide/testing.md) reads memory and never writes it.

## Share a memory between agents

A **memory** is an object of its own, with a name, that any number of agents can read: what every agent of your company should know, or what one agent learned that another needs. Attach it with `memories=`, and the agent reads it besides its own memory:

```python
company = noclick.Memory.from_name("company", create_if_missing=True)
company.save("tone", "Warm, brief, no jargon.", description="How we write")

support = noclick.Agent.from_name("support", create_if_missing=True, instructions="Answer billing questions.",
                                  memories=[company])
scribe = noclick.Agent.from_name("scribe", create_if_missing=True, instructions="Keep the company's notes.",
                                 memories=[company.writable()])
```

```ts
import { Agent, Memory } from "noclick";

const company = Memory.fromName("company", { createIfMissing: true });
await company.save("tone", "Warm, brief, no jargon.", { description: "How we write" });

const support = Agent.fromName("support", {
  createIfMissing: true, instructions: "Answer billing questions.", memories: [company],
});
const scribe = Agent.fromName("scribe", {
  createIfMissing: true, instructions: "Keep the company's notes.", memories: [company.writable()],
});
```

An attached memory is read-only to the agent unless it's attached with `writable()`; its own memory, `agent.memory`, it always writes. Every turn starts with the name and description of each entry it can read, its own memory's first and then each attached one under its name. Its memory tools take `memory="company"` to read or write a particular one; without it, `memory__save` and `memory__delete` act on its own memory, `memory__read` looks in its own memory first and then in the attached ones (naming them when two hold the same entry), and `memory__search` looks in all of them.

Every agent's own memory is a memory too: `agent.memory_id` is its id, and `memories=[other.memory]` lets an agent read what another one remembers. It goes when its agent is deleted for good; a named memory outlives the agents that attach it.

A memory belongs to your account or to one user, as a volume does. The account's keeps its own entries and each user's, as an account agent's own memory does; a user's keeps one layer, its user's. An agent attaches memories of its own scope, and a user's agent the account's too (read-only: only a call acting for a memory's own scope attaches it with `writable()`), never another user's; a [browser session](https://www.noclick.com/docs/guide/sessions.md) never attaches the account's. Up to 10 memories besides its own.

```python
for memory in noclick.Memory.list():
    print(memory.name, memory.entry_count, memory.attached_to)

company.entries()                                  # one layer, as agent.memory.list() is
company.read("tone")
company.forget("old-pricing")
noclick.Memory.list(agent=noclick.Agent.from_name("support"))   # its own memory first
company.delete()                                   # to the trash, off every agent until it's restored
```

```ts
import { Agent, Memory } from "noclick";

for (const memory of await Memory.list()) {
  console.log(memory.name, memory.entryCount, memory.attachedTo);
}

await company.entries();                           // one layer, as agent.memory.list() is
await company.read("tone");
await company.forget("old-pricing");
await Memory.list({ agent: Agent.fromName("support") });          // its own memory first
await company.delete();                            // to the trash, off every agent until it's restored
```

A memory's entries are read and written exactly as an agent's own memory's are: the same names, descriptions, versions and layers (`entries`, `search`, `read`, `save` and `forget`, with `user=` for a user's layer). Each entry's `written_by` says who last wrote it: an agent's turn (`type` `agent`, with the agent and the conversation it learned it in), a person in the app (`console`) or your code (`api`). Deleting a memory moves it to the [trash](https://www.noclick.com/docs/guide/undo.md) with its entries and takes it off every agent that attaches it; restoring it puts it back on them, each as it had it.

What an agent learns goes to its NoClick memory on every harness: a coding harness's own memory (Claude Code's auto-memory, Codex's memories, Hermes's and OpenClaw's) is off, so what it learns follows the agent when you change its harness, and other agents can read it.

## Long conversations

A conversation that runs for weeks doesn't run out of room. Once an `sdk` agent's conversation grows past about 32,000 tokens, its turn folds the older part into a **checkpoint**: one entry of the agent's memory, named `context.` and a hash, in that conversation's layer, with `conversation` set to its key. Each later turn reads the checkpoint and then the recent messages in full, and the conversation you read back stays whole. The checkpoint keeps word for word anything the agent was asked to remember in that conversation, until it's asked to forget it, and leaves out any other secret it saw.

```python
support = noclick.Agent.from_name("support")
for entry in support.memory.list(user="user_42"):
    if entry.conversation == "ticket-1841":
        print(support.memory.read(entry.name).content)   # what the agent carries forward
```

```ts
import { Agent } from "noclick";

const support = Agent.fromName("support");
for (const entry of await support.memory.list({ user: "user_42" })) {
  if (entry.conversation === "ticket-1841") {
    console.log((await support.memory.read(entry.name)).content);   // what the agent carries forward
  }
}
```

A checkpoint is yours to read and correct like any entry: an edit wins over the next summary, and deleting it forgets that part of the conversation for good, since it's never rebuilt from the history. The agent sees it only in its own conversation, never in its list of entries, `memory__search` or its memory tools, and it doesn't count toward a layer's entries. Names starting with `context.` are kept for checkpoints. A conversation whose memory is read-only keeps none. Summaries run on NoClick's default model and are billed with the turn. Agents on a [coding harness](https://www.noclick.com/docs/guide/agents.md#harnesses) compact their own conversations.

## Work with an agent's files

An agent has one workspace, a folder it sees as `/workspace` in every turn, whoever it answers. What it writes with its shell or its tools lands there, and what you put there is waiting for it. Clearing or resuming its session leaves it as it is (a [workspace of channels](https://www.noclick.com/docs/guide/workspaces.md) is something else):

```python
analyst = noclick.Agent.from_name("analyst")
workspace = analyst.workspace()
workspace.write("sales.csv", "region,total\nnorth,1200\nsouth,950\n")
reply = analyst.message("Chart sales.csv by region and save it as chart.png.")

for file in workspace.files():
    print(file.path, file.size_bytes, file.modified_at, file.url)
```

```ts
import { Agent } from "noclick";

const analyst = Agent.fromName("analyst");
const workspace = analyst.workspace();
await workspace.write("sales.csv", "region,total\nnorth,1200\nsouth,950\n");
const reply = await analyst.message("Chart sales.csv by region and save it as chart.png.");

for (const file of await workspace.files()) {
  console.log(file.path, file.sizeBytes, file.modifiedAt, file.url);
}
```

`workspace()` sends nothing. An account agent's user has a workspace of their own (`user=`); a `conversation` is accepted and ignored. `files` lists what's there, each with a download `url` that works without a key for about an hour. `read` returns a file's bytes, and `url` makes a link for your user to click:

```python
analyst = noclick.Agent.from_name("analyst")
workspace = analyst.workspace()
chart = workspace.read("chart.png")
link = workspace.url("chart.png", expires_in_s=600)
```

```ts
import { Agent } from "noclick";

const analyst = Agent.fromName("analyst");
const workspace = analyst.workspace();
const chart = await workspace.read("chart.png");
const link = await workspace.url("chart.png", { expiresInS: 600 });
```

A link lasts an hour unless you say otherwise, and a day at most.

An `sdk` agent's shell starts each turn from the workspace as it stands, so a file you write reaches its next turn. An agent on a [coding harness](https://www.noclick.com/docs/guide/agents.md#harnesses) keeps its sandbox up between turns and mounted the workspace when the sandbox started, so it sees your file once the sandbox starts again, a few minutes after its last turn: write before its first turn, or while it's idle. A [volume](https://www.noclick.com/docs/guide/volumes.md) is the place for files several agents share.

## Limits and billing

- A memory holds up to 500 entries per layer, and an agent attaches up to 10 besides its own. Memory is free to keep; the agent's own reads and saves are part of its turns.
- Each turn's list of names and descriptions takes up to about 6,000 characters of the turn, for all its memories together: its own first, the account's entries after the user's and newest first in each; past that the agent finds the rest with `memory__search`.
- A file you write to a workspace is at most 100 MB. A workspace lasts as long as its agent, and goes when the agent is deleted for good. Workspaces cost nothing to keep.

## Errors

| Error | When |
| --- | --- |
| `noclick.NotFound` | No entry by that name in that layer, a version the entry no longer keeps, no file at that path, or no memory by that name or id. |
| `noclick.Conflict` | The entry isn't at the `version` you passed any more (`details["current_version"]` is where it is, absent once it was deleted); a memory name that's taken (`existing_id`); deleting an agent's own memory. |
| `noclick.InvalidRequest` | A name that isn't allowed, a new entry without a description, a description over 200 characters, empty content or content over 20,000 characters, an agent's own memory among its `memories`, or one memory listed twice. |
| `noclick.PermissionDenied` | A user's memory attached to an agent outside its user, a memory attached `writable()` by a call that doesn't act for its scope, or the account's memory from a browser session. |
| `noclick.QuotaExceeded` | The layer already has 500 entries, or more than 10 memories in `memories`. |

Every method is in the reference: [`AgentMemory`](https://www.noclick.com/docs/reference/python/Agent.md#AgentMemory) with [`save`](https://www.noclick.com/docs/reference/python/Agent.md#AgentMemory.save) and [`search`](https://www.noclick.com/docs/reference/python/Agent.md#AgentMemory.search), [`Memory`](https://www.noclick.com/docs/reference/python/Memory.md) with [`writable`](https://www.noclick.com/docs/reference/python/Memory.md#Memory.writable), [`MemoryEntry`](https://www.noclick.com/docs/reference/python/Agent.md#MemoryEntry), and [`AgentWorkspace`](https://www.noclick.com/docs/reference/python/Agent.md#AgentWorkspace) with [`files`](https://www.noclick.com/docs/reference/python/Agent.md#AgentWorkspace.files).
