liveBook · Manning

Episode

As explained in Build a Multi-Agent System (from Scratch)

Episode is a book-specific record of an agent task execution containing its task, result or exception, rollout, metadata, identifier, and completion timestamp.

An Episode records one completed agent task execution for use in episodic memory. It preserves the task, its outcome, and the rollout so that a later task can draw on that experience. Unlike the working rollout used during a task, episodes can persist across tasks in an external store. section 7.1.2 section 7.2.1

Episode is a Pydantic BaseModel with these fields:

  • task: the Task; rollout: a string describing its execution. section 7.2.1
  • result: an optional TaskResult; error: an optional Exception. The recording workflow requires at least one of them. section 7.2.1 section 7.5.2
  • metadata: a dictionary of string annotations; id_: a generated unique identifier; completed_at: a completion timestamp. section 7.2.1
Listing 7.1 Defining Episode and its attributes
# llm_agents_from_scratch/data_structures/memory.py
import uuid
from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field
from .agent import Task, TaskResult
 
 
class Episode(BaseModel):
    """A completed task to be stored in memory.""" #A
 
    model_config = ConfigDict(arbitrary_types_allowed=True) #B
 
    id_: str = Field(default_factory=lambda: str(uuid.uuid4())) #C
    task: Task
    rollout: str
    result: TaskResult | None = Field(default=None)
    error: Exception | None = Field(default=None)
    metadata: dict[str, str] = Field(default_factory=dict)
    completed_at: datetime = Field(default_factory=datetime.now)

The declaration shows the field types, defaults, and configuration for arbitrary types. section 7.2.1

Episode.format() serializes the record for prompt injection or embedding. EpisodeFormatMode.XML produces XML blocks; CONCAT produces plain key: value entries. The method accepts a set of fields to exclude and defaults to excluding id_ and rollout. __str__() uses XML formatting. The underlying formatting helpers are separate from the mode-selection logic. section 7.2.1

At the start of a task, TaskHandler loads relevant memories and injects their formatted episodes into the system message for each step. At completion, record_memory() constructs an Episode and sends it to each configured Memory. Individual recall or recording failures are logged without stopping the remaining memory operations. section 7.5.2 section 7.7

Memory uses a BaseMemoryStore backend to persist and recall episodes. Before recording, it can enrich an episode with metadata produced by configured functions. It deep-copies the episode first, so metadata generated by one Memory does not leak into records written by another. Recall queries the store with the task instruction and joins the returned episodes as formatted strings. section 7.4.1

Reusing a sequence result

An agent computes the stop-at-one sequence for 4 and records the completed execution. For a later task asking the same question, similarity memory recalls an XML episode containing the earlier task and result; the agent then answers with 4 → 2 → 1. section 7.5.2

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Implements the model, serialization dispatch, and recording lifecycle, while deferring the formatting helpers.
Examples
Demonstrates a Hailstone sequence record and recalls a stop-at-one sequence result for a later task.
What you can do after
Builds an Episode-based memory workflow from scratch for recording and recalling agent task executions.
How it connects
Connects episodic memory with working memory, task execution, serialization, and semantic similarity.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Multi-Agent System (from Scratch). The section links point to the parts of the book it draws on.