> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# SurrealDB for Agent

> Claude agent persists sessions in SurrealDB over a WebSocket connection.

````python surrealdb_for_agent.py theme={null}
r"""
Run SurrealDB in a container before running this script

```
docker run --rm --pull always -p 8000:8000 surrealdb/surrealdb:latest start --user root --pass root
```

or with

```
surreal start -u root -p root
```

Then, run this test like this:

```
uv run cookbook/06_storage/surrealdb/surrealdb_for_agent.py
```
"""

from agno.agent import Agent
from agno.db.surrealdb import SurrealDb
from agno.models.anthropic import Claude
from agno.tools.websearch import WebSearchTools

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
SURREALDB_URL = "ws://localhost:8000"
SURREALDB_USER = "root"
SURREALDB_PASSWORD = "root"
SURREALDB_NAMESPACE = "agno"
SURREALDB_DATABASE = "surrealdb_for_agent"

creds = {"username": SURREALDB_USER, "password": SURREALDB_PASSWORD}
db = SurrealDb(None, SURREALDB_URL, creds, SURREALDB_NAMESPACE, SURREALDB_DATABASE)

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    db=db,
    model=Claude(id="claude-sonnet-4-5-20250929"),
    tools=[WebSearchTools()],
    add_history_to_context=True,
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    agent.print_response("How many people live in Costa Rica?")
    agent.print_response("What is their national anthem called?")
````

## Run the Example

<Snippet file="create-venv-step.mdx" />

**1. Install dependencies**

```bash theme={null}
uv pip install -U agno anthropic ddgs surrealdb
```

**2. Export your Anthropic API key**

```bash Mac/Linux theme={null}
export ANTHROPIC_API_KEY="your_anthropic_api_key_here"
```

```bash Windows theme={null}
$Env:ANTHROPIC_API_KEY="your_anthropic_api_key_here"
```

**3. Run SurrealDB**

```bash theme={null}
docker run -d --rm --name surrealdb --pull always -p 8000:8000 surrealdb/surrealdb:latest start --user root --pass root
```

**4. Run the example**

Save the code above as `surrealdb_for_agent.py`, then run:

```bash theme={null}
python surrealdb_for_agent.py
```

Full source: [cookbook/06\_storage/surrealdb/surrealdb\_for\_agent.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/06_storage/surrealdb/surrealdb_for_agent.py)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.