Give an Agent Long-Term Memory
An agent memory that keeps the current answer, the history it replaced, and the evidence behind each claim, in a local file or on a server
The Result
An agent records what it learns as claims with evidence. Recall returns the current answer, and a dated recall returns what was true then, even when the older runbook was imported last:
curl -X POST http://127.0.0.1:8080/memory/v1/spaces/agent/recall \
-H 'Content-Type: application/json' \
-d '{"subject": "production", "valid_at": 1769904000000, "token_budget": 4096}'
{
"items": [
{
"memory": {
"assertion": {"id": "deploy-helm", "value": "Helm",
"valid_from": 1767571200000, "valid_to": 1772409600000},
"lifecycle": "superseded",
"superseded_by": "deploy-terraform"
},
"historical": true,
"citations": [{"id": "runbook-2026-01", "span": null}]
}
],
"complete": true,
"revision": 4
}
Before You Start
Antfly running in standalone mode. No model is needed: the agent supplies its claims, and Antfly orders and reconciles them.
curl -s http://127.0.0.1:8080/memory/v1/spaces/agent
A 404 with "code": "TableNotFound" means the server is up and the space doesn't exist yet.
Build It
1. Create a Memory Space
A space holds one agent's memory. predicates declares the relationships the agent records; cardinality: "one" means a subject has one current value, so a newer claim replaces an older one. Every write takes an Idempotency-Key, so a retried request is applied once.
curl -X POST http://127.0.0.1:8080/memory/v1/spaces \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: create-agent' \
-d '{
"space": "agent",
"config": {
"features": {"reconciliation": "deterministic"},
"predicates": [{"name": "deploys_with", "cardinality": "one"}]
}
}'
{"action": "create", "phase": "succeeded", "revision": 2}
2. Record Claims With Their Evidence
Each observation pairs evidence (an immutable source version) with assertions that cite it. valid_from is when the claim became true in the world, in milliseconds since the Unix epoch. Record the March runbook first:
curl -X POST http://127.0.0.1:8080/memory/v1/spaces/agent/observations \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: runbook-2026-03' \
-d '{
"evidence": [{"id": "runbook-2026-03", "source_id": "deployment-runbook",
"source_version": "2026-03", "text": "Production deploys with Terraform."}],
"assertions": [{"id": "deploy-terraform", "subject": "production",
"predicate": "deploys_with", "value": "Terraform",
"statement": "Production deploys with Terraform.",
"valid_from": 1772409600000,
"evidence": [{"id": "runbook-2026-03"}]}]
}'
{"action": "observe", "phase": "succeeded", "revision": 3, "evidence_accepted": true, "assertions_ready": true}
Then import the older January runbook, the way a backfill would. Repeat the call with ids runbook-2026-01 and deploy-helm, the value Helm, and "valid_from": 1767571200000.
3. Recall the Current Answer
curl -X POST http://127.0.0.1:8080/memory/v1/spaces/agent/recall \
-H 'Content-Type: application/json' \
-d '{"subject": "production", "token_budget": 4096}'
{
"items": [
{
"memory": {
"assertion": {"id": "deploy-terraform", "value": "Terraform",
"valid_from": 1772409600000, "valid_to": null},
"lifecycle": "active",
"superseded_by": null
},
"historical": false,
"citations": [{"id": "runbook-2026-03", "span": null}]
}
],
"complete": true,
"revision": 4
}
Terraform stays current although Helm arrived later, because validity time decides, not import order. The Helm claim is kept as superseded, ending where Terraform begins. Recall with valid_at returns it, as in The Result. Add "include_history": true to get both in one response.
4. Explain a Memory
curl -X POST http://127.0.0.1:8080/memory/v1/spaces/agent/memories/deploy-helm/explain \
-H 'Content-Type: application/json' -d '{}'
The response is a graph of nodes: the assertion, its evidence, the recorded decisions that reconciled it, and the deploy-terraform assertion that replaced it. complete and missing_evidence say whether the chain is whole. GET .../memories/deploy-helm/history returns every recorded revision.
5. Record What Happened
Close the loop by telling Antfly which memories a task used and whether it worked, so later reviews can see which memories led to good results.
curl -X POST http://127.0.0.1:8080/memory/v1/spaces/agent/outcomes \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: release-42' \
-d '{"id": "release-42", "task_id": "deploy-release-42", "success": true,
"used_memory_ids": ["deploy-terraform"], "detail": "terraform apply succeeded"}'
{"action": "outcome", "phase": "succeeded", "revision": 7}
Outcomes feed back into ranking only where you ask for it. Recall with "utility_weight": 0.5 (0 to 1) adds that weight times the memory's outcome utility, (successes - failures) / (successes + failures + 2), to its score. The default of 0 leaves ranking unchanged. Utility counts only retained outcomes the caller is allowed to read, in the selected branch. Forgetting any memory used by an outcome immediately excludes that outcome from every utility score. Historical recall excludes outcomes recorded after known_at. Utility reads at most 64 outcomes per memory from your own feedback; administrators use an all-owner view. Other users’ private outcomes do not consume your limit. Existing spaces backfill owner indexes through bounded background maintenance, reporting utility.complete=false until that migration finishes.
6. Load a Session Brief
Memory works in layers: evidence is the transcript, claims are the agent's notes, and the brief is the small slice injected at the start of every session. Anything loaded every session costs tokens and spreads any error it contains, so the brief stays small and points to deeper recall instead of repeating it.
curl -s 'http://127.0.0.1:8080/memory/v1/spaces/agent/brief?limit=8&token_budget=1024'
{
"items": [
{"id": "deploy-terraform", "statement": "Production deploys with Terraform.",
"kind": "semantic", "subject": "production", "predicate": "deploys_with",
"stability": "settled", "pinned": false, "score": 2.67,
"utility": {"successes": 1, "failures": 0, "score": 0.33, "complete": true}}
],
"subjects": [{"subject": "production", "memories": 1}],
"guidance": "These are the memories most worth knowing at the start of a session. ...",
"complete": true,
"token_upper_bound": 512
}
The brief holds current, active claims only. Disputed claims stay out unless pinned. Provisional claims stay out unless pinned or you pass include_provisional. Constraints and preferences rank first, then procedures and lessons, then decisions and plain facts. Settled claims and claims with good outcomes rank higher, and restated duplicates collapse to one item. subjects lists what else the space knows about, so the agent can recall it on demand. scope, task and environment narrow the brief to one context. The whole rendered response, not just the items, fits within token_budget bytes.
Pin a memory to prioritize it ahead of unpinned memories, within the brief’s item and byte limits:
curl -X POST http://127.0.0.1:8080/memory/v1/spaces/agent/pins -H 'Content-Type: application/json' -H 'Idempotency-Key: pin-terraform' -d '{"id": "deploy-terraform", "reason": "Every deploy task needs this"}'
Send "pinned": false to unpin, including after the evidence expires. Pins belong to the selected branch and are not inherited by forks. Pinning a team-visible memory changes every principal's brief, so it requires curator access.
7. Review Memory Health
Memory degrades without upkeep. Notes can be wrong when written, facts go stale, and duplicates pile up. A consolidation review reads the space and reports what needs attention, without changing anything:
curl -X POST http://127.0.0.1:8080/memory/v1/spaces/agent/consolidation -H 'Content-Type: application/json' -d '{"limit": 32, "stale_after_ms": 2592000000}'
{
"findings": [
{"kind": "unverified", "memory_ids": ["owner-guess"], "entity_id": null, "target_id": null,
"reason": "provisional or inferred claim; check it against its evidence and settle or retract it",
"suggested_action": "verify"}
],
"stats": {"memories": 2, "by_kind": [{"name": "semantic", "memories": 2}], "pinned": 0, "entities": 0},
"complete": true
}
Follow next_cursor until complete is true, including pages with no findings. Keep stale_after_ms unchanged across pages and sum each page's statistics; writes invalidate the cursor.
cursor = None
while True:
page = client.memory.consolidate("agent", cursor=cursor, limit=32)
for finding in page["findings"]:
print(finding["kind"], finding["memory_ids"])
if page["complete"]:
break
cursor = page["next_cursor"]
A curator can create reviewable work with memory.prepare_consolidation("agent", idempotency_key="review-2026-01"). The receipt contains proposal_ids for the supersession verdict flow and review_cursor for the next preparation request. Preparation returns only open proposals whose pins match current authorized views, and refreshes stale work while retaining its audit history. Continue review_cursor even when a page returns no proposal IDs. Proposal creation leaves beliefs unchanged. Verifying a claim with correct to active/settled records last_verified_at and resets staleness.
After consuming recall or brief results, call memory.access("agent", ["deploy-helm"], idempotency_key="session-42-recall-1"). Reuse the key on retries. memory.usage("agent", "deploy-helm") returns your branch-local count, last access, verification time and utility. Reads themselves do not create access events. Inspection includes paged statistics for retained space growth; continue with memory.execute("agent", "inspect", body={"cursor": next_cursor}) and sum counts.
Retrieval evaluation cases compare policy.retrieval="recall", "brief" or "none". Brief cases accept session-brief filters and budgets; no-memory cases use zero context tokens.
Each finding carries a suggested_action that maps to an existing reviewed write:
| Finding | Meaning | Suggested action |
|---|---|---|
duplicate | Active claims with the same subject, predicate, applicability and value | propose_supersession with a restates verdict |
dispute | Conflicting current claims | review_supersession |
unordered | Single-valued claim without valid_from | add_valid_from |
unverified | Provisional, inferred or hypothesized claim | verify, then correct to settle or retract |
stale | Single-valued or provisional claim not verified within stale_after_ms | verify |
harmful | Used mostly in failed tasks | correct |
dangling_link | Links to a memory that was retired | fix_link |
orphan_entity | Entity no current claim mentions | curate_entity |
promote | Settled durable or repeatedly useful claim | pin |
Run the review on a schedule, or after a burst of writes, and act on findings through a person or a judge model. Similarity never changes a memory by itself. stats counts memories by kind, lifecycle, stability and producer. A sharp rise in claims from one producer usually means an agent is logging everything instead of what matters.
Run It Embedded
The same actions run in-process on a .aflite file through the embedded bindings, with the action name in the request:
import antfly_embedded
with antfly_embedded.create("agent.aflite") as db:
def memory(action, body=None, key=""):
return db.memory("agent", {"action": action, "body": body or {}, "idempotency_key": key})
memory("create", {"features": {"reconciliation": "deterministic"},
"predicates": [{"name": "deploys_with", "cardinality": "one"}]}, "create-agent")
# observe, recall, explain and outcome take the same bodies as above.
print(memory("recall", {"subject": "production", "token_budget": 4096}))
Against a server, the Python SDK uses the same action names: AntflyClient(url).memory.execute("agent", "recall", body={...}), plus helpers such as memory.brief("agent") and memory.recall("agent", query=...) that generate idempotency keys for you.
For Vercel AI SDK agents, @antfly/ai-sdk exports createMemorySession. Give it
the authenticated client, fixed space/catalog scope, a globally unique host
runId, and brief/recall budget ceilings. loadBrief() returns fresh bounded
context; tools opts into the native eight-tool agent profile. After consuming
memory, the host calls recordAccess(ids, {eventId}) and reports its actual task
result with recordOutcome(...). Stable run, tool-call and event IDs deduplicate
retries across restarts. The existing createMemoryTools default keeps its three
tools; pass profile: "agent" to opt in. Consolidation and curator review remain
explicit application/operator calls, and transcript ingestion is never implicit.
Connect an Agent over MCP
Point an agent at /memory/v1/mcp/agent. It lists a small toolset: memory_brief, memory_recall, memory_observe, memory_correct, memory_forget, memory_outcome, memory_get and memory_explain. The tool descriptions say what memory is for rather than prescribing rules. Writes on this endpoint don't need an idempotency_key: when one is omitted, Antfly derives it from the tool arguments, so an identical retry replays the first result. Operators and curators use /memory/v1/mcp, which lists every action, including proposals, lineage, evaluation, pins and consolidation. The full contract, covering evidence and temporal policy, identity and supersession judging, provenance, and recorded explanations, is in MEMORY.md.
Tradeoffs
Keep the brief small. If an agent needs more than a screen of context every session, pin less and let it recall the rest. Consolidation reports are suggestions: an inferred claim may be right, and a duplicate may be an intended restatement in a different scope.
The decision you own is which claims are facts with one current value. Declare a predicate with cardinality: "one" only when a newer claim should replace the old one; without it, both values stay active and recall returns both. Give every claim a real valid_from. A claim with no validity time can't be ordered against the others, so ingestion order never decides which is current.
Evidence IDs name immutable source versions. When a runbook changes, record the new version under a new evidence ID rather than editing the old one, so explanations keep pointing at what the agent actually read.
This guide has the agent write its own claims. Spaces can also extract and judge claims with a model; Codex on Antfly does that, building a knowledge graph of decisions and lessons from each session. It also records each tool-approval verdict with its outcome so later approvals can follow precedent, indexes session history for search, and runs embedded or against a server from one setting.
Use Agent Skills
Everything above is also encoded in the Antfly skill, so a coding agent can execute this guide for you:
npx skills add antflydb/antfly-skills
Then prompt it with the outcome, for example "give my agent an Antfly memory space that records deployment guidance with sources and answers what was true at any date", and use this page to judge the result.
Next Steps
- Antfly Lite to ship the memory inside your agent as a single file.
- Laya typed decisions when the agent also needs to make and record yes or no calls.
- Use Antfly from n8n for connecting an agent over MCP.