---
title: Self-hosted agent memory on ClickHouse
description: Evaluate self-hosted agent memory for coding agents. Verify local semantic recall and query decision history and writer provenance with SQL.
seo:
  title: Self-hosted agent memory with SQL
lastModified: '2026-11-25T15:15:36.000Z'
---

Loom runs on your own infrastructure, with Docker or on Kubernetes with its Helm chart, and
stores agent memory, documents, conversations and code indexes as tables in ClickHouse. A
deployment can choose Postgres at first boot for live memory and knowledge state. Loom's
stores and model providers are separate deployment choices. What your agents remember
is queryable with SQL.

## What you get

- **Docker or Kubernetes.** The one-line installer starts the local stack; the Helm chart
  runs N identical replicas behind one URL. [Deployment](/docs/operations/deployment) ·
  [Kubernetes](/docs/operations/kubernetes) · [Running N replicas](/docs/operations/replicas)
- **ClickHouse OSS or ClickHouse Cloud.** [Choosing the ClickHouse](/docs/operations/deployment#choosing-the-clickhouse) ·
  [ClickHouse Cloud and Managed Postgres](/docs/operations/clickhouse-cloud)
- **One store for vectors, text and audit.** Under the default ClickHouse authority,
  vector, lexical and graph search, filters, audit and erasure are SQL in one database.
  [Storage authority](/docs/architecture/storage-authority)
- **Multi-user.** Orgs, namespaces, roles, tokens and single sign-on.
  [Identities and namespaces](/docs/concepts/identities-and-namespaces) ·
  [Single sign-on](/docs/operations/sso)
- **Local models.** The released stack embeds on CPU with a bundled local model, and
  the embedding provider accepts an OpenAI-compatible endpoint you run.
  [Models and API keys](/docs/getting-started/models-and-keys)

## Choose self-hosted memory for coding agents

For a team deployment, record where each component sends data before giving it repository
or conversation access. Review every destination against your network and data policies.

| Component      | What to verify                                                                                                                                                                                                                                                                             |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Storage        | Record who operates the databases, the document bucket and the backup destinations. Check the [storage authority](/docs/architecture/storage-authority) and [backup and recovery plan](/docs/operations/backup-and-recovery).                                                              |
| Loom inference | The released stack includes a local CPU embedder. For extraction, summaries and answers, check the selected [LLM and any per-job or fallback models](/docs/getting-started/models-and-keys#keep-every-llm-job-on-your-chosen-provider).                                                    |
| Coding client  | Check the client’s model provider and capture settings. The client can pass retrieved memories to its model. Use the [coding-agent connection guide](/docs/getting-started/connect-a-coding-agent) to configure capture for each client.                                                   |
| Telemetry      | Check the [trace exporter](/docs/features/observability/telemetry#exporters) and any trace sinks. Loom sends aggregate self-hosted adoption reports by default. You can [disable those reports on each server and worker](/docs/features/observability/telemetry#can-i-disable-telemetry). |

### Verify semantic recall on a private network

Start a disposable deployment from the [released Docker stack](/docs/getting-started/quickstart#fast-path-released-image-no-clone).
Pull the images before restricting runtime traffic. Keep the bundled local embedder,
use a ClickHouse the server can reach on the private network, and restrict runtime
egress to the destinations you approved.

1. [Check `/v1/health`](/docs/getting-started/quickstart#check-the-stack). With the bundled
   embedder, `fake_embedding` should be `false`. Record the model and provider settings
   with the health response.
2. [Save a test fact over HTTP](/docs/getting-started/quickstart#4-first-memory-over-http),
   such as “Production deployments are allowed only on Tuesdays and Thursdays.”
   Check the exact key with `memory.get`. Ask “What days can we deploy changes to
   production?” with `memory.search` and `search_mode="semantic"`. Verify that search
   returns the saved key while the network restriction is in effect.
3. If the team needs extraction or generated answers, configure its approved LLM and
   run those examples separately. Check the destinations observed at runtime for both
   Loom and the coding client.

## Reuse SQL lessons across agent sessions

Give the agent two connections. Use the [ClickHouse MCP server](https://github.com/ClickHouse/mcp-clickhouse)
with a read-only ClickHouse user to inspect current tables and run queries. Use
[Loom over MCP](/docs/features/integrations/mcp-clients) to save and recall metric
definitions and decisions. Grant database access and [memory namespace access](/docs/guides/tokens-and-grants)
separately. Share metric rules and schema references that every reader in the namespace
may see. Keep query results and credentials out of shared lessons.

In a disposable ClickHouse database, create a table and three test rows as an operator:

```sql
CREATE TABLE ingest_events (id UInt64, accepted UInt8)
ENGINE = MergeTree ORDER BY id;
INSERT INTO ingest_events VALUES (1, 1), (2, 0), (3, 1);
```

Set `CLICKHOUSE_DATABASE` on the read-only MCP connection to the test database.
Limit that database user's `SELECT` grant to `ingest_events`.

1. With the read-only client, use `run_query` to check `system.columns` for `ingest_events`
   in the test database. Run `SELECT count() FROM ingest_events WHERE accepted = 1`
   and expect `2`. Save the predicate and a reminder to recheck the schema under
   `example/sql/accepted-events` using `loom_memory_set` in a test namespace.
2. As the operator, run `ALTER TABLE ingest_events RENAME COLUMN accepted TO is_accepted`.
   Start a fresh session with a separate Loom token that can read the same namespace.
   Call `loom_memory_get` with that test `namespace` and `memory_key="example/sql/accepted-events"`.
   Check that it returns the writer and the saved predicate containing `accepted`.
3. Repeat the metadata lookup and verify `is_accepted` before applying the lesson.
   Run `SELECT count() FROM ingest_events WHERE is_accepted = 1` and expect `2`.
4. Repeat the same lookup arguments with a token that lacks access to the test namespace.
   Require an access refusal with no saved value. Remove the disposable table and lesson when done.

## Query agent memory with SQL

Use the [read-only ClickHouse views](/docs/reference/sql-surface) to audit saved facts,
trace corrections, or pull usage into a reporting job. The ClickHouse user provisioned
by `loom sql-access` reads only its granted orgs and namespaces. Start with
[`sql_v1_memories`](/docs/reference/sql-surface#sql_v1_memories) for current facts and
[`sql_v1_memory_history`](/docs/reference/sql-surface#sql_v1_memory_history) for versions.
Agents can keep using MCP or HTTP for semantic recall and writes.

### Audit a changed decision with SQL

Try this on a disposable single-user quickstart deployment with bundled ClickHouse and
the default `dev/local` scope and database `loom`.

1. Save `deploy_window` using the [first HTTP memory write](/docs/getting-started/quickstart#4-first-memory-over-http).
   Keep its `memory_id`. Pass that ID, `org: "dev"` and a replacement `value` to
   [`memory.update_by_id`](/docs/reference/operations/memory#memoryupdate_by_id).
   Check that the operation returns `updated: true`.
2. From the Loom container, create a SQL reader scoped to
   this namespace. The [SQL access requirements](/docs/reference/sql-surface#access) list
   the privileges the operator needs.

   ```bash
   docker compose --project-directory "$HOME/.loom" exec loom \
     loom sql-access grant --user loom_demo_audit --org dev --namespaces local
   ```

3. Open the bundled ClickHouse client as `loom_demo_audit` and enter the password printed
   by the grant.

   ```bash
   docker compose --project-directory "$HOME/.loom" exec clickhouse \
     clickhouse-client --user loom_demo_audit --ask-password
   ```

   Query the history of the decision written by the quickstart's `http` agent.

   ```sql
   SELECT version, actor, updated_at, value, value_withheld
   FROM loom.sql_v1_memory_history
   WHERE org = 'dev'
     AND namespace = 'local'
     AND agent = 'http'
     AND memory_key = 'deploy_window'
   ORDER BY version;
   ```

The history shows both versions, with each `value` returned as a JSON string. The
single-user example leaves `actor` blank. On an authenticated deployment, it identifies
the writer's token subject. The view returns NULL for a redacted `value` or any version
of a key ever labeled `high` and gives the reason in `value_withheld`. Under the
Postgres authority, the version appears once it has been
[published to ClickHouse](/docs/reference/sql-surface#freshness).

### Trace a saved memory to its agent run

Use the same SQL reader to inspect the requests recorded for the run attached to a
memory. For a test, [start a run](/docs/reference/operations/run#runbegin) and pass
its `run_id` to [`memory.set`](/docs/reference/operations/memory#memoryset) with
the same identity. Keep the `memory_id` from the write. Reopen the ClickHouse client
with the same reader and add `--param_memory_id=<uuid>`, using the returned ID.

```sql
SELECT ts, run_id, operation, success, request_id
FROM loom.sql_v1_agent_events
WHERE (org, namespace, agent, user_id, session_id, run_id) IN (
    SELECT org, namespace, agent, user_id, session_id, run_id
    FROM loom.sql_v1_memories
    WHERE org = 'dev'
      AND namespace = 'local'
      AND memory_id = {memory_id:UUID}
      AND run_id NOT IN ('', '-')
)
ORDER BY ts, request_id;
```

Read the timestamps, operation statuses and request IDs to see what the agent did
during the run. The query matches the saved memory's org, namespace, agent, user,
session and run ID. It returns no rows for a memory without a run or for a run
with no requests in the [ledger's retention window](/docs/reference/sql-surface#sql_v1_agent_events).
Allow the [ledger to flush](/docs/reference/sql-surface#freshness) before checking
requests that just finished.

### Count active memories by agent and source

Use the same SQL reader to see how many active, unexpired memories each agent has by
provenance class. This report returns counts without memory keys or values.

```sql
SELECT
    agent,
    source_class,
    count() AS active_memories,
    countIf(value_withheld != '') AS withheld_values
FROM loom.sql_v1_memories
WHERE org = 'dev'
  AND namespace = 'local'
  AND status = 'active'
  AND (expires_at IS NULL OR expires_at > now())
GROUP BY agent, source_class
ORDER BY agent, source_class;
```

`withheld_values` counts active rows whose values the view suppresses for sensitivity or
redaction. It is a count within `active_memories`. In a reporting job, provision a reader
for the namespaces the job covers. The view reads the
[granted scope](/docs/reference/sql-surface#what-a-read-costs) before applying the report's filters.

Run `loom sql-access revoke --user loom_demo_audit` in the Loom container when finished.

## Measured

A synthetic load of memories with 2.4 versions per key and 3072-dimension embeddings (two
vectors per row) took about 13.6 KB per key in ClickHouse, almost all of it the embedding
row; a smaller embedder takes less, and parts with few rows carry somewhat more per row
until they merge.
[Memory at scale](/docs/benchmarks/results#memory-at-scale)

## Start

[Quickstart](/docs/getting-started/quickstart) · [Prerequisites](/docs/getting-started/overview#prerequisites) ·
[Upgrades](/docs/operations/upgrades) · [Backup and recovery](/docs/operations/backup-and-recovery)
