Skill in 《Compact Memory Implementation Guide》

Skill Description

A developer implementation guide for adding compact memory to an Agent built with the Anthropic API or Claude Agent SDK — covers trigger strategy, the fork-compactor pattern, compact JSON schema design, and cross-session memory restoration with full working code.

Skill.md

Compact Memory Implementation Guide

Agents running long sessions face a fundamental problem: conversation history grows unboundedly, but the model's context window is fixed. Once you hit the limit, you either truncate history (losing critical context) or the session fails. This skill provides a complete engineering implementation: when context approaches the limit, automatically fork a compactor sub-agent to compress the session, inject the result into the next system prompt, and persist it across sessions.

Designed for engineers building agents with the Anthropic API (Python or TypeScript SDK) or the Claude Agent SDK.

7-Step Implementation Framework

StepWhat it covers
Step 1Understand your setup: SDK, agent architecture, session model
Step 2Trigger strategy: token threshold (recommended), turn count, phase boundary
Step 3Fork the compactor: separate API call, Haiku model, synchronous wait
Step 4Compact output schema: task, current_state, key_decisions, eliminated_approaches, next_steps
Step 5Memory restoration: system prompt injection (recommended) or first-message injection
Step 6Full agent loop: complete code with trigger, compact, persist, and restore
Step 7Chaining compacts: merge across sessions instead of stacking

Why Fork Instead of Self-Compact

A main agent deep in a long task has drifted — it's focused on current details, not the full picture. A fresh compactor reads the entire history from scratch and produces a more accurate summary. Separation of concerns: executing vs. summarizing are different cognitive tasks. And practically: Haiku is good enough for compaction; reserve the expensive model for the main work.

Supported Use Cases

  • Agents that need to maintain state across sessions (research, code review, multi-step execution)
  • Sessions of unpredictable length where manual intervention is not acceptable
  • Scenarios where key decisions, eliminated approaches, and tool results must survive compaction
  • Long-running projects where the compact must stay accurate across many sessions (chaining mode)

Common Pitfalls

PitfallFix
Compact loses tool results needed laterInclude summarized results in relevant_tool_results
New session ignores compactInject into system prompt, not messages
Compactor uses the expensive modelUse Haiku for compaction, Opus for main work
Compact grows unbounded across sessionsUse chaining compacts — merge, don't stack
Compact JSON parse failure crashes sessionAdd retry with error feedback + fallback compact

Limitations

This skill provides an implementation framework and code templates, not a drop-in library. Compact quality depends on the compactor's system prompt — if the instructions are vague, the summary will miss key context. Validate the first few compacts manually in development before relying on them in production.

Install & Use

Install command

npx skills add simbajigege/book2skills/skills/compact-memory-implementation
OR

Direct download

Related Skills

Folded Memory Implementation Guide AI Skill

A developer implementation guide for building hierarchical (folded) memory into an Agent — three-layer architecture where recent turns stay detailed.

Agent Memory Implementation Guide AI Skill

Restructures a bloated or chaotic MEMORY.md into the 2-layer architecture Claude Code's autoDream service uses internally.

Compact with Memory AI Skill

Executes /compact correctly — generates a high-quality conversation summary that preserves reasoning, decisions, and current state.

Session Dream AI Skill

On-demand session memory distillation — extracts key decisions, eliminated approaches, new discoveries, and current blockers.

Query Loop Implementation AI Skill

Implement a production-ready LLM query loop for AI applications: tool calling, structured tool_result feedback, ReAct-style cycles.

Tool Permission System AI Skill

Design a configurable, layered permission/safety system for agent tools: one permission pipeline decides allow / ask / deny.

Agent Tool Builder AI Skill

Define agent tools using the fail-closed design pattern — unified name, schema, security properties, and execution logic in one class.

Harness Step 3 — Session State Management AI Skill

Creates init.sh, tasks.json, and progress.md — so any new Claude Code session recovers full working context in under 30 seconds.

Harness Step 1 — Create AGENTS.md & Knowledge Base AI Skill

Scans your codebase and generates AGENTS.md plus a complete docs/ knowledge base skeleton for any Claude Code session.

LangChain Tool Builder AI Skill

Build LangChain (Python) tools using Claude Code's fail-closed design pattern — unified name, schema, security, and execution in one class.