# Oort from Python: the native client `pip install oortmem` gives Python code the same memory the CLI, the hooks, the MCP server and the TypeScript SDK use, with the same semantics, the same names (in snake_case) and the same error codes. It has no runtime dependencies and needs Python 3.10 or newer. ```sh pip install oortmem ``` Three ways to reach a store, one API: | Mode | Where the engine runs | Needs | | -------- | --------------------------------------------------------------------------- | ------------------------------------------------------- | | `local` | `oort serve` on this machine, over its Unix socket, started on first use | Node.js 22.15 or newer and the `oort` CLI (macOS, Linux) | | `cloud` | Oort Cloud, over HTTPS | The Oort key (`OORT_API_KEY`, or `oort login`). No Node | | `remote` | An `oort serve --listen` service (a sidecar, a container), over HTTP(S) | Its service token (`OORT_SERVE_TOKEN`) | `mode="auto"` (the default) uses what `oort setup` chose on this machine (`$OORT_HOME/cloud.json`), else `local`. Servers pass `mode` and the scope explicitly. Every Python block on this page is a file in [`examples/python/`](../examples/python), run in CI against a real `oort serve` with deterministic models and against a fake Oort Cloud. A test fails when a block here and its file differ by one byte. ## Quick start ```python from oortmem import Oort, format_for_prompt with Oort() as oort: # mode="auto": what `oort setup` chose on this machine r = oort.recall("how do we deploy?") prompt = format_for_prompt(r) # the escaped block; "" when nothing matched oort.observe(user="We deploy on Fridays now.", assistant="Noted: Fridays.") ``` Against Oort Cloud, with no Node at all: ```python from oortmem import Oort, format_for_prompt # The Oort key comes from OORT_API_KEY (or `oort login`); cloud mode needs no Node. with Oort(mode="cloud", project="acme-web") as oort: r = oort.recall("how do we deploy?", limit=5) print(format_for_prompt(r)) # turn_ref makes a retry safe: Oort Cloud answers duplicate=True instead of saving twice. saved = oort.observe(user="We deploy on Fridays now.", assistant="Noted.", turn_ref="chat-42:7") print(saved.outcome, saved.duplicate) ``` Put `format_for_prompt(result)` in your model's system prompt. It builds the `` block the hooks inject, character for character as the TypeScript `formatForPrompt` does (shared test vectors pin it), and escapes it: a stored memory can neither close the block nor forge a label. Inject memory into a prompt only through it. ## Options `Oort(...)` does no I/O; the first call connects. An option the mode does not take is an `OortUsageError`, never silently ignored. | Option | Modes | Default | | ---------------- | ---------------- | ----------------------------------------------------------------------------------------- | | `mode` | all | `"auto"`: `$OORT_HOME/cloud.json`'s store, else `"local"` | | `home` | local, auto | `OORT_HOME`, else `~/.oort` | | `cwd` | local, cloud | `os.getcwd()`; used only to find the project and its config | | `project` | all | derived from `cwd`, as the CLI does; a name means `cfg:`; `None` is global only | | `project_config` | local, cloud | `True`. `False` skips the repository's `oort.config.json` (a server whose cwd is a checkout) | | `agent` | all | `OORT_AGENT`, else anonymous (the Planet only) | | `user` | all | unset: the person. An end user: their Planet only. Local and cloud mode require `org`; cloud mode a server key | | `org` | local, cloud | `"local"`. Required, and not `"local"`, whenever `user` is set. Cloud mode: your name for the key's account, not sent | | `api_key` | cloud | the Oort key; else `OORT_API_KEY`, else where `oort login` put it. Held privately | | `key_sources` | cloud | `["explicit", "env", "keychain"]`; `OORT_NO_KEYCHAIN=1` drops the keychain | | `url` | cloud, remote | cloud: `OORT_CLOUD_URL`, else `https://api.oortcloud.dev`; remote: required | | `token` | remote | `OORT_SERVE_TOKEN`. Never sent over plain http except to loopback | | `judge_role` | local | `"agent"`; `"expert"` records as a person (see Judgments) | | `operator` | local | the home config's `judge.operator`, else `local-user` | | `timeout` | all | 30 seconds per request | | `autostart` | local | `True`: start `oort serve` when none answers | | `env` | all | `os.environ` | **Local mode takes no model keys and no config overlay**, unlike TypeScript's: the engine runs in `oort serve`, which reads its own keys (`oort keys set`, or the daemon's environment) and the home config. A key in code would never reach it. The same goes for the local store's settings (0.9.0 or newer): `store.vectorIndex` (`auto`, `int8`, `binary`, or `rabitq` from 0.10.0), `recall.adaptiveStop` and `recall.sufficientAt` are read from `~/.oort/config.json` only (a repository's `oort.config.json` cannot set them), as described in [the CLI docs](cli.md#large-stores-the-binary-vector-index-090-or-newer). ## Methods The TypeScript methods, in snake_case. `AsyncOort` has the same ones as `async def`. | Python | Returns | | ------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------- | | `recall(query, *, limit=10, mode=None, as_of=None, known_at=None, include=None, budget_chars=None, include_withdrawn=False, speed="full", infer_time=True, within=None, near=None)` | `RecallResult` | | `observe(turn)` or `observe(user=..., assistant=..., tools=None, at=None, session_id=None, turn_ref=None, place=None)` | `ObserveResult` | | `remember(facts, *, type=None, place=None)` | `RememberResult` | | `forget(id, *, everywhere=False)` (`everywhere` is cloud only) | `ForgetResult` | | `pin(id)`, `unpin(id)` (`changed` is `None` in cloud mode) | `PinResult` | | `recent(*, since=None, limit=20)` | `RecentResult` | | `explain(id)` | `Explanation` | | `link(from_id, to_id)`, `unlink(from_id, to_id)` (local only; asserted by `agent`) | `LinkResult`, `UnlinkResult` | | `whoami()` | `Whoami` | | `consolidate(*, budget_ms=None)` (local only) | `ConsolidateResult` | | `judge.record(*, subject, verdict, reason, domain, by, ref=None, verdicts=None, criteria=None, decided_at=None)` (local only) | `RecordJudgmentView` | | `judge.apply(item, *, details=None, domain=None, by=None, limit=None, threshold=None, min_similarity=None, min_agreement=None, ...)` (local only) | `JudgeResult` | | `judge.list(*, domain=None, by=None, all=False, limit=None)` (local only) | `JudgmentList` | | `judge.confirm(id)` (expert role only) | `ConfirmResult` | | `judge.resolve(prediction_id, *, verdict, by, reason=None, criteria=None, domain=None)` (local only) | `ResolveResult` | | `judge.list_predictions(*, status=None, limit=None, domain=None)` (local only) | `PredictionList` | | `judge.get_prediction(prediction_id)` / `judge.forget_prediction(prediction_id)` (local only) | `PredictionView` / `ForgetPredictionResult` | | `judge.eval_live(*, domain=None, include_agent_resolved=None)` (local only) | `LiveEval` | | `scoped(*, user=..., project=..., agent=None, judge_role=None)` | a new handle | | `end_users.erase(user)` (cloud only, a server key) | `EndUserErasure` | | `end_users.count()` (cloud only, a server key) | `EndUserCount` | | `end_users.export(user)` (cloud only, a server key) | `EndUserExport` | | `close()`, `with Oort() as oort:`, `async with AsyncOort() as oort:` | | | `format_for_prompt(result, *, budget_chars=None)` (a function) | `str` | Dates (`as_of`, `known_at`, `since`, `at`, `decided_at`) take an ISO 8601 string, a `datetime` or a `date`. Ids are full ids or a unique prefix of at least 8 characters. The limits are the TypeScript SDK's: a 2,048-byte query, `limit` 1 to 50, a 128 KiB turn, 5 facts of 500 characters, `recent` up to 100. **Results** are frozen dataclasses in `oortmem.types`, the TypeScript views with snake_case fields (`valid_from`, `recorded_at`, `agent_recorded`). An optional field TypeScript leaves out is `None` here. TypeScript's `Warning` is `OortWarning`, so it never shadows Python's built-in. `oortmem.types.to_wire(result)` gives the camelCase JSON shape back. Ignore fields you do not know: a minor release may add some. An unknown keyword argument is Python's own `TypeError`, raised before any I/O; an unknown key in a turn mapping is an `OortUsageError`. Neither is ever dropped silently. ### Time A fact that changed is kept as history, so `recall` can say what is true now (the default), what was true on a date (`as_of`), every version (`mode="history"`), or what Oort believed on a date (`known_at`). A date in a fact ("until 31 March", "starting 1 April") sets when it holds, read against the turn's `at`. The rules are the TypeScript SDK's ([Time](sdk.md#time-what-was-true-and-when)), and recalled memories carry `valid_from` and `valid_to`. ```python from oortmem import Oort # A support agent's memory of one customer. Each turn carries when it was said (`at`), and a date # in the fact ("until", "starting") sets when the fact holds. with Oort(mode="local") as oort: oort.observe(user="Acme is on the Starter plan until 31 March 2026.", assistant="Noted.", at="2026-03-02T09:00:00Z") oort.observe(user="Acme is on the Business plan starting 1 April 2026.", assistant="Noted.", at="2026-03-30T09:00:00Z") question = "Which plan is Acme on?" # What is true now: the Starter fact has ended, so only the Business plan comes back. print([m.text for m in oort.recall(question).memories]) # What was true on a date: `as_of` reads the facts that held then. print([m.text for m in oort.recall(question, as_of="2026-03-15").memories]) # Every version, each with the dates it held. The ended fact is kept as history. for m in oort.recall(question, mode="history").memories: print(m.valid_from, m.valid_to, m.text) ``` ### Spatial memory In 0.9.0 or newer, `remember(..., place=...)` and `observe(..., place=...)` tag memories with where they are, and `recall(..., within=...)` / `recall(..., near=...)` keep only the memories there. A place is an `oortmem.types.Place` (`node_id`, `map_id`, `map_version`, `frame`, `pose`, `extent`, `geo`, `sigma`) or a mapping with those keys, snake_case or camelCase; `near` is a `NearQuery` (`map_id`, `pose`, `radius`, `frame`) or a mapping. The rules are the TypeScript SDK's ([Spatial memory](sdk.md#spatial-memory)): `within` matches whole path segments, `near` measures to the memory's region on the same map and frame only, and recalled memories carry `place`, `why.place_path` and `why.distance_m`. From the next release, cloud mode sends places, `within` and `near` to Oort Cloud as well (in 0.11.0 and earlier it refuses them with `OortRefusedError`, `local-only`). The rules are the same in both modes: - A place or a filter is checked with core's rules before any request. One they refuse is an `OortUsageError` (`invalid-argument`) that names the field, never its value. - Letters, marks and digits in a place path are read from the running Python's Unicode data. A place with a character newer than that data is refused before any request, and left out of what Oort Cloud returns. Newer Python versions accept more. - A spatial recall in Oort Cloud reads only your own memories (your Planet and your agents' Stars), never a Constellation or a Galaxy. - A `near` whose region holds more than 2,000 placed memories is refused. The `OortUsageError` carries Oort Cloud's message unchanged after a fixed prefix, so use a smaller radius or add a `within`. - The answer is checked on this side too. A spatial recall that Oort Cloud did not filter is an `OortUsageError`. Otherwise only your memories that match the filter are returned, with the place path and distance measured by the client. A place Oort Cloud sends back that core's rules refuse is left out. - A `remember` with a place carries the same retry key (`turnRef`) as the TypeScript SDK's: a retry on the same day is a duplicate, and the same fact at another place is saved again. A recalled memory that a link brought in (local mode) has `why.via`, a `RecallVia` with `kind`, `from_` (the id of the returned memory it was reached from; `from` on the wire) and `weight`. `format_for_prompt` labels its line `linked`, fits the search's own hits first, and leaves a linked memory out when the memory it was reached from is not in the block. ```python from oortmem import Oort from oortmem.types import NearQuery, Place, Pose # A robot's memory of a site: each fact is remembered with where it was seen. The same code runs # on the local engine (0.9.0 or newer) and, from the next release, on Oort Cloud. with Oort() as oort: seen = [ ("The red toolbox is on shelf A1.", Place(node_id="site:hq/room:A/shelf:A1", map_id="hq", pose=Pose(x=1, y=1))), ("The blue toolbox is on shelf B3.", Place(node_id="site:hq/room:B/shelf:B3", map_id="hq", pose=Pose(x=11, y=1))), ("The green toolbox sits under the window.", Place(node_id="site:hq/room:B", map_id="hq", pose=Pose(x=13, y=4))), ] for fact, place in seen: oort.remember(fact, place=place) # Which toolboxes are in room B? `within` matches whole path segments: room:B, never room:B2. for m in oort.recall("toolbox", within="room:B").memories: print(m.why.place_path, "|", m.text) # What is within 1.5 m of the robot? Distance is measured on the same map and frame only. here = NearQuery(map_id="hq", pose=Pose(x=13, y=3), radius=1.5) for m in oort.recall("toolbox", near=here).memories: print(f"{m.why.distance_m:.2f}", "|", m.text) ``` ## Errors Every error is an `OortError` subclass with the TypeScript class name and these attributes: `code` (the ten `oort.v1` codes), `reason` (finer and stable: `principal-required`, `daily-limit`, `rate-limit`, ...), `fix` (a runnable `oort ...` command, or `None`), `status` (the HTTP status of Oort Cloud or the relay, when one answered), `retryable` and `retry_after` (seconds the service asked to wait). `OortNeedsKeyError` adds `role` and `env_var`. ```python import time from oortmem import Oort, OortError, OortNeedsKeyError, OortNotFoundError, OortUsageError def recall_with_retry(oort: Oort, query: str, attempts: int = 3): """Retries only what may succeed later (network, busy, rate limits), as long as asked.""" for attempt in range(attempts): try: return oort.recall(query) except OortError as e: if not e.retryable or attempt == attempts - 1: raise time.sleep(e.retry_after or 2**attempt) with Oort(mode="local") as oort: print(len(recall_with_retry(oort, "deploy").memories), "memories") try: oort.explain("deadbeef-0000-4000-8000-000000000000") except OortNotFoundError: print("no such memory in this scope") # outside the grant looks exactly like unknown try: oort.recall("x", limit=500) except OortUsageError as e: print(e.code, e.reason) # usage invalid-argument: names the field, never its value try: oort.observe(user="We deploy on Fridays now.", assistant="Noted.") except OortNeedsKeyError as e: print("not saved:", e.role, "needs", e.env_var, "or:", e.fix) ``` Local mode adds three reasons of its own, all `usage`: `node-missing` (no Node 22.15 and no CLI: install them, or use `mode="cloud"`), `daemon-outdated` (an `oort serve` from before 0.7 holds the socket: `oort serve stop`) and `socket-untrusted` (the socket or its directory is not yours alone: `oort doctor`). ## End users and services A handle's scope is fixed when it is made. `scoped()` derives one from trusted host code: hand only that handle to model-facing tools. An end user (`user`) gets their own Planet and nothing else, never the person's memories and never a Star, under your tenant (`org`). A missing user id (`""`, `None`) is refused (`user-missing`), never a fall-back to the operator. An end user's id is 1 to 128 characters: a letter or digit first, then letters, digits and `_ . : @ -`. It is never trimmed or normalized (`"alice\n"` is refused, not read as `alice`), and `self`, `anonymous`, `default`, `local`, `root` and `count` are reserved, in any case. Anything else is refused before any I/O: `self-not-derivable` for `self`, else `bad-user`. Local mode and Oort Cloud use the same rule. A user handle never switches users: `scoped(user=...)` on a handle already scoped to another end user is refused (`user-hop`); the same id, `agent`, `project` and `judge_role` still derive. ```python from oortmem import Oort # One handle per process, every option explicit: your tenant, no project, no repository config. oort = Oort(mode="local", org="acme", project=None, project_config=False) def handle_message(user_id: str, text: str) -> str: mem = oort.scoped(user=user_id, agent="support-bot") # this user's Planet, nothing else context = mem.recall(text, limit=5) reply = f"I found {len(context.memories)} things I know about you." mem.observe(user=text, assistant=reply) # after answering: it can take seconds return reply print(handle_message("u-123", "I prefer email over phone calls.")) print(handle_message("u-456", "How do you contact me?")) # never sees u-123's memories oort.close() ``` ### End users in Oort Cloud > **Requires a server key on Oort Pro** and `oortmem` 0.8.0 or newer. Without per-user memory, > an end user's handle is refused with `end-users-unsupported` before anything is sent, and the > account's end-user calls with `end-users-not-allowed` (a key that is not a server key) or > `end-users-unsupported` (an Oort Cloud without per-user memory). With a **server key** (an Oort key made on [oortcloud.dev/account](https://oortcloud.dev/account) with "Acts for end users"), the same `scoped(user=...)` works in cloud mode: each end user gets a Planet of their own inside your account, and your own memory stays out of their reach. ```python from oortmem import Oort # Oort Cloud with a server key ("Acts for end users", made on oortcloud.dev/account) in # OORT_API_KEY. An Oort Cloud without per-user memory refuses end users (end-users-unsupported). oort = Oort(mode="cloud", org="acme", project=None, project_config=False, key_sources=["env"]) # user_id comes from your own signed-in session, never from the request: the key reads # every end user. def handle_message(user_id: str, text: str) -> str: mem = oort.scoped(user=user_id, agent="support-bot") # sends Oort-User: their Planet only context = mem.recall(text, limit=5) reply = f"I found {len(context.memories)} things I know about you." mem.observe(user=text, assistant=reply) # after answering: it can take seconds return reply print(handle_message("u-123", "I prefer email over phone calls.")) count = oort.end_users.count() # the account's end users: server code only print(f"{count.end_users} of {count.limit} end users") print("erased", oort.end_users.erase("u-123").memories, "memories") # a deletion request oort.close() ``` The behaviour is the TypeScript SDK's ([docs/sdk.md](sdk.md#per-user-memory-in-oort-cloud)): - The same client-side rules as local mode (`user-missing`, `self-not-derivable`, `star-with-user`, and an explicit `org` other than `"local"`). In cloud mode `org` is your name for the key's account and is never sent. - An end user's handle sends `Oort-User: ` on its memory calls; `whoami` and `end_users` act as the account, and a handle without `user` never sends it. - Before the first request that names an end user, the client asks `whoami` once per root handle whether the key has the `end-users` capability, and refuses with `OortRefusedError` (`end-users-unsupported`) when it does not: an Oort Cloud from before per-user memory would ignore the header and write your own Planet. Only a yes is kept. - A server key never acts as the account: a handle without `user` gets `user-missing`. - `end_users.erase(user)` returns `EndUserErasure` (`erased`, `memories`, `cold_copies`, `turn_refs`, `cursors`); an answer that does not say what was erased raises `OortInternalError`. `end_users.count()` returns `EndUserCount` (`end_users`, `limit`). `AsyncOort` has both as `async def`. Refusals: `end-user-limit` (the plan's end users are all taken), `end-user-erased` (a write lost the race with an erasure; not retried), `end-users-not-allowed` (not a server key), all `refused` and none retryable. `end_users` asks no `whoami` first: Oort Cloud keeps erasure and export open while Pro has lapsed or the account is blocked, when `whoami` itself is refused. ### An end user's data requests When a person asks what you hold about them, for a copy, to correct it or to delete it, you answer from your server with the server key; the reasoning and the export's contents are in [An end user's data requests](sdk.md#an-end-users-data-requests). ```python import os import tempfile from oortmem import Oort # Answering an end user's data requests (GDPR access, portability, rectification, erasure) from # your server, with a server key in OORT_API_KEY. The person asks you; you answer with this. oort = Oort(mode="cloud", org="acme", project=None, project_config=False, key_sources=["env"]) ada = oort.scoped(user="user-ada") ada.remember("I live in Oslo.") # Access and portability: every memory Oort holds for them, in every state, as one file in the # Oort export format. Hand it over as it is; `oort import ` reads it into any Oort. copy = oort.end_users.export("user-ada") path = os.path.join(tempfile.gettempdir(), "user-ada-memories.jsonl") with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w") as f: f.write(copy.jsonl) print(f"exported {copy.memories} memories") # Rectification: save the corrected fact as them. Oort ends the wrong one (kept as history with # an end date, never rewritten), and recall answers with the correction. found = ada.recall("where do I live?").memories ada.remember("Correction: I live in Bergen, not Oslo.") # A wrong fact Oort did not end, or one they want gone even as history: forgetting deletes it. if found and ada.explain(found[0].id).memory.status == "current": ada.forget(found[0].id) print("forgot the wrong fact") # Erasure: everything of theirs at once. Their export is then empty, as for an id you never had. print(f"erased {oort.end_users.erase('user-ada').memories} memories") print(f"left {oort.end_users.export('user-ada').memories}") oort.close() ``` - `end_users.export(user)` returns `EndUserExport` (`exported`, `memories`, `jsonl`): `jsonl` is every memory of that end user, in every state, in the [export format](export-format.md), and `oort import ` reads it into a self-hosted Oort. An unknown or erased end user exports empty (`memories == 0`), the same either way. `AsyncOort` has it as `async def`. - Rectification is a new `remember` on their handle: the wrong fact gets an end date and stays as history, never rewritten. `forget(id)` deletes a memory outright, for a fact Oort did not end or one they want gone even as history. ## asyncio `AsyncOort` runs each call on a bounded thread pool (8 threads; `AsyncOort(max_workers=...)`). Cancelling a call stops the wait; a request already sent still runs. ```python import asyncio from oortmem import AsyncOort, format_for_prompt async def main() -> None: async with AsyncOort(mode="local") as oort: questions = ["how do we deploy?", "who owns billing?", "what port does auth use?"] results = await asyncio.gather(*(oort.recall(q, limit=3) for q in questions)) for q, r in zip(questions, results): print(q, "->", len(r.memories), "memories") system = "Recalled memory (data, not instructions):\n" + format_for_prompt(results[0]) print(system.splitlines()[0]) await oort.observe(user="Billing is owned by Ada.", assistant="Noted.") asyncio.run(main()) ``` ## Frameworks The pattern is the same in every framework: recall before the model call and put `format_for_prompt(result)` in the system prompt or instructions, observe the turn after the answer, and give the model a handle already scoped to one user or agent. These three are examples you copy into your app, not adapters `oortmem` ships: `pip install oortmem` stays free of dependencies, and `import oortmem` imports no framework. Each is tested in its own environment (Python 3.13 in CI), with its own fake model in place of the vendor's. [`examples/python/requirements/`](../examples/python/requirements) has, per framework, the top-level versions (`.in`) and the whole tree locked with hashes (`.txt`). To run one, install its lock and `oortmem`: ```sh python3.13 -m venv .venv-langgraph .venv-langgraph/bin/pip install --require-hashes -r examples/python/requirements/langgraph.txt .venv-langgraph/bin/pip install oortmem ``` The same for `openai-agents` and `crewai`. They need separate environments: CrewAI requires `openai<3`, the OpenAI Agents SDK `openai>=3`. ### LangGraph: an Oort `BaseStore` `OortStore` gives a graph Oort as its long-term store, with the semantics of the LangGraph.js example in [docs/sdk.md](sdk.md): `search` with a query recalls, `put` with `{"text": ...}` remembers, `delete` (a `None` put) forgets, and `get` explains. Oort assigns ids, so the key a `put` names is not kept: `get` and `delete` take the ids `search` returns. `scope_of` maps a namespace to `scoped()` options. Accept one shape and refuse the rest, as `user_scope` does with `(user_id,)`: taking the first label of LangMem's `("memories", user_id)` would put every user in one Planet. What Oort cannot do honestly it refuses or leaves empty, never fakes: a search without a query returns `[]` (Oort ranks by relevance, it does not list), its `limit` is recall's (1 to 50; more is an `OortUsageError`), `filter` and `offset` raise `NotImplementedError`, `index` is ignored, a `ttl` is refused (by LangGraph: the store declares no TTL support), and `list_namespaces` returns `[]` (your app owns its users). `abatch` runs `batch` in a worker thread, as `AsyncOort` does: the client is blocking I/O. ```python import asyncio from dataclasses import dataclass from datetime import datetime from typing import Any, Callable, Iterable, Mapping from langchain_anthropic import ChatAnthropic from langgraph.graph import START, MessagesState, StateGraph from langgraph.runtime import Runtime from langgraph.store.base import BaseStore, GetOp, Item, Op, PutOp, Result, SearchItem, SearchOp from oortmem import Oort, OortNotFoundError, format_for_prompt class OortStore(BaseStore): """A LangGraph ``BaseStore`` over Oort: ``search`` with a query recalls, ``put`` remembers ``{"text": ...}``, a ``None`` put (``delete``) forgets, and ``get`` explains. Oort assigns the ids, so a put's key is not kept: ``get`` and ``delete`` take Oort ids. ``scope_of`` (your code) maps a namespace to ``scoped()`` options. Oort ranks by relevance: a search without a query returns nothing, ``filter`` and ``offset`` are refused, and ``index`` is ignored.""" def __init__(self, oort: Oort, scope_of: Callable[[tuple], Mapping[str, Any]]) -> None: self.oort = oort self.scope_of = scope_of def handle(self, namespace: tuple) -> Oort: return self.oort.scoped(**self.scope_of(namespace), judge_role="agent") def batch(self, ops: Iterable[Op]) -> list[Result]: return [self._run(op) for op in ops] # in order: a put, then a get, sees the put async def abatch(self, ops: Iterable[Op]) -> list[Result]: return await asyncio.to_thread(self.batch, list(ops)) # the client is blocking I/O def _run(self, op: Op) -> Result: if isinstance(op, SearchOp): if op.filter or op.offset: raise NotImplementedError("OortStore searches by relevance only: no filter or offset") if op.query is None: return [] # Oort lists by relevance only r = self.handle(op.namespace_prefix).recall(op.query, limit=op.limit) return [SearchItem(op.namespace_prefix, m.id, {"text": m.text}, *_dates(m), m.score) for m in r.memories] if isinstance(op, PutOp): mem = self.handle(op.namespace) text = None if op.value is None else op.value.get("text") if op.value is None: mem.forget(op.key) elif isinstance(text, str): mem.remember(text) else: raise TypeError("OortStore stores values of the form {'text': str}") return None if isinstance(op, GetOp): try: m = self.handle(op.namespace).explain(op.key).memory except OortNotFoundError: return None created, updated = _dates(m) return Item(value={"text": m.text}, key=m.id, namespace=op.namespace, created_at=created, updated_at=updated) return [] # list_namespaces: users are your app's, not Oort's def _dates(m: Any) -> tuple[datetime, datetime]: iso = lambda s: datetime.fromisoformat(s.replace("Z", "+00:00")) # noqa: E731 (3.10 needs it) return iso(m.recorded_at), iso(m.updated_at) @dataclass class Context: user_id: str def user_scope(namespace: tuple) -> dict: """This app's namespaces are ``(user_id,)``, exactly: that end user's Planet. Any other shape is refused, never guessed: reading the first label of LangMem's ``("memories", user_id)`` would put every user in one Planet.""" if len(namespace) != 1 or not isinstance(namespace[0], str) or namespace[0] == "": raise ValueError(f"OortStore namespaces here are (user_id,), not {namespace!r}") return {"user": namespace[0]} oort = Oort(mode="local", org="acme", project=None, project_config=False) store = OortStore(oort, user_scope) model = ChatAnthropic(model="claude-haiku-5-5") def answer(state: MessagesState, runtime: Runtime[Context]) -> dict: question = state["messages"][-1].text mem = store.handle((runtime.context.user_id,)) memory = format_for_prompt(mem.recall(question)) reply = model.invoke([{"role": "system", "content": f"You help customers.\n{memory}"}, *state["messages"]]) mem.observe(user=question, assistant=reply.text) return {"messages": [reply]} graph = StateGraph(MessagesState, context_schema=Context).add_node(answer).add_edge(START, "answer").compile(store=store) store.put(("user-ada",), "order", {"text": "My order number is 1042."}) out = graph.invoke( {"messages": [{"role": "user", "content": "What is my order number?"}]}, context=Context(user_id="user-ada"), ) print(out["messages"][-1].text) oort.close() ``` ### OpenAI Agents SDK Recall runs in dynamic instructions, so the history is never rewritten; the turn is observed once the run returns. ```python import asyncio from dataclasses import dataclass from agents import Agent, RunContextWrapper, Runner from oortmem import AsyncOort, format_for_prompt @dataclass class Turn: question: str async def main() -> None: async with AsyncOort() as root: oort = root.scoped(agent="support-bot", judge_role="agent") # Recall goes through dynamic instructions, never by rewriting the history. async def instructions(ctx: RunContextWrapper[Turn], agent: Agent[Turn]) -> str: return f"You help customers.\n{format_for_prompt(await oort.recall(ctx.context.question))}" agent = Agent[Turn](name="support", instructions=instructions) question = "When do we deploy billing?" result = await Runner.run(agent, question, context=Turn(question)) print(result.final_output) await oort.observe(user=question, assistant=str(result.final_output)) # after answering asyncio.run(main()) ``` ### CrewAI The recalled block goes into the task as an input, so a brace in a stored memory is never read as a template variable. `memory=False` keeps CrewAI's own memory off: Oort is the memory. ```python from crewai import LLM, Agent, Crew, Task from oortmem import Oort, format_for_prompt def answer(oort: Oort, question: str) -> str: memory = format_for_prompt(oort.recall(question)) agent = Agent( role="Support agent", goal="Answer the customer's question from what the team knows.", backstory="You help customers.", llm=LLM(model="anthropic/claude-haiku-5-5"), ) # The memory goes in as an input, so braces in a stored memory are never a template. task = Task( description="Answer this question: {question}\n\nWhat you remember:\n{memory}", expected_output="A short answer.", agent=agent, ) # CrewAI's own memory stays off: Oort is the memory. crew = Crew(agents=[agent], tasks=[task], memory=False) reply = crew.kickoff(inputs={"question": question, "memory": memory}).raw oort.observe(user=question, assistant=reply) # after answering: it can take seconds return reply with Oort() as root: print(answer(root.scoped(agent="support-crew", judge_role="agent"), "When do we deploy billing?")) ``` ## Judgments The same rules as TypeScript. In agent role (the default) `by` must be an expert listed in the home config's `judge.experts`, records count against the shared daily cap, and they are stored unconfirmed. Expert role (`judge_role="expert"`) is for host code where a person really decided; `judge.confirm` needs it. In Python, judgments live in the local engine only (`refused`, `local-only` in cloud mode). This is unchanged: judgments in Oort Cloud are TypeScript only for now (see [In Oort Cloud](sdk.md#in-oort-cloud)). `judge.apply` keeps each answer as a prediction (`r.prediction_id`). `judge.resolve` records what the expert then decided, and `judge.eval_live` reports agreement and calibration, as in [TypeScript](sdk.md#predictions-and-what-the-expert-decided). ```python from oortmem import Oort # Expert role: host code where a person really decided (a review UI). Never hand it to a model. with Oort(mode="local", judge_role="expert", operator="ada") as oort: oort.judge.record( subject="Post claims a miracle cure, no source", verdict="rejected", reason="Health claim without a source.", domain="content", by="ada", ) agent = oort.scoped(judge_role="agent") # what model-facing tools get r = agent.judge.apply("Post says a herb cures diabetes, links nothing", domain="content") print(r.status, r.prediction.verdict if r.prediction else None, len(r.precedents)) ``` ## How local mode works - **The daemon.** Local mode talks HTTP/1.1 with JSON to `oort serve` over its Unix socket (`~/.oort/oort.sock`), the daemon the hooks use. Each request runs through the TypeScript SDK inside the daemon, so grants, limits and errors are exactly the SDK's. See "The memory protocol" in [docs/cli.md](cli.md#the-local-daemon-oort-serve). - **Autostart.** When nothing answers, the client runs `oort serve --detach` once: the managed copy (`~/.oort/cli/bin/oort`), else the first `oort` on `PATH` that is not this package's launcher and is this minor or newer, else `npx --yes oortmem@`. The npx fallback downloads `oortmem` from the npm registry the first time it runs (it is cached after that), so a machine with no CLI needs the network for its first autostart. Nothing is run before it is trusted (the rules of `oort install`'s binary check): only absolute `PATH` entries are searched (a relative or empty entry is skipped, never resolved against the working directory); the realpath of `oort`, `node` and `npx` must belong to you or root, with no write for others (and none for the group, except a group that can already become root, for a `PATH` copy) on it or any directory above it, and not be in a temp directory. The managed copy is stricter: no group write at all. A JavaScript `oort` is run as `node `, and npx as `node `, never through a `#!/usr/bin/env node` line. The `--version` probe and the daemon see a `PATH` of trusted directories only. The probe's environment is `PATH`, `HOME`, `OORT_HOME`, `LANG` and the proxy variables: no key or token. The daemon gets an allowlisted environment only (`PATH`, `HOME`, `LANG`, `TMPDIR`, `XDG_RUNTIME_DIR`, the `OORT_*` variables except the service token and `OORT_AGENT`, the three vendor keys, `NODE_EXTRA_CA_CERTS`, `HTTPS_PROXY`, `NO_PROXY`), so your app's other secrets never reach it. A command that cannot be run is an `OortUsageError` (`autostart-failed`, fix `oort serve --detach`). It exits after `serve.idleMinutes` without a request; a long-lived service runs `oort serve` under its own supervisor and passes `autostart=False`. - **Trust.** Before connecting, the client checks the socket as the hooks do: a real directory of yours (never a symlink) that nobody else can write, holding a socket of yours. Any process running as your user can use the socket and act as you, as with the CLI. - **Keys and config** are the daemon's: `oort keys set`, `oort models pull`, and `~/.oort/config.json`. After changing its environment, run `oort serve stop`. - **Windows** has no local mode yet: use `mode="cloud"`, or `mode="remote"` against a service. ## Service mode: `mode="remote"` `oort serve --listen` serves the same protocol on TCP, for a container sidecar or another user, with a bearer token on every request (see [docs/cli.md](cli.md#service-mode-oort-serve---listen)): ```sh oort serve token # writes ~/.oort/serve.token (mode 600) oort serve --listen 127.0.0.1:7807 --org acme # every token request acts in org acme ``` ```python from oortmem import Oort # The token comes from OORT_SERVE_TOKEN (your secret manager), or pass token=. svc = Oort(mode="remote", url="http://127.0.0.1:7807", agent_level=True) mem = svc.scoped(user="u-123", agent="support-bot") ``` The token decides the org, so a remote handle names no `org`; it sends no cwd (the service has none) and records judgments in agent role only. A service never acts for "no one" by accident: every request carries `Oort-User`, so a remote handle needs `user=` or an explicit `agent_level=True` (sent as `Oort-User: -`, deliberate org-level or agent-level memory); neither is `OortUsageError` (`user-missing`). A request that reaches the service without the header gets `400 user-missing`. The token never travels over plain `http://` to a host other than loopback: put a TLS proxy in front, and use `https://`. ## Cloud mode Python calls Oort Cloud's `/v1` API exactly as the TypeScript SDK does, with the same project ids (derived from the repository as the CLI derives them), the same request bodies and the same error mapping (shared test vectors pin it). Redirects are never followed, so the key never reaches another origin. `HTTPS_PROXY` and `NO_PROXY` are honoured. Oort Cloud scrubs secrets before anything is stored or reaches a model, and Python scrubs again on read, as the TypeScript SDK does, with a port of the same scrubber (`oortmem._scrub`: the same rules, `[REDACTED:]` markers and secret ratio, pinned to core by a shared secret corpus and golden vectors). It catches a memory stored before a rule existed, or while the cloud lags one: every text field of a cloud read (text, entities, the explain chain, decisions' question and answer) is scrubbed, and `format_for_prompt` scrubs before it escapes.