ant_ai.memory.backends.file
FileMemory
pydantic-model
Bases: Memory
Long-term memory kept in one Markdown file, which the agent reads and writes whole.
Unlike Mem0Memory it needs no service: the file can be read and edited by
hand, kept in git, and used offline and in tests.
Example
from ant_ai.agent import Agent
from ant_ai.memory.backends.file import FileMemory
agent = Agent(memory=FileMemory(path="./agent_state/MEMORY.md"), ...)
The agent can now call:
- FileMemory_read()
- FileMemory_write(content="# Memory\n\n- The user prefers short answers\n")
Notes
A write replaces the whole memory, so it is refused unless the
conversation has read the memory as it is now: a model cannot erase what
it has not seen, or undo a change made since it read, by another
conversation or by hand. A write longer than max_chars is refused too,
asking the model to shorten the memory.
The file and its parent directories are created on the first write, and
each write replaces the file at once, so a crash never leaves half of it.
One file is shared by every conversation and user of the memory, whatever
ctx says: give each user their own agent and path when their memories
must stay apart.
Writes take turns across threads and, on POSIX, processes, by locking
the file's directory; an edit by hand during a write is not stopped.
Failures raise ToolError, which ToolStep turns into a recoverable
ERROR: ... message for the agent.
Show JSON schema:
{
"description": "Long-term memory kept in one Markdown file, which the agent reads and writes whole.\n\nUnlike `Mem0Memory` it needs no service: the file can be read and edited by\nhand, kept in git, and used offline and in tests.\n\nExample:\n ```python\n from ant_ai.agent import Agent\n from ant_ai.memory.backends.file import FileMemory\n\n agent = Agent(memory=FileMemory(path=\"./agent_state/MEMORY.md\"), ...)\n ```\n\n The agent can now call:\n\n - FileMemory_read()\n - FileMemory_write(content=\"# Memory\\n\\n- The user prefers short answers\\n\")\n\nNotes:\n A write replaces the whole memory, so it is refused unless the\n conversation has read the memory as it is now: a model cannot erase what\n it has not seen, or undo a change made since it read, by another\n conversation or by hand. A write longer than `max_chars` is refused too,\n asking the model to shorten the memory.\n The file and its parent directories are created on the first write, and\n each write replaces the file at once, so a crash never leaves half of it.\n One file is shared by every conversation and user of the memory, whatever\n `ctx` says: give each user their own agent and path when their memories\n must stay apart.\n Writes take turns across threads and, on POSIX, processes, by locking\n the file's directory; an edit by hand during a write is not stopped.\n Failures raise `ToolError`, which `ToolStep` turns into a recoverable\n `ERROR: ...` message for the agent.",
"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"
},
"path": {
"description": "The memory file, e.g. `./MEMORY.md`.",
"format": "path",
"title": "Path",
"type": "string"
},
"max_chars": {
"anyOf": [
{
"minimum": 1,
"type": "integer"
},
{
"type": "null"
}
],
"default": 10000,
"description": "Longest memory `write` accepts, or None for no limit.",
"title": "Max Chars"
}
},
"required": [
"path"
],
"title": "FileMemory",
"type": "object"
}
Fields:
-
name(str | None) -
description(str | None) -
parameters(dict[str, Any] | None) -
__namespace_methods__(list[str]) -
path(Path) -
max_chars(int | None)
Validators:
-
_set_defaults
Source code in src/ant_ai/memory/backends/file.py
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
path
pydantic-field
path: Path
The memory file, e.g. ./MEMORY.md.
max_chars
pydantic-field
max_chars: int | None = 10000
Longest memory write accepts, or None for no limit.
read
read(ctx: InvocationContext | None = None) -> str
Read your long-term memory: what was saved in earlier conversations. Call it at the start of a conversation, and before answering anything that may depend on what you already know about the user or the task.
Source code in src/ant_ai/memory/backends/file.py
73 74 75 76 77 | |
write
write(
content: str, ctx: InvocationContext | None = None
) -> str
Replace your whole long-term memory with content (Markdown); read it first. To save a new fact, write the memory with the fact added; to forget one, write it without. Save durable facts (preferences, decisions, explicit "remember this" requests) as soon as you learn them, and keep the memory short.
Source code in src/ant_ai/memory/backends/file.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |