Skip to content
Writing
Skill

/folded-memory-implementation

Developer implementation guide for building hierarchical (folded) memory into an Agent. Three-layer architecture where recent turns stay detailed, older content compresses into episodes, and the oldest distills into durable semantic facts. Use when compact-memory-implementation

From plugin
book2skills
20338 skills

Claude Code

$ npx -y skills add simbajigege/book2skills --skill folded-memory-implementation --agent claude-code

SKILL.md

folded-memory-implementation.SKILL.md
name: folded-memory-implementation
description: Developer implementation guide for building hierarchical (folded) memory into an Agent. Three-layer architecture where recent turns stay detailed, older content compresses into episodes, and the oldest distills into durable semantic facts. Use when compact-memory-implementation is not retaining enough, or when agents need to recall decisions from many sessions ago.

folded-memory-implementation

A developer guide for hierarchical memory: instead of replacing history with a single flat summary, maintain three memory layers at different levels of detail. Old content is compressed more aggressively — not discarded. Each layer is independently stored and selectively recalled.

**Prerequisite**: read `compact-memory-implementation` first. Folded memory builds on the same fork-agent and trigger concepts.

---

The core idea

L1 Working memory    [ turn 38..50 ] — raw turns, full detail, short window
L2 Episodic memory   [ turn 10..37 ] — compressed episodes, medium detail
L3 Semantic memory   [ turn 1..9  ] — abstract facts and decisions, sparse

When L1 fills up → fork an episode compactor → move oldest L1 turns into L2. When L2 fills up → fork a semantic extractor → distill L2 into L3.

At each agent turn, inject the right combination of layers into the system prompt.

---

Step 1 — Understand the setup

Same questions as `compact-memory-implementation`, plus:

  • **How long do sessions run?** If sessions are short (<50 turns), flat compact is enough.
  • **What kind of information ages badly?** Decisions and patterns age well (good for L3). Exact tool outputs age badly (keep only in L1 or summarize into L2).
  • **Does the agent need to cite past reasoning?** If yes, L2/L3 must preserve decision rationale, not just conclusions.

---

Step 2 — Three-layer architecture

Layer 1 — Working memory

  • **Content**: raw conversation turns, full fidelity
  • **Window**: last N turns (e.g., 20 turns or ~30k tokens)
  • **Trigger to flush**: when L1 exceeds its window, oldest turns move to L2
  • **Injected as**: full message history in `messages[]`

Layer 2 — Episodic memory

  • **Content**: compressed episode summaries — what happened, what was decided, what was tried
  • **Window**: up to M episodes (e.g., 10 episodes, each covering ~20 turns)
  • **Trigger to flush**: when episode count exceeds M, oldest episodes distill into L3
  • **Injected as**: structured block in system prompt

Layer 3 — Semantic memory

  • **Content**: abstract facts, stable decisions, eliminated approaches, domain knowledge learned
  • **Window**: unbounded, but aggressively filtered — only durable knowledge
  • **Trigger to flush**: never purged, but updated/merged when contradicted
  • **Injected as**: compact block in system prompt, always present

---

Step 3 — Data structures

from dataclasses import dataclass, field
from typing import Any

@dataclass
class Episode:
    episode_id: int
    turn_range: tuple[int, int]       # (start_turn, end_turn)
    summary: str
    decisions: list[dict]             # [{"decision": ..., "reason": ...}]
    eliminated: list[dict]            # [{"approach": ..., "why": ...}]
    open_questions: list[str]
    tool_results: dict[str, Any]      # summarized results worth keeping

@dataclass
class SemanticMemory:
    facts: list[str]                  # stable domain facts
    decisions: list[dict]             # durable decisions (never re-litigate)
    eliminated_approaches: list[dict] # things proven not to work
    patterns: list[str]               # recurring patterns observed

@dataclass
class FoldedMemory:
    semantic: SemanticMemory
    episodes: list[Episode]
    current_episode_id: int = 0
    total_turns_seen: int = 0

---

Step 4 — Fork agent: L1 → L2 (episode compactor)

Triggered when working memory (L1) exceeds its window. Takes the oldest N turns and compresses them into one `Episode`.

EPISODE_COMPACTOR_PROMPT = """
You are an episode compactor. Read the provided conversation turns and produce a structured Episode summary.

An Episode captures:
- What was attempted and what happened (not the full dialogue — the outcome)
- Decisions made and WHY (the reasoning behind them, not just the choice)
- Approaches tried and ruled out, with reasons (prevents re-exploration)
- Open questions that were not resolved
- Tool results that future turns will need (summarize, don't dump raw output)

Do NOT include:
- Intermediate back-and-forth that led to a conclusion (keep the conclusion, drop the path)
- Tool outputs that have already been acted on and have no future relevance
- Anything a fresh agent could derive by reading the code or running a command

Output valid JSON:
{
  "summary": "2-3 sentence narrative of what happened in this episode",
  "decisions": [{"decision": "...", "reason": "...", "constraint": "..."}],
  "eliminated": [{"approach": "...", "why": "..."}],
  "open_questions": ["..."],
  "tool_results": {"key": "summarized result"}
}
"""

def compact_l1_to_episode(turns: list[dict], episode_id: int, turn_range: tuple) -> Episode:
    response = client.messages.create(
        model="claude-haiku-4-5-20251001",
        max_tokens=2048,
        system=EPISODE_COMPACTOR_PROMPT,
        messages=[{"role": "user", "content": format_turns(turns)}],
    )
    data = json.loads(response.content[0].text)
    return Episode(
        episode_id=episode_id,
        turn_range=turn_range,
        **data,
    )

---

Step 5 — Fork agent: L2 → L3 (semantic extractor)

Triggered when the episode count exceeds the L2 window. Takes the oldest episodes and distills durable knowledge into `SemanticMemory`.

SEMANTIC_EXTRACTOR_PROMPT = """
You are a semantic memory extractor. Read the provided episode summaries and extract only
knowledge that is durable — facts, decisions, and patterns that will still matter many
sessions from now.

Extract:
- Stable domain facts discovered ("the API al
Read more
@simbajigege
Ships withbook2skills

Transform the wisdom of classic books into AI Agent Skills. book2skills is an open-source project focused on systematically distilling high-quality practical books into Agent Skills format, enabling AI agents like Claude and Claude Code to directly apply the

Stars on GitHub

Momentum

stars in total
203
Star history of simbajigege/book2skills on GitHub, a chart by star-history.com

Other skills on book2skills.

agent-tool-builder

agent-tool-builder

Define agent tools using the fail-closed design pattern — unified name/schema/security/execution in one class, with three-layer…

More skills from other plugins.

humanizer

humanizer

Rewrite AI-sounding text so it reads like the writer without changing what it says. Use when editing or reviewing prose for AI…

@blader@bladerInstall Skill
no-ai-slop

no-ai-slop

Edit drafts into sharper, more human writing while preserving the writer's personal voice, or detect AI-slop patterns without…

@petergyang@petergyangInstall Skill
browser-cdp

browser-cdp

Use this skill when you need to control a Chrome browser via CDP (Chrome DevTools Protocol) to reuse existing login sessions.…

oh-story6d8.6k
@zenstory-ai@zenstory-aiInstall Skill

Questions about folded-memory-implementation.

Which coding agents can use folded-memory-implementation?
folded-memory-implementation installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.