Metadata-Version: 2.4
Name: crystalbrain
Version: 0.7.0
Summary: Python client for the CrystalBrain REST API: read and write the memory of your users.
Project-URL: Homepage, https://crystalbrain.ai
Project-URL: Documentation, https://crystalbrain.ai/docs/api
License-Expression: MIT
License-File: LICENSE
Keywords: agents,ai-memory,chatgpt,claude,crystalbrain,llm,mcp,memory,sdk
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# crystalbrain (Python)

Python client for the [CrystalBrain](https://crystalbrain.ai) REST API: read and write the memory of your users from your own app. No dependency (standard library only), Python 3.11 or later. Every input and result is typed (TypedDicts generated from the API's OpenAPI document).

## Install

```bash
pip install crystalbrain
```

## Usage

Create an API key on the Connections page of your CrystalBrain dashboard and keep it on your server.

```python
import base64
import os
from crystalbrain import CrystalBrain, CrystalBrainError

cb = CrystalBrain(os.environ["CRYSTALBRAIN_API_KEY"])

# What CrystalBrain knows about a customer, as a context block for your LLM
result = cb.search(user="cus_123", app="my-app", query="What does this customer prefer?")
print(result["context_block"])

# Save something durable about the customer (an unknown user is created)
cb.save(user="cus_123", app="my-app", type="preference", title="Delivery on Saturdays", content="Prefers deliveries on Saturday mornings.")

# An image (screenshot, photo): Claude describes it and transcribes its text; searches return that description, and
# "images" gives a link to each image found (valid one hour) to pass to your LLM
with open("checkout.png", "rb") as f:
    cb.add_document(user="cus_123", title="Checkout screen", image=base64.b64encode(f.read()).decode(), media_type="image/png")

# Skills written in the format of Claude skills (a SKILL.md per skill), from a zip: shared by the workspace, or given
# to one user with user="cus_123". A name the owner already has is updated.
with open("skills.zip", "rb") as f:
    imported = cb.import_skills(f.read())

# Correct or forget a memory (the previous version is kept)
memory_id = result["entries"][0]["id"]
cb.update_memory(id=memory_id, user="cus_123", app="my-app", content="Prefers deliveries on Sunday mornings.")
cb.archive_memory(id=memory_id, user="cus_123")

# Brain access: a group of customers reads your rules and writes into one common brain
cb.create_group(name="E-commerce customers", external_id="grp_ecom")
cb.update_group(group="grp_ecom", add_members=["cus_123"])
cb.update_brain(brain="Refund policy", groups=[{"group": "grp_ecom", "read": True, "write": False}])
cb.update_brain(brain="Customer behaviour", groups=[{"group": "grp_ecom", "read": False, "write": True}])
cb.update_user(user="cus_123", no_new_brain=True)  # its saves never create a brain

# Read everything, page by page
page = cb.list_memories(user="cus_123", limit=100)
while page["next_offset"] is not None:
    page = cb.list_memories(user="cus_123", limit=100, offset=page["next_offset"])

# Errors carry a stable code
try:
    cb.search(user="cus_123", query="hello")
except CrystalBrainError as err:
    if err.code == "RATE_LIMITED":
        print(f"retry in {err.retry_after} s")
```

`user` is your customer's ID or name in your own app; omit it to work on your own account. Query parameters named like Python keywords go through a dict: `cb.get_analytics(user="cus_123", kind="searches", **{"from": "2026-10-01"})`.

| Method | API call |
|---|---|
| `search(**input)` | `POST /api/v1/search` |
| `save(**input)` | `POST /api/v1/save` |
| `list_brains(user=None)` | `GET /api/v1/brains` |
| `create_brain(**input)` | `POST /api/v1/brains` |
| `list_users(**input)` | `GET /api/v1/users` |
| `add_document(**input)` | `POST /api/v1/documents` |
| `list_skills(user=None)` | `GET /api/v1/skills` |
| `mention_skill(**input)` | `POST /api/v1/skills` |
| `import_skills(zip, user=None, filename="skills.zip")` | `POST /api/v1/skills/import` |
| `log_tool_calls(**input)` | `POST /api/v1/tool-calls` |
| `update_memory(**input)` | `PATCH /api/v1/memory` |
| `archive_memory(**input)` | `POST /api/v1/memory/archive` |
| `delete_user(user)` | `DELETE /api/v1/user` |
| `restore_user(user)` | `POST /api/v1/user/restore` |
| `get_user(user=None)` | `GET /api/v1/user` |
| `get_brain(**input)` | `GET /api/v1/brain` |
| `list_memories(**input)` | `GET /api/v1/memories` |
| `get_memory(id)` | `GET /api/v1/memory` |
| `get_analytics(**input)` | `GET /api/v1/analytics` |
| `resolve_contradiction(**input)` | `POST /api/v1/contradictions/resolve` |
| `similar_memories(id, limit=None)` | `GET /api/v1/memory/similar` |
| `merge_memories(**input)` | `POST /api/v1/memories/merge` |
| `undo_merge(id)` | `POST /api/v1/memories/unmerge` |
| `update_brain(**input)` | `PATCH /api/v1/brain` |
| `update_user(**input)` | `PATCH /api/v1/user` |
| `list_groups()` | `GET /api/v1/groups` |
| `create_group(**input)` | `POST /api/v1/groups` |
| `update_group(**input)` | `PATCH /api/v1/group` |

Brain access: `update_brain` sets what the AI may do in a brain, for its owner (`ai_read`, `ai_write`) and for each group it is opened to (`read`, `write`; the list replaces the current one). `update_user` sets `no_new_brain` (the user's saves never create a brain: a memory no brain open in writing fits is not kept) and the user's exceptions for brains of its groups. A search, save or edit on a brain not open for that mode raises `CrystalBrainError` with code `BRAIN_CLOSED`.

Every field and error code: [API reference](https://crystalbrain.ai/docs/api).

## Options

`CrystalBrain(api_key, base_url="https://crystalbrain.ai", timeout=60, transport=None)`. `transport` replaces the HTTP layer (tests): a function `(method, url, headers, body) -> (status, headers, body)`.

## Development

- Types: `python scripts/generate_types.py` regenerates `src/crystalbrain/types.py` from `../typescript/openapi.json` (the API snapshot shared with the TypeScript SDK; update it there with `npm run spec`).
- Tests: `PYTHONPATH=src python -m unittest discover tests` (fake transport, no network).
- Publish: `python -m build` then `python -m twine upload dist/*` (needs a PyPI account).

## License

MIT
