Memory¶
Base memory and memory store classes.
BaseMemory
¶
Bases: ABC
Base class for episodic memory.
Subclasses are responsible for three things:
- Retrieval strategy — given a task and a store, decide which store primitive(s) to invoke and how to compose or score the results.
- Prompt formatting — convert the retrieved episodes into the string that gets injected into the agent's system prompt.
- Write-time transformation — optionally transform an episode before recording it (e.g. reflection, summarisation, fact extraction).
Source code in src/llm_agents_from_scratch/base/memory.py
recall
abstractmethod
async
¶
Retrieve relevant past episodes for a task.
Called at the start of each task. Returns a formatted string ready to inject into the agent's system prompt under the recalled_episodes block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
Task
|
The incoming task used to select relevant episodes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatted episode context for the system prompt. |
Source code in src/llm_agents_from_scratch/base/memory.py
record
abstractmethod
async
¶
Persist a completed episode.
Called at the end of each task. Implementations may apply write-time transformations (e.g. reflection, summarisation) before storing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
episode
|
Episode
|
The completed task, trajectory, and result. |
required |
Source code in src/llm_agents_from_scratch/base/memory.py
summary
abstractmethod
async
¶
Return a human-readable summary of the memory store.
Useful for inspection and debugging — describes what is currently held in memory (e.g. episode count, date range, topics).
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A short summary of the memory contents. |
Source code in src/llm_agents_from_scratch/base/memory.py
BaseMemoryStore
¶
Bases: ABC
Base class for episodic memory stores.
Subclasses are responsible for two things:
- Durable persistence — write episodes to the substrate, including any preprocessing required by that substrate (embedding, indexing, tokenization).
- Retrieval primitives — expose
read_recentandsearchso thatBaseMemoryimplementations can compose and score results without knowing the underlying storage details.
Source code in src/llm_agents_from_scratch/base/memory.py
write
abstractmethod
async
¶
Persist an episode to the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
episode
|
Episode
|
The completed episode to store. |
required |
embedded_text
|
str | None
|
Pre-formatted text for
substrates that embed episodes (e.g. vector stores).
When provided, the store uses this text for embedding
instead of formatting the episode itself. Substrates
that do not embed (e.g. |
None
|
Source code in src/llm_agents_from_scratch/base/memory.py
read_recent
abstractmethod
async
¶
Return the N most recently recorded episodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Maximum number of episodes to return. |
required |
Returns:
| Type | Description |
|---|---|
list[Episode]
|
list[Episode]: Episodes ordered from most recent to oldest. |
Source code in src/llm_agents_from_scratch/base/memory.py
count
abstractmethod
async
¶
Return the total number of episodes in the store.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Episode count. |
search
abstractmethod
async
¶
Return the K episodes most relevant to a query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The search query (e.g. the task instruction). |
required |
k
|
int
|
Maximum number of episodes to return. |
required |
**kwargs
|
Any
|
Optional substrate-specific search parameters (e.g. filters, score thresholds). |
{}
|
Returns:
| Type | Description |
|---|---|
list[Episode]
|
list[Episode]: Episodes ordered by relevance to the query. |
Source code in src/llm_agents_from_scratch/base/memory.py
summary
abstractmethod
async
¶
Return a human-readable summary of the store contents.
Describes the substrate, episode count, and the oldest and newest episodes. Intended for inspection and debugging.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Multi-line summary of the store. |