# AutoGen

Adding persistent memory to Microsoft AutoGen agents with the SurrealDB Agent Memory SDK.

[AutoGen](https://microsoft.github.io/autogen/) builds conversational and multi-agent systems in Python. SurrealDB Agent Memory gives those agents long-term memory: recall relevant facts before a turn and store new ones after. There is no dedicated adapter. The [Python SDK](/docs/agent-memory/integrations/sdks/python.md) (`surrealdb`) exposes the memory operations you register as agent tools.

> [!NOTE]
> This is an integration guide. It wires the SurrealDB Agent Memory SDK into AutoGen's tool interface; adapt the function-registration calls to your installed AutoGen version.

## Installation

> [!NOTE]
> `Spectron` was the project name for SurrealDB Agent Memory. These type names
> will be renamed in a future release.

```bash
pip install autogen-agentchat 'autogen-ext[openai]'
pip install --pre surrealdb
```

```bash
export SPECTRON_ENDPOINT="https://api.spectron.example"
export SPECTRON_CONTEXT="acme-prod"
export SPECTRON_API_KEY="sk-spec-..."
```

## Memory as agent tools

Wrap the SurrealDB Agent Memory client in plain functions and pass them to the agent. AutoGen calls them like any other tool:

```python
import os
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
from surrealdb import Spectron

memory = Spectron(
    endpoint=os.environ["SPECTRON_ENDPOINT"],
    context=os.environ["SPECTRON_CONTEXT"],
    api_key=os.environ["SPECTRON_API_KEY"],
)
scope = ["org/acme/user/alice"]

def remember(text: str) -> str:
    """Store a durable fact for later recall."""
    memory.remember(text, scopes=scope)
    return "stored"

def recall(query: str) -> str:
    """Retrieve relevant memory for a query."""
    return memory.query_context(query, k=8, lens=scope)

agent = AssistantAgent(
    name="assistant",
    model_client=OpenAIChatCompletionClient(model="gpt-4o"),
    tools=[remember, recall],
    system_message="Use recall before answering and remember anything worth keeping.",
)
```

## Recall around a run

To keep memory out of the agent's tool list, recall before the run and inject the context into the system message, then store the exchange yourself:

```python
block = memory.query_context(user_message, k=8, lens=scope)
agent = AssistantAgent(
    name="assistant",
    model_client=OpenAIChatCompletionClient(model="gpt-4o"),
    system_message=f"You are a helpful assistant.\n\n## Memory\n{block}",
)
# after the run
memory.remember_many(
    [{"role": "user", "content": user_message}, {"role": "assistant", "content": reply}],
    scopes=scope,
)
```

## Scope per user

Pass a `scope` on every call to isolate memory. A scope is a slash path or an array of paths, for example `["org/acme/user/alice"]`. Register paths with `spectron scopes create` before first use.

## Next steps

- [Python SDK](/docs/agent-memory/integrations/sdks/python.md): the full client surface
- [MCP server](/docs/agent-memory/integrations/mcp-server/install.md): if your host speaks MCP instead
