Skip to content

ant_ai.tools.builtins.memory_tool

MemoryTool pydantic-model

Bases: Memory, Tool

Base class for long-term memory backends directly usable as an agent Tool.

Subclass and implement retrieve/update (the Memory protocol, unchanged) to connect a backend — search/add below are the LLM-facing tools built automatically on top of it, registered as <ClassName>_search / <ClassName>_add.

Example
from ant_ai.agent import Agent
from ant_ai.memory.backends.mem0 import Mem0Memory

agent = Agent(memory=Mem0Memory(), ...)

The agent gets Mem0Memory_search/Mem0Memory_add automatically — the LLM decides when to call them.

Notes

ctx is injected automatically by ToolStep from the current InvocationContext and is never exposed to the LLM.

Show JSON schema:
{
  "description": "Base class for long-term memory backends directly usable as an agent Tool.\n\nSubclass and implement `retrieve`/`update` (the `Memory` protocol,\nunchanged) to connect a backend \u2014 `search`/`add` below are the\nLLM-facing tools built automatically on top of it, registered as\n`<ClassName>_search` / `<ClassName>_add`.\n\nExample:\n    ```python\n    from ant_ai.agent import Agent\n    from ant_ai.memory.backends.mem0 import Mem0Memory\n\n    agent = Agent(memory=Mem0Memory(), ...)\n    ```\n\n    The agent gets `Mem0Memory_search`/`Mem0Memory_add` automatically \u2014\n    the LLM decides when to call them.\n\nNotes:\n    `ctx` is injected automatically by `ToolStep` from the current\n    `InvocationContext` and is never exposed to the LLM.",
  "properties": {
    "name": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "description": "Tool name.",
      "title": "Name"
    },
    "description": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "description": "Tool description. Is used by the LLM to decide whether to call or not the specific tool.",
      "title": "Description"
    },
    "parameters": {
      "anyOf": [
        {
          "additionalProperties": true,
          "type": "object"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "description": "The parameters needed by the tool. This is a self-constructed field.",
      "title": "Parameters"
    }
  },
  "title": "MemoryTool",
  "type": "object"
}

Fields:

Validators:

  • _set_defaults
Source code in src/ant_ai/tools/builtins/memory_tool.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class MemoryTool(Memory, Tool):
    """
    Base class for long-term memory backends directly usable as an agent Tool.

    Subclass and implement `retrieve`/`update` (the `Memory` protocol,
    unchanged) to connect a backend — `search`/`add` below are the
    LLM-facing tools built automatically on top of it, registered as
    `<ClassName>_search` / `<ClassName>_add`.

    Example:
        ```python
        from ant_ai.agent import Agent
        from ant_ai.memory.backends.mem0 import Mem0Memory

        agent = Agent(memory=Mem0Memory(), ...)
        ```

        The agent gets `Mem0Memory_search`/`Mem0Memory_add` automatically —
        the LLM decides when to call them.

    Notes:
        `ctx` is injected automatically by `ToolStep` from the current
        `InvocationContext` and is never exposed to the LLM.
    """

    async def search(
        self, query: str, ctx: InvocationContext | None = None
    ) -> list[str]:
        """Search long-term memory for facts relevant to `query`. Call this
        whenever recalling something about the user or a past conversation
        would help answer the current request."""
        messages: list[Message] = await self.retrieve(query, ctx=ctx)
        return [m.content for m in messages if m.content]

    async def add(self, facts: list[str], ctx: InvocationContext | None = None) -> str:
        """Persist one or more durable facts (user preferences, personal
        details, explicit "remember this" instructions) for future
        conversations. Call this proactively as soon as you learn something
        worth keeping — do not wait to be asked, and do not wait until the
        end of the conversation."""
        if not facts:
            return "Nothing to save."
        await self.update([Message(role="system", content=f) for f in facts], ctx=ctx)
        return f"Saved {len(facts)} fact(s) to memory."

search async

search(
    query: str, ctx: InvocationContext | None = None
) -> list[str]

Search long-term memory for facts relevant to query. Call this whenever recalling something about the user or a past conversation would help answer the current request.

Source code in src/ant_ai/tools/builtins/memory_tool.py
34
35
36
37
38
39
40
41
async def search(
    self, query: str, ctx: InvocationContext | None = None
) -> list[str]:
    """Search long-term memory for facts relevant to `query`. Call this
    whenever recalling something about the user or a past conversation
    would help answer the current request."""
    messages: list[Message] = await self.retrieve(query, ctx=ctx)
    return [m.content for m in messages if m.content]

add async

add(
    facts: list[str], ctx: InvocationContext | None = None
) -> str

Persist one or more durable facts (user preferences, personal details, explicit "remember this" instructions) for future conversations. Call this proactively as soon as you learn something worth keeping — do not wait to be asked, and do not wait until the end of the conversation.

Source code in src/ant_ai/tools/builtins/memory_tool.py
43
44
45
46
47
48
49
50
51
52
async def add(self, facts: list[str], ctx: InvocationContext | None = None) -> str:
    """Persist one or more durable facts (user preferences, personal
    details, explicit "remember this" instructions) for future
    conversations. Call this proactively as soon as you learn something
    worth keeping — do not wait to be asked, and do not wait until the
    end of the conversation."""
    if not facts:
        return "Nothing to save."
    await self.update([Message(role="system", content=f) for f in facts], ctx=ctx)
    return f"Saved {len(facts)} fact(s) to memory."