Memory: Memory: Lessons and Outcomes Agents Learn From

MemoryR Documentation

Memory: Lessons and Outcomes Agents Learn From

Description

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.

Public fields

path

'[character(1) | NULL]' JSON file the memory is saved to after every change. 'NULL' keeps the memory in the current session only.

lessons

A list of lesson records ('id', 'lesson', 'task', 'agent_name', 'score', 'success', 'created_at').

outcomes

A list of outcome records ('agent_name', 'task', 'success', 'score', 'source', 'created_at').

max_lessons

Maximum number of lessons kept (oldest are dropped first).

max_outcomes

Maximum number of outcomes kept (oldest are dropped first).

Methods

Public methods


Method new()

Create a memory, loading it from 'path' when that file exists.

Usage
Memory$new(
  path = NULL,
  max_lessons = 500,
  max_outcomes = 1000,
  search_fn = NULL
)
Arguments
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.


Method add_lesson()

Store a lesson.

Usage
Memory$add_lesson(
  lesson,
  task = NA_character_,
  agent_name = NA_character_,
  score = NA_real_,
  success = NA
)
Arguments
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.

Returns

Invisibly, the stored lesson record.


Method add_outcome()

Record the outcome of a task.

Usage
Memory$add_outcome(
  agent_name,
  task,
  success,
  score = NA_real_,
  source = "validation"
)
Arguments
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"'.

Returns

Invisibly, the stored outcome record.


Method search()

Retrieve the lessons most relevant to a query.

Usage
Memory$search(query, k = 3, agent_name = NULL)
Arguments
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.

Returns

A list of lesson records, most relevant first (possibly empty).


Method track_record()

Summarise the recorded outcomes per agent.

Usage
Memory$track_record(agent_names = NULL)
Arguments
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.

Returns

A data frame with columns 'agent_name', 'n_tasks', 'n_success', 'success_rate' and 'mean_score'.


Method save()

Save the memory to a JSON file.

Usage
Memory$save(path = self$path)
Arguments
path

'[character(1)]' Destination file. Defaults to '$path'.

Returns

Invisibly, 'self'.


Method load()

Load lessons and outcomes from a JSON file written by $save(), replacing the current content.

Usage
Memory$load(path = self$path)
Arguments
path

'[character(1)]' Source file. Defaults to '$path'.

Returns

Invisibly, 'self'.


Method clear()

Remove all lessons and outcomes (and save, when '$path' is set).

Usage
Memory$clear()
Returns

Invisibly, 'self'.


Method print()

Print a short summary of the memory.

Usage
Memory$print(...)
Arguments
...

Ignored.


Method clone()

The objects of this class are cloneable with this method.

Usage
Memory$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

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)


mini007 documentation built on Dec. 3, 2026, 5:10 p.m.