> ## Documentation Index
> Fetch the complete documentation index at: https://docs.memcontext.in/llms.txt
> Use this file to discover all available pages before exploring further.

# How It Works

> How to save, retrieve, update, and isolate memory in your app.

MemContext gives your AI system a simple memory workflow:

1. Save durable context.
2. Search for relevant context before generating an answer.
3. Use scopes to isolate users or tenants.
4. Send feedback when results are helpful or wrong.

You do not need to manage deduplication, evolving facts, or retrieval quality
yourself.

## Save Memories

Use `POST /api/memories`, the TypeScript SDK, or MCP `save_memory`.

```typescript theme={null}
await client.save({
  content: "User prefers concise release notes.",
  category: "preference",
  scope: "user_123",
  project: "docs",
});
```

MemContext returns a status that tells you what happened:

| Status      | Meaning                                     |
| ----------- | ------------------------------------------- |
| `saved`     | A new memory was created                    |
| `updated`   | A newer memory replaced an older one        |
| `extended`  | The memory added detail to an existing fact |
| `duplicate` | The same memory already exists              |
| `accepted`  | A larger note was accepted for extraction   |

For large notes, use the returned `jobId` to track extraction.

## Search Memories

Search before your app generates a response or makes a decision.

```typescript theme={null}
const results = await client.search({
  query: "How should release notes be written?",
  scope: "user_123",
  project: "docs",
});
```

Each result includes memory content, optional category/project information, and
a relevance score.

## Use Scopes For Isolation

Use `scope` as the hard boundary for your own users, tenants, accounts, or
organizations.

```typescript theme={null}
const userMemory = client.withScope("user_123");
```

Only memories saved in that scope are searched when the same scope is provided.
If you omit `scope`, MemContext searches only the unscoped memory lane.

Use `project` for grouping inside a scope:

```typescript theme={null}
const projectMemory = userMemory.withProject("supportbot");
```

## Handle Changing Information

You can save updated information without manually deleting old memories.

Example:

1. Save: `User prefers long-form LinkedIn posts.`
2. Later save: `User now prefers short hook-first LinkedIn posts.`

MemContext can return the newer memory during search while preserving history
for audit or debugging through the history endpoint.

## Use Categories

Categories help you filter and reason about memory:

| Category     | Use for                      |
| ------------ | ---------------------------- |
| `preference` | Likes, dislikes, style rules |
| `fact`       | Objective information        |
| `decision`   | Choices that were made       |
| `context`    | General background or state  |

## Use Expiry For Temporary Facts

Set `validUntil` when a memory should stop being used after a specific time.

```typescript theme={null}
await client.save({
  content: "Sprint demo is on Friday.",
  category: "fact",
  validUntil: "2026-06-05T23:59:59Z",
});
```

Expired memories are excluded from search.

## Recommended App Flow

1. Search MemContext with the current user or workspace context.
2. Add the returned memories to your AI prompt.
3. Generate the answer in your own app.
4. Save any durable new preference, fact, or decision.
5. Send feedback when retrieved memories were useful or wrong.
