| Memory | R Documentation |
A 'Memory' stores what agents learned from past tasks so that later tasks can benefit from it, within a session or across sessions when a 'path' is given. It holds two kinds of records:
* **Lessons**: short, reusable strategy notes distilled by an LLM from a judged task ("for X, doing Y worked" / "avoid Z"). Relevant lessons are retrieved and added to the prompt of future tasks. * **Outcomes**: one record per judged task (agent, task, success, score). They form each agent's track record, which a 'LeadAgent' uses when choosing which agent should handle a task.
Attach a memory with $set_memory() on an 'Agent' or a 'LeadAgent'.
Several agents can share the same 'Memory' object.
Retrieval ranks lessons by keyword relevance (BM25) to the task at hand, with no extra dependency. Supply 'search_fn' to plug in another retriever, e.g. one based on embeddings.
path'[character(1) | NULL]' JSON file the memory is saved to after every change. 'NULL' keeps the memory in the current session only.
lessonsA list of lesson records ('id', 'lesson', 'task', 'agent_name', 'score', 'success', 'created_at').
outcomesA list of outcome records ('agent_name', 'task', 'success', 'score', 'source', 'created_at').
max_lessonsMaximum number of lessons kept (oldest are dropped first).
max_outcomesMaximum number of outcomes kept (oldest are dropped first).
new()Create a memory, loading it from 'path' when that file exists.
Memory$new( path = NULL, max_lessons = 500, max_outcomes = 1000, search_fn = NULL )
path'[character(1) | NULL]' JSON file to persist the memory to. Defaults to 'NULL' (in-session only).
max_lessons'[integerish(1)]' Maximum number of lessons kept. Default 500.
max_outcomes'[integerish(1)]' Maximum number of outcomes kept. Default 1000.
search_fn'[function | NULL]' Optional custom retriever with signature 'function(query, lessons, k)' returning a list of at most 'k' lessons taken from 'lessons', most relevant first.
add_lesson()Store a lesson.
Memory$add_lesson( lesson, task = NA_character_, agent_name = NA_character_, score = NA_real_, success = NA )
lesson'[character(1)]' The lesson text.
task'[character(1)]' The task the lesson was learned from.
agent_name'[character(1)]' Name of the agent that performed the task.
score'[numeric(1)]' Judge score of the task (0 to 1), 'NA' if unknown.
success'[logical(1)]' Whether the task succeeded, 'NA' if unknown.
Invisibly, the stored lesson record.
add_outcome()Record the outcome of a task.
Memory$add_outcome( agent_name, task, success, score = NA_real_, source = "validation" )
agent_name'[character(1)]' Name of the agent that performed the task.
task'[character(1)]' The task.
success'[logical(1)]' Whether the task succeeded.
score'[numeric(1)]' Judge score (0 to 1), 'NA' if unknown.
source'[character(1)]' Where the verdict comes from, e.g. '"validation"' or '"hitl"'.
Invisibly, the stored outcome record.
search()Retrieve the lessons most relevant to a query.
Memory$search(query, k = 3, agent_name = NULL)
query'[character(1)]' The task or prompt to find lessons for.
k'[integerish(1)]' Maximum number of lessons to return. Default 3.
agent_name'[character(1) | NULL]' When given, only lessons learned by this agent are considered.
A list of lesson records, most relevant first (possibly empty).
track_record()Summarise the recorded outcomes per agent.
Memory$track_record(agent_names = NULL)
agent_names'[character | NULL]' Agents to report on. Agents with no recorded outcome get a row with 'n_tasks = 0'. Defaults to every agent that has an outcome.
A data frame with columns 'agent_name', 'n_tasks', 'n_success', 'success_rate' and 'mean_score'.
save()Save the memory to a JSON file.
Memory$save(path = self$path)
path'[character(1)]' Destination file. Defaults to '$path'.
Invisibly, 'self'.
load()Load lessons and outcomes from a JSON file written by $save(),
replacing the current content.
Memory$load(path = self$path)
path'[character(1)]' Source file. Defaults to '$path'.
Invisibly, 'self'.
clear()Remove all lessons and outcomes (and save, when '$path' is set).
Memory$clear()
Invisibly, 'self'.
print()Print a short summary of the memory.
Memory$print(...)
...Ignored.
clone()The objects of this class are cloneable with this method.
Memory$clone(deep = FALSE)
deepWhether to make a deep clone.
memory <- Memory$new()
memory$add_lesson(
lesson = "When translating to German, keep product names in English.",
task = "Translate the product description to German",
agent_name = "translator",
score = 0.9,
success = TRUE
)
memory$search("Translate this text to German")
# Persist across sessions
path <- tempfile(fileext = ".json")
memory <- Memory$new(path = path)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.