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

# Python

# Python SDK

Official Python client for Anona Memory.

## Installation

```bash theme={null}
pip install anona
```

Requires Python 3.9+.

## Quick Start

```python theme={null}
from anona import AnonaClient

client = AnonaClient(api_key="anona_live_YOUR_KEY")

client.add_memory(space_id="spc_a1b2c3d4", content="Alice is a senior engineer at Google")

results = client.search(space_id="spc_a1b2c3d4", query="Where does Alice work?")
print(results)
```

***

## Client Initialization

```python theme={null}
from anona import AnonaClient

client = AnonaClient(
    api_key="anona_live_YOUR_KEY",
    base_url="http://anona-prod-alb-747552680.us-east-1.elb.amazonaws.com"  # optional, this is the default
)
```

The client doesn't read an environment variable automatically - pass `api_key` explicitly:

```python theme={null}
import os
client = AnonaClient(api_key=os.environ["ANONA_API_KEY"])
```

Use it as a context manager to ensure connections close cleanly:

```python theme={null}
with AnonaClient(api_key="anona_live_YOUR_KEY") as client:
    client.add_memory(space_id="spc_a1b2c3d4", content="...")
```

***

## Methods

### `add_memory(space_id, content, metadata=None) -> dict`

```python theme={null}
memory = client.add_memory(
    space_id="spc_a1b2c3d4",
    content="Alice works at Google and specializes in distributed systems",
    metadata={"user_id": "alice_123", "tags": ["work"]}
)
print(memory["memory_id"])
```

### `search(space_id, query, limit=10) -> list[dict]`

```python theme={null}
results = client.search(
    space_id="spc_a1b2c3d4",
    query="Where does Alice work?",
    limit=10
)
for r in results:
    print(r["content"], r["relevance_score"])
```

Returns the `results` list directly (empty list on no match).

### `insights(space_id, query) -> str | None`

```python theme={null}
answer = client.insights(
    space_id="spc_a1b2c3d4",
    query="What have we learned about Alice's career?"
)
print(answer)
```

Returns the synthesized answer string, or `None`.

### `list_spaces() -> list[dict]`

```python theme={null}
spaces = client.list_spaces()
for s in spaces:
    print(s["space_id"], s["name"])
```

***

## Async Support

Every method has an `async_` counterpart backed by a separate `httpx.AsyncClient`:

```python theme={null}
import asyncio
from anona import AnonaClient

async def main():
    client = AnonaClient(api_key="anona_live_YOUR_KEY")

    await client.async_add_memory(space_id="spc_a1b2c3d4", content="Alice works at Google")
    results = await client.async_search(space_id="spc_a1b2c3d4", query="Where does Alice work?")
    print(results)

    await client.aclose()

asyncio.run(main())
```

Use `async with AnonaClient(...) as client:` for automatic cleanup.

***

## Error Handling

```python theme={null}
from anona import AnonaClient, AnonaError

client = AnonaClient(api_key="anona_live_YOUR_KEY")

try:
    results = client.search(space_id="spc_a1b2c3d4", query="Where does Alice work?")
except AnonaError as e:
    print(e.status_code)  # int
    print(e.detail)       # parsed error body (dict) or raw text
```

***

## MCP Server

The SDK also ships an MCP server for connecting Anona Memory to Claude Code, Claude Desktop, and other MCP clients - see the [MCP guide](/mcp) for setup and usage.

***

## Next Steps

<CardGroup cols={2}>
  <Card title="MCP" icon="plug" href="/mcp">
    Connect to Claude Code and other MCP clients
  </Card>

  <Card title="API Reference" icon="rectangle-terminal" href="/api-reference/authentication">
    Full REST API documentation
  </Card>
</CardGroup>
