Skip to content

ant_ai.memory.protocol

Memory pydantic-model

Bases: Tool

Base class for an agent's long-term memory, kept across conversations.

A memory is a tool namespace: its public methods are the tools the model calls to recall and save what it knows, registered as <ClassName>_<method> when the memory is set as Agent(memory=...). Each backend picks the tools that fit its storage: Mem0Memory searches and adds facts, FileMemory reads and writes one Markdown file.

Example
from ant_ai.core.types import InvocationContext
from ant_ai.memory import Memory


class NotesMemory(Memory):
    notes: list[str] = []

    def recall(self, ctx: InvocationContext | None = None) -> list[str]:
        # The docstring of a method is the tool description the model reads.
        return self.notes

    def save(self, note: str, ctx: InvocationContext | None = None) -> str:
        self.notes.append(note)
        return "Saved."
Notes

A method that declares ctx receives the current InvocationContext (e.g. ctx.user_id, to keep each user's memories apart); it is never shown to the model.

Show JSON schema:
{
  "description": "Base class for an agent's long-term memory, kept across conversations.\n\nA memory is a tool namespace: its public methods are the tools the model\ncalls to recall and save what it knows, registered as `<ClassName>_<method>`\nwhen the memory is set as `Agent(memory=...)`. Each backend picks the tools\nthat fit its storage: `Mem0Memory` searches and adds facts, `FileMemory` reads\nand writes one Markdown file.\n\nExample:\n    ```python\n    from ant_ai.core.types import InvocationContext\n    from ant_ai.memory import Memory\n\n\n    class NotesMemory(Memory):\n        notes: list[str] = []\n\n        def recall(self, ctx: InvocationContext | None = None) -> list[str]:\n            # The docstring of a method is the tool description the model reads.\n            return self.notes\n\n        def save(self, note: str, ctx: InvocationContext | None = None) -> str:\n            self.notes.append(note)\n            return \"Saved.\"\n    ```\n\nNotes:\n    A method that declares `ctx` receives the current `InvocationContext`\n    (e.g. `ctx.user_id`, to keep each user's memories apart); it is never\n    shown to the model.",
  "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": "Memory",
  "type": "object"
}

Fields:

Validators:

  • _set_defaults
Source code in src/ant_ai/memory/protocol.py
 6
 7
 8
 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
class Memory(Tool):
    """
    Base class for an agent's long-term memory, kept across conversations.

    A memory is a tool namespace: its public methods are the tools the model
    calls to recall and save what it knows, registered as `<ClassName>_<method>`
    when the memory is set as `Agent(memory=...)`. Each backend picks the tools
    that fit its storage: `Mem0Memory` searches and adds facts, `FileMemory` reads
    and writes one Markdown file.

    Example:
        ```python
        from ant_ai.core.types import InvocationContext
        from ant_ai.memory import Memory


        class NotesMemory(Memory):
            notes: list[str] = []

            def recall(self, ctx: InvocationContext | None = None) -> list[str]:
                # The docstring of a method is the tool description the model reads.
                return self.notes

            def save(self, note: str, ctx: InvocationContext | None = None) -> str:
                self.notes.append(note)
                return "Saved."
        ```

    Notes:
        A method that declares `ctx` receives the current `InvocationContext`
        (e.g. `ctx.user_id`, to keep each user's memories apart); it is never
        shown to the model.
    """