Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
feat(memory): add opt-in Apple-GPU (MPS) embedding runtime (#766)
Logan Kangc71592d4214a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f599ab3f9..7545d2140 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0* **proxy:** per-project savings breakdown on the dashboard for all wrapped agents — Claude Code, Codex, aider, Copilot, and Cursor ([#802](https://github.com/chopratejas/headroom/issues/802)). `headroom wrap claude`/`codex` tag requests with an `X-Headroom-Project` header (launch-directory name); `wrap aider`/`copilot`/`cursor` — whose clients cannot send custom headers — use a `/p/<name>` base-URL prefix the proxy strips. Savings are aggregated per project (persisted, schema v3 with transparent v2 migration), exposed as `savings.per_project` in `/stats` and `projects` in `/stats-history`, and shown in a Per-Project Savings dashboard table.+### Features++* **memory:** opt-in Apple-GPU (MPS) embedding offload via `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps`. When set (and Apple MPS is available), the memory embedder runs on the torch sentence-transformers backend on the Apple GPU instead of the default ONNX CPU embedder, freeing the CPU under load. If MPS or the dependencies are unavailable, Headroom logs a warning and uses the existing default embedder selection path (ONNX when available, then the pre-existing local fallback). MPS encode calls are serialized internally (torch-MPS is not thread-safe). Adds the new `[pytorch-mps]` extra (`pip install 'headroom-ai[pytorch-mps]'`). Default behavior is unchanged.+### Bug Fixes* **ccr:** make retrieval store TTL configurable with `HEADROOM_CCR_TTL_SECONDS`, expose the effective TTL in `/v1/retrieve/stats`, and distinguish expired retrievals from missing hashes.diff --git a/README.md b/README.mdindex 7e5a2c096..52b8e5962 100644--- a/README.md+++ b/README.md@@ -99,7 +99,7 @@ headroom proxy --port 8787 # drop-in proxy, zero code changesheadroom perf```-Granular extras: `[proxy]`, `[mcp]`, `[ml]`, `[code]`, `[memory]`, `[relevance]`, `[image]`, `[agno]`, `[langchain]`, `[evals]`. Requires **Python 3.10+**.+Granular extras: `[proxy]`, `[mcp]`, `[ml]`, `[code]`, `[memory]`, `[relevance]`, `[image]`, `[agno]`, `[langchain]`, `[evals]`, `[pytorch-mps]` (Apple-GPU memory-embedder offload — set `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps`). Requires **Python 3.10+**.## Proof@@ -228,7 +228,7 @@ npm install headroom-ai # TypeScript / Nodedocker pull ghcr.io/chopratejas/headroom:latest```-Granular extras: `[proxy]`, `[mcp]`, `[ml]` (Kompress-base), `[code]`, `[memory]`, `[relevance]`, `[image]`, `[agno]`, `[langchain]`, `[evals]`. Requires **Python 3.10+**.+Granular extras: `[proxy]`, `[mcp]`, `[ml]` (Kompress-base), `[code]`, `[memory]`, `[relevance]`, `[image]`, `[agno]`, `[langchain]`, `[evals]`, `[pytorch-mps]` (Apple-GPU memory-embedder offload — set `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps`). Requires **Python 3.10+**.Using `pipx`? Choose a supported interpreter explicitly:diff --git a/docs/content/docs/configuration.mdx b/docs/content/docs/configuration.mdxindex eabd41348..8b6751793 100644--- a/docs/content/docs/configuration.mdx+++ b/docs/content/docs/configuration.mdx@@ -234,6 +234,7 @@ headroom proxy --learn --min-evidence 3| `HEADROOM_PROXY_PYTHON_FORWARDER_MODE` | Python forwarder serialization mode. `byte_faithful` (default) forwards original request bytes verbatim when no transform mutated the body and re-serializes canonically only when needed — keeps Anthropic prompt-cache hit-rate intact. `legacy_json_kwarg` is an explicit operator opt-in for emergency rollback to the historical `httpx ... json=body` behavior. NOT a fallback — only flip on explicit operator decision. | `byte_faithful` || `HEADROOM_STRIP_INTERNAL_HEADERS` | Python proxy: whether to strip internal `x-headroom-*` request headers (e.g. `x-headroom-bypass`, `x-headroom-mode`, `x-headroom-user-id`, `x-headroom-stack`, `x-headroom-base-url`) before every upstream forwarder call (PR-A5, fixes P5-49). `enabled` (default) stops fingerprinting / leakage. `disabled` is an explicit operator opt-in for diagnostic shadow tracing — NOT a fallback. Inbound reads of these headers (bypass gating, memory user-id resolution) are unaffected because they read `request.headers` directly. | `enabled` || `HEADROOM_PROXY_STRIP_INTERNAL_HEADERS` | Rust proxy: same policy as `HEADROOM_STRIP_INTERNAL_HEADERS` but for the Rust transparent proxy. Stripping happens inside `build_forward_request_headers` so both HTTP and WebSocket upstream calls are gated by one flag. `enabled` default; `disabled` operator opt-in for diagnostic shadow tracing. Response-side `X-Headroom-*` injection (e.g. `x-headroom-tokens-saved`) is unrelated and stays. | `enabled` |+| `HEADROOM_EMBEDDER_RUNTIME` | Set to `pytorch_mps` to run the memory embedder via the torch sentence-transformers backend on the Apple GPU (MPS). Only engages when Apple MPS is actually available; otherwise it logs a warning and uses the existing default embedder selection path. `pytorch_mps` is the only accepted value. Requires the `[pytorch-mps]` extra. See [Memory](/docs/memory#embedding-runtime--gpu-offload-apple-silicon). | default embedder selection |### Filesystem Contractdiff --git a/docs/content/docs/installation.mdx b/docs/content/docs/installation.mdxindex a1da1995c..96a3ef7bd 100644--- a/docs/content/docs/installation.mdx+++ b/docs/content/docs/installation.mdx@@ -51,6 +51,7 @@ pip install "headroom-ai[all]"| `langchain` | LangChain `HeadroomChatModel` wrapper | `pip install "headroom-ai[langchain]"` || `agno` | Agno `HeadroomAgnoModel` wrapper | `pip install "headroom-ai[agno]"` || `evals` | Evaluation framework (GSM8K, SQuAD, BFCL benchmarks) | `pip install "headroom-ai[evals]"` |+| `pytorch-mps` | Apple-GPU (MPS) memory-embedder offload — **macOS only**, not in `[all]` (torch + sentence-transformers); opt in with `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps` | `pip install "headroom-ai[pytorch-mps]"` || `all` | Everything above | `pip install "headroom-ai[all]"` |You can combine extras:diff --git a/docs/content/docs/memory.mdx b/docs/content/docs/memory.mdxindex cd4468cc8..c0b919b67 100644--- a/docs/content/docs/memory.mdx+++ b/docs/content/docs/memory.mdx@@ -195,6 +195,36 @@ config = MemoryConfig()```+### Embedding Runtime / GPU Offload (Apple Silicon)++By default the proxy's memory embedder runs on the **ONNX CPU** backend -- fast+and dependency-light, but CPU-only. Under sustained load the embedding step can+saturate the CPU and make the proxy less responsive.++On Apple Silicon you can opt in to running the embedder on the **Apple GPU+(MPS)** instead, which offloads that work off the CPU and keeps the proxy+responsive. Install the extra and set the env var:++```bash+pip install "headroom-ai[pytorch-mps]" # also works as [pytorch_mps]+export HEADROOM_EMBEDDER_RUNTIME=pytorch_mps+```++When set, the embedder runs via the torch sentence-transformers backend on the+Apple GPU instead of the default ONNX CPU embedder. Notes:++- **Strictly opt-in.** `pytorch_mps` is the only accepted value; anything else+ (or unset) keeps the default ONNX CPU embedder. Default behavior is unchanged,+ and there is no CLI flag -- it is env-var only.+- **Auto-fallback.** It only activates when Apple MPS is actually available+ (Apple Silicon + torch). If MPS is unavailable or torch/sentence-transformers+ is not installed, it logs a warning and uses the existing default embedder+ selection path: ONNX when available, then the pre-existing local+ sentence-transformers fallback.+- **MPS serialization.** torch-MPS is not thread-safe, so the embedder+ serializes MPS encode calls internally via a single-worker executor. This is+ automatic -- there is nothing to configure.+### StorageStorage uses **SQLite** for CRUD and filtering, **HNSW** for vector similarity search, and **FTS5** for full-text keyword search. All embedded -- no external services required.diff --git a/headroom/memory/adapters/embedders.py b/headroom/memory/adapters/embedders.pyindex 73334cd57..dba028b11 100644--- a/headroom/memory/adapters/embedders.py+++ b/headroom/memory/adapters/embedders.py@@ -14,6 +14,7 @@ import asyncioimport loggingimport osimport warnings+from concurrent.futures import ThreadPoolExecutorfrom functools import cached_propertyfrom typing import TYPE_CHECKING, Any, cast@@ -123,6 +124,10 @@ class LocalEmbedder:self._device: str | None = Noneself._dimension: int | None = Noneself._lock = asyncio.Lock()+ # Dedicated single-worker executor, created only when the resolved device+ # is MPS (see _load_model). torch-MPS is not thread-safe, so every encode()+ # must run on one thread. Stays None for CPU/CUDA → default shared executor.+ self._executor: ThreadPoolExecutor | None = Nonedef _check_dependencies(self) -> None:"""Check that required dependencies are installed."""@@ -166,6 +171,13 @@ class LocalEmbedder:else:self._device = self._detect_device()+ # torch-MPS is not thread-safe: concurrent encode() calls from the default+ # multi-worker executor abort with "commit an already committed command+ # buffer" (verified). Funnel every encode through one worker thread when on+ # MPS so calls serialize; other devices keep the shared default executor.+ if self._device == "mps" and self._executor is None:+ self._executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="mps-embed")+# Use centralized registry for shared model instancesself._model = MLModelRegistry.get_sentence_transformer(self._model_name, self._device)@@ -199,7 +211,7 @@ class LocalEmbedder:model = self._model # Local reference for lambda closureloop = asyncio.get_event_loop()embedding = await loop.run_in_executor(- None,+ self._executor,lambda: model.encode(text, convert_to_numpy=True, normalize_embeddings=False),)@@ -242,7 +254,7 @@ class LocalEmbedder:model = self._model # Local reference for lambda closureloop = asyncio.get_event_loop()embeddings = await loop.run_in_executor(- None,+ self._executor,lambda: model.encode(non_empty_texts, convert_to_numpy=True, normalize_embeddings=False),@@ -276,9 +288,14 @@ class LocalEmbedder:return self.DEFAULT_MAX_TOKENSasync def close(self) -> None:- """Close resources (no-op for local embedder)."""- # LocalEmbedder doesn't hold persistent connections- pass+ """Close resources: shut down the MPS serialization executor and drop the+ cached model reference so a later embed() fully re-initializes (and+ re-creates the serialized executor) instead of encoding on a torn-down pool.+ """+ if self._executor is not None:+ self._executor.shutdown(wait=False)+ self._executor = None+ self._model = None# =============================================================================diff --git a/headroom/proxy/memory_handler.py b/headroom/proxy/memory_handler.pyindex 37f6d5756..033933d41 100644--- a/headroom/proxy/memory_handler.py+++ b/headroom/proxy/memory_handler.py@@ -29,6 +29,7 @@ import enumimport inspectimport jsonimport logging+import osfrom dataclasses import dataclass, fieldfrom datetime import datetime, timezonefrom pathlib import Path@@ -327,15 +328,44 @@ class MemoryHandler:embedder_model = "all-MiniLM-L6-v2"vector_dimension = 384+ # Opt-in GPU offload: HEADROOM_EMBEDDER_RUNTIME=pytorch_mps routes embedding+ # through the torch sentence-transformers backend on the Apple GPU (MPS).+ # LocalEmbedder serializes MPS encode calls (torch-MPS is not thread-safe).+ # We switch only when MPS is actually available; otherwise keep the+ # existing default embedder selection path (ONNX when available, then+ # the pre-existing local sentence-transformers fallback).+ if os.environ.get("HEADROOM_EMBEDDER_RUNTIME", "").strip().lower() == "pytorch_mps":+ try:+ import sentence_transformers # noqa: F401+ import torch++ if torch.backends.mps.is_available():+ embedder_backend = "local"+ logger.info(+ "Memory: HEADROOM_EMBEDDER_RUNTIME=pytorch_mps → "+ "torch embedder on Apple GPU (MPS)"+ )+ else:+ logger.warning(+ "Memory: HEADROOM_EMBEDDER_RUNTIME=pytorch_mps requested but "+ "MPS is not available; using default embedder selection"+ )+ except ImportError:+ logger.warning(+ "Memory: HEADROOM_EMBEDDER_RUNTIME=pytorch_mps requested but "+ "torch/sentence-transformers not installed; using default embedder selection"+ )+# Check if ONNX runtime is available (should be — it's in proxy deps)- try:- import onnxruntime # noqa: F401- except ImportError:- # Fall back to sentence-transformers (requires torch)- embedder_backend = "local"- logger.info(- "Memory: onnxruntime not available, falling back to sentence-transformers"- )+ if embedder_backend == "onnx":+ try:+ import onnxruntime # noqa: F401+ except ImportError:+ # Fall back to sentence-transformers (requires torch)+ embedder_backend = "local"+ logger.info(+ "Memory: onnxruntime not available, falling back to sentence-transformers"+ )backend_config = LocalBackendConfig(db_path=self.config.db_path,diff --git a/pyproject.toml b/pyproject.tomlindex 59fcfc2cb..f734dba58 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -109,6 +109,12 @@ memory-stack = ["qdrant-client>=1.9.0,<2.0","neo4j>=5.20.0,<7.0",]+# Apple-Silicon GPU (MPS) offload for the memory embedder. Opt in at runtime with+# HEADROOM_EMBEDDER_RUNTIME=pytorch_mps. macOS-only; intentionally excluded from [all].+pytorch-mps = [+ "torch>=2.0.0; sys_platform == 'darwin'",+ "sentence-transformers>=2.2.0; sys_platform == 'darwin'",+]# Semantic relevance scoring with embeddings.# Uses `fastembed` (BAAI/bge-small-en-v1.5 by default — 33M params,# 384 dims, ~30 MB int8-quantized ONNX). Same library + model used bydiff --git a/tests/test_memory/test_embedder_mps_serialization.py b/tests/test_memory/test_embedder_mps_serialization.pynew file mode 100644index 000000000..31229af09--- /dev/null+++ b/tests/test_memory/test_embedder_mps_serialization.py@@ -0,0 +1,69 @@+"""Regression tests for the LocalEmbedder MPS serialization fix.++torch-MPS is not thread-safe: concurrent encode() calls from the default+multi-worker executor abort with "commit an already committed command buffer".+LocalEmbedder funnels every encode through a dedicated single-worker executor+when (and only when) the resolved device is MPS. CPU/CUDA keep the shared pool.+"""++from __future__ import annotations++import asyncio++import pytest++torch = pytest.importorskip("torch")+pytest.importorskip("sentence_transformers")++from headroom.memory.adapters.embedders import LocalEmbedder # noqa: E402++_HAS_MPS = bool(getattr(torch.backends, "mps", None)) and torch.backends.mps.is_available()+++async def test_cpu_uses_shared_executor() -> None:+ """On CPU the dedicated executor stays None (unchanged default-pool behavior)."""+ emb = LocalEmbedder(device="cpu")+ await emb.embed("hello world")+ assert emb._device == "cpu"+ assert emb._executor is None # default shared executor, not serialized+ await emb.close()++[email protected](not _HAS_MPS, reason="requires Apple-Silicon MPS")+async def test_mps_creates_single_worker_executor() -> None:+ """On MPS a dedicated max_workers=1 executor is created for serialization."""+ emb = LocalEmbedder(device="mps")+ await emb.embed("warmup")+ assert emb._device == "mps"+ assert emb._executor is not None+ assert emb._executor._max_workers == 1 # type: ignore[attr-defined]+ await emb.close()+ assert emb._executor is None # close() tears it down++[email protected](not _HAS_MPS, reason="requires Apple-Silicon MPS")+async def test_mps_concurrent_embeds_do_not_crash() -> None:+ """Concurrent embeds on MPS must not SIGABRT — the serialization guarantees it."""+ emb = LocalEmbedder(device="mps")+ await emb.embed("warmup")+ batches = [emb.embed_batch([f"text {i} " * 20] * 8) for i in range(16)]+ results = await asyncio.gather(*batches)+ assert len(results) == 16+ assert all(len(r[0]) == emb.dimension for r in results)+ await emb.close()++[email protected](not _HAS_MPS, reason="requires Apple-Silicon MPS")+async def test_mps_reembed_after_close_recreates_executor() -> None:+ """close() drops the cached model so a later embed() re-initializes and+ re-creates the serialized executor — never encodes on the torn-down pool."""+ emb = LocalEmbedder(device="mps")+ await emb.embed("warmup")+ await emb.close()+ assert emb._executor is None+ assert emb._model is None+ # Re-use after close must re-initialize cleanly and stay serialized.+ await emb.embed("again")+ assert emb._executor is not None+ assert emb._executor._max_workers == 1 # type: ignore[attr-defined]+ await emb.close()diff --git a/wiki/configuration.md b/wiki/configuration.mdindex c66611180..97866fcde 100644--- a/wiki/configuration.md+++ b/wiki/configuration.md@@ -311,6 +311,7 @@ Some settings can be configured via environment variables:| `HEADROOM_SAVINGS_PATH` | Full path to the proxy savings JSON ledger. Always wins when set. | derived from `${HEADROOM_WORKSPACE_DIR}` || `HEADROOM_TOIN_PATH` | Full path to the TOIN telemetry JSON file. Always wins when set. | derived from `${HEADROOM_WORKSPACE_DIR}` || `HEADROOM_SUBSCRIPTION_STATE_PATH` | Full path to the subscription tracker state. Always wins when set. | derived from `${HEADROOM_WORKSPACE_DIR}` |+| `HEADROOM_EMBEDDER_RUNTIME` | Set to `pytorch_mps` to run the memory embedder via the torch sentence-transformers backend on the Apple GPU (MPS). Only engages when Apple MPS is actually available; otherwise it logs a warning and uses the existing default embedder selection path. `pytorch_mps` is the only accepted value. Requires the `[pytorch-mps]` extra. See [Memory](memory.md#embedding-runtime--gpu-offload-apple-silicon). | default embedder selection |## Filesystem Contractdiff --git a/wiki/macos-deployment.md b/wiki/macos-deployment.mdindex 19a577ba9..e59a8e8ba 100644--- a/wiki/macos-deployment.md+++ b/wiki/macos-deployment.md@@ -648,6 +648,34 @@ Limit CPU and memory usage:</dict>```+## Apple GPU (MPS) Embedding Offload++On Apple Silicon, the proxy's memory embedder can run on the Apple GPU (MPS)+instead of the default ONNX CPU backend. Offloading embedding to the GPU frees+the CPU under load, keeping the proxy responsive — useful on fanless Macs (e.g.+the M5 Air) that are prone to CPU-saturation timeouts.++Enable it by installing the extra and setting the env var:++```bash+pip install 'headroom-ai[pytorch-mps]' # also works as [pytorch_mps]+export HEADROOM_EMBEDDER_RUNTIME=pytorch_mps+```++Under a LaunchAgent, set the env var in the plist `EnvironmentVariables`+section:++```xml+<key>HEADROOM_EMBEDDER_RUNTIME</key>+<string>pytorch_mps</string>+```++It only engages when Apple MPS is actually available (Apple Silicon + torch).+If MPS is unavailable or the dependencies are missing, the proxy logs a warning+and uses the existing default embedder selection path. This is strictly opt-in;+default behavior is unchanged. See [Memory](memory.md#embedding-runtime--gpu-offload-apple-silicon)+for details.+## FAQ**Q: Why LaunchAgent instead of running `headroom proxy` manually?**diff --git a/wiki/memory.md b/wiki/memory.mdindex cd0c6eadb..f699c4015 100644--- a/wiki/memory.md+++ b/wiki/memory.md@@ -446,6 +446,38 @@ config = MemoryConfig()```+### Embedding Runtime / GPU Offload (Apple Silicon)++By default the proxy's memory embedder runs on the **ONNX CPU** backend. This+is fast and dependency-light, but it is CPU-only — under sustained load the+embedding step can saturate the CPU and make the proxy less responsive.++On Apple Silicon you can opt in to running the embedder on the **Apple GPU+(MPS)** instead, which offloads that work off the CPU and keeps the proxy+responsive. This is especially useful on fanless Macs (e.g. the M5 Air) that+are prone to CPU-saturation timeouts.++Enable it by installing the extra and setting the env var:++```bash+pip install 'headroom-ai[pytorch-mps]' # also works as [pytorch_mps]+export HEADROOM_EMBEDDER_RUNTIME=pytorch_mps+```++When set, the embedder runs via the torch sentence-transformers backend on the+Apple GPU instead of the default ONNX CPU embedder. Notes:++- **Strictly opt-in.** `pytorch_mps` is the only accepted value; anything else+ (or unset) keeps the default ONNX CPU embedder. Default behavior is unchanged.+- **Auto-fallback.** It only activates when Apple MPS is actually available+ (Apple Silicon + torch). If MPS is unavailable or torch/sentence-transformers+ is not installed, it logs a warning and uses the existing default embedder+ selection path: ONNX when available, then the pre-existing local+ sentence-transformers fallback.+- **MPS serialization.** torch-MPS is not thread-safe, so the embedder+ serializes MPS encode calls internally via a single-worker executor. This is+ automatic — there is nothing to configure.+### Storage Configuration```python