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

# AG2 (AutoGen)

> Persistent cross-session memory for AG2 agents via memanto_remember, memanto_recall, and memanto_answer tools.

# AG2 + Memanto

<img src="https://mintcdn.com/memanto/y3z1OQlaycNoW40F/logo/integrations/ag2.png?fit=max&auto=format&n=y3z1OQlaycNoW40F&q=85&s=78327b53e728f4552f70e22509e6a1d3" alt="AG2" width="64" style={{marginBottom: "1.5rem", background: "#fff", borderRadius: "8px"}} data-path="logo/integrations/ag2.png" />

[AG2](https://github.com/ag2ai/ag2) (the AutoGen-style multi-agent stack) agents normally forget context when a chat ends. The **`memanto-ag2`** package registers **Memanto memory tools** on your `AssistantAgent` and `UserProxyAgent` so the LLM can store and retrieve durable memory through [Moorcheh](https://moorcheh.ai) — the same **remember → recall → answer** pattern as other Memanto framework adapters.

<CardGroup cols={2}>
  <Card title="Drop-in tools" icon="wrench">
    `register_memanto_tools()` wires `memanto_remember`, `memanto_recall`, and `memanto_answer` in one call.
  </Card>

  <Card title="AG2-native" icon="code">
    Plain Python functions with `Annotated` hints for `register_for_llm` / `register_for_execution`.
  </Card>

  <Card title="Shared namespace" icon="users">
    One Memanto `agent_id` per team or app (like a memory bank); GroupChat agents can share it.
  </Card>

  <Card title="Selective tools" icon="sliders">
    Enable or disable remember, recall, or answer with `include_*` flags.
  </Card>
</CardGroup>

## How It Works

```text theme={null}
User message → UserProxyAgent → AssistantAgent (LLM)
                      ↑                    │
                      │         tool_call: memanto_remember | recall | answer
                      └──── execute tool ──┘
                                │
                                ▼
                         SdkClient → Moorcheh (persistent agent namespace)
```

Memanto does **not** replace AG2 or your LLM. The model **chooses** when to call memory tools (unlike [AgentCore](/integrations/agentcore), which recalls automatically before each handler turn).

| AG2 tool           | Memanto SDK  | Role                                                                   |
| ------------------ | ------------ | ---------------------------------------------------------------------- |
| `memanto_remember` | `remember()` | Store typed memories (preference, fact, event, …)                      |
| `memanto_recall`   | `recall()`   | Semantic search over stored memories                                   |
| `memanto_answer`   | `answer()`   | RAG-style synthesis (similar to “reflect” tools in other integrations) |

Memory is scoped by **`agent_id`** (e.g. `my-ag2-team`). Use the same id across chats and processes to share memory; use different ids to isolate tenants or teams.

## Prerequisites

* Python **3.10+**
* [Memanto](https://pypi.org/project/memanto/) and a [Moorcheh API key](https://console.moorcheh.ai/api-keys)
* **AG2 0.9.x** for runtime chat: `pip install "ag2>=0.9,<1"` (provides `from autogen import AssistantAgent`)
* An LLM API key your AG2 config uses (e.g. `OPENAI_API_KEY` for OpenAI)

<Warning>
  Install **`ag2`** (AI agents), not **`a2g`** — an unrelated PyPI package. Do not use **`ag2` 1.x** with this adapter yet; it uses a different API. Pin **`ag2>=0.9,<1`**.
</Warning>

## Install

<Steps>
  <Step title="Install the adapter">
    ```bash theme={null}
    pip install memanto-ag2
    pip install "ag2>=0.9,<1"   # AG2 runtime only — not required for unit/smoke tests
    ```

    **`memanto-ag2` installs `memanto` for you** via its package dependencies. Use `pip install memanto memanto-ag2` only if you want to pin or upgrade `memanto` explicitly.
  </Step>

  <Step title="Configure Moorcheh">
    ```bash theme={null}
    export MOORCHEH_API_KEY=your_key_xxxxxxxxxxxxxxxxxx
    ```

    Or run `memanto` once to write `~/.memanto/.env`.
  </Step>
</Steps>

## Register tools on your agents

```python theme={null}
import os
from autogen import AssistantAgent, UserProxyAgent
from memanto.cli.client.sdk_client import SdkClient
from memanto_ag2 import openai_llm_config, register_memanto_tools

llm_config = openai_llm_config("gpt-4o-mini")

assistant = AssistantAgent(
    name="assistant",
    llm_config=llm_config,
    system_message=(
        "You have memanto_remember, memanto_recall, and memanto_answer. "
        "Use memanto_remember when the user asks you to store something."
    ),
)
user_proxy = UserProxyAgent(name="user", human_input_mode="NEVER")

client = SdkClient(api_key=os.environ["MOORCHEH_API_KEY"])
register_memanto_tools(
    assistant,
    executor=user_proxy,
    client=client,
    agent_id="my-ag2-team",
)

user_proxy.initiate_chat(
    assistant,
    message="Remember that I prefer Python over JavaScript.",
    max_turns=2,
)
```

### GroupChat with shared memory

Register the same `agent_id` on every assistant that should read or write shared memory; use one executor (`UserProxyAgent`) to run tool calls:

```python theme={null}
executor = UserProxyAgent(name="executor", human_input_mode="NEVER")
for agent in [researcher, writer]:
    register_memanto_tools(agent, executor=executor, client=client, agent_id="team-memory")
```

### Global defaults

```python theme={null}
from memanto_ag2 import configure, register_memanto_tools

configure(source="ag2-prod", recall_limit=15)
register_memanto_tools(assistant, executor=user_proxy, agent_id="my-bank")
```

## Verify persistence

1. Run a chat that calls **`memanto_remember`** (check AG2 logs for `EXECUTING FUNCTION memanto_remember` and a Memanto memory ID).
2. Start a **new Python process** with the same **`agent_id`**.
3. Ask the assistant to **`memanto_recall`** or **`memanto_answer`** — prior facts should appear even though AG2 chat history is empty.

From the Memanto repo:

```bash theme={null}
export MOORCHEH_API_KEY=your_key
python integrations/ag2/scripts/smoke_test.py
```

Steps 1–5 use the API only. Set `OPENAI_API_KEY` and install `ag2` for optional Step 6 (live AG2 chat).

Unit tests (no API key):

```bash theme={null}
cd integrations/ag2
PYTHONPATH=memanto_ag2:.. python -m pytest tests/test_tools.py -q
```

## AgentCore vs AG2

|          | [AgentCore](/integrations/agentcore)      | AG2 (this page)               |
| -------- | ----------------------------------------- | ----------------------------- |
| Trigger  | Automatic recall/retain each handler turn | LLM invokes tools when needed |
| Identity | `tenant` + `user_id` + `agent_name`       | You choose `agent_id`         |
| Best for | Bedrock AgentCore Runtime handlers        | AutoGen/AG2 chats, GroupChat  |

## Shared memory across integrations

All Memanto integration packages use the same Moorcheh-backed agents when they share an `agent_id`:

| Integration                                         | Package             | What it does                                          |
| --------------------------------------------------- | ------------------- | ----------------------------------------------------- |
| **AG2**                                             | `memanto-ag2`       | AG2 tool registration for remember / recall / answer. |
| [`integrations/agentcore`](/integrations/agentcore) | `memanto-agentcore` | Recall/retain wrapper for Bedrock AgentCore Runtime.  |
| [`integrations/crewai`](/integrations/crewai)       | `crewai-memanto`    | CrewAI tools for multi-agent memory.                  |
| [`integrations/langgraph`](/integrations/langgraph) | `langgraph-memanto` | LangGraph `BaseStore`, nodes, and tools.              |
| [`integrations/mcp`](/integrations/mcp)             | `memanto-mcp`       | MCP server for Claude Desktop, Cursor, etc.           |

## Next Steps

* [AG2 integration source & README](https://github.com/moorcheh-ai/memanto/tree/main/integrations/ag2)
* [AG2 project](https://github.com/ag2ai/ag2)
* [Remember API](/api-reference/data/remember)
* [Recall API](/api-reference/search/recall)
