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

Tool Permission System AI Skill

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

Agent Memory Implementation Guide AI Skill

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

Session Dream AI Skill

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

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

Scans your codebase and generates AGENTS.md (the agent's orientation file) plus a complete docs/ knowledge base skele...

Harness Step 3 — Session State Management AI Skill

Creates init.sh (environment health check), tasks.json (structured task queue), and progress.md (append-only session ...

Compact with Memory AI Skill

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

LangChain Tool Builder AI Skill

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

Harness Step 2 — Fill docs/ with Real Content AI Skill

Deep-reads your codebase and fills every docs/ file with specific, accurate content grounded in what the code actuall...

Query Loop Implementation AI Skill

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

Common Stocks and Uncommon Profits AI Skill

Apply Philip Fisher's growth stock investment system to deeply evaluate any publicly traded company. Covers the full ...

Contract Drafting and Review Guide (Chinese Law) AI Skill

Apply the San Guan Si Bu Fa method to draft and review China-law contracts through transaction structure, contract st...