Agent Builder
Mathews-Tom/armory
Build AI agents and automate Claude Code programmatically via the Claude Agent SDK and headless CLI mode.
A developer guide to adding compact memory to an agent: when to trigger compaction, how to fork a compactor sub-agent, what the summary holds, and how to restore it.
$ npx skills add simbajigege/book2skills --skill compact-memory-implementation -a claude-codeProject install by default; add -g for ~/.claude/skills/.
$ gh skill install simbajigege/book2skills compact-memory-implementation --agent claude-codeProject scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .claude/skills && cp -r skills-src/skills/compact-memory-implementation .claude/skills/compact-memory-implementation && rm -rf skills-srcUse ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.
Claude Code skills documentation · loads skills from .claude/skills/
Install the "compact-memory-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/compact-memory-implementation into .claude/skills/compact-memory-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "compact-memory-implementation", then confirm the skill loads.Claude Code copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$skill-installer install https://github.com/simbajigege/book2skills/tree/main/skills/compact-memory-implementationType this inside Codex. $skill-installer <name> installs a curated skill from openai/skills. The installer writes to $CODEX_HOME/skills (default ~/.codex/skills). Restart Codex if the skill does not show up.
$ npx skills add simbajigege/book2skills --skill compact-memory-implementation -a codexProject install goes to .agents/skills/; add -g for ~/.codex/skills/.
$ gh skill install simbajigege/book2skills compact-memory-implementation --agent codexProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .agents/skills && cp -r skills-src/skills/compact-memory-implementation .agents/skills/compact-memory-implementation && rm -rf skills-srcUse ~/.agents/skills/ instead of .agents/skills for a personal install.
Codex skills documentation · loads skills from .agents/skills/
Install the "compact-memory-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/compact-memory-implementation into .agents/skills/compact-memory-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "compact-memory-implementation", then confirm the skill loads.Codex copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add simbajigege/book2skills --skill compact-memory-implementation -a cursorProject install goes to .agents/skills/; add -g for ~/.cursor/skills/.
$ gh skill install simbajigege/book2skills compact-memory-implementation --agent cursorProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .cursor/skills && cp -r skills-src/skills/compact-memory-implementation .cursor/skills/compact-memory-implementation && rm -rf skills-srcUse ~/.cursor/skills/ instead of .cursor/skills for a personal install.
Cursor skills documentation · loads skills from .cursor/skills/, .agents/skills/, .claude/skills/, .codex/skills/
Install the "compact-memory-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/compact-memory-implementation into .cursor/skills/compact-memory-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "compact-memory-implementation", then confirm the skill loads.Cursor copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gemini skills install https://github.com/simbajigege/book2skills.git --path skills/compact-memory-implementation--scope user (default) or --scope workspace; --path is the subfolder of the repo that holds the skill; --consent skips the security confirmation prompt.
$ npx skills add simbajigege/book2skills --skill compact-memory-implementation -a gemini-cliProject install goes to .agents/skills/; add -g for ~/.gemini/skills/.
$ gh skill install simbajigege/book2skills compact-memory-implementation --agent gemini-cliProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .gemini/skills && cp -r skills-src/skills/compact-memory-implementation .gemini/skills/compact-memory-implementation && rm -rf skills-srcUse ~/.gemini/skills/ instead of .gemini/skills for a personal install, then run /skills reload.
Gemini CLI skills documentation · loads skills from .gemini/skills/, .agents/skills/
Install the "compact-memory-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/compact-memory-implementation into .gemini/skills/compact-memory-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "compact-memory-implementation", then confirm the skill loads.Gemini CLI copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gh skill install simbajigege/book2skills compact-memory-implementationInstalls for Copilot at project scope by default; add --scope user for a personal install. Preview a skill first with gh skill preview. Needs GitHub CLI 2.90.0 or later (public preview).
$ npx skills add simbajigege/book2skills --skill compact-memory-implementation -a github-copilotProject install goes to .agents/skills/; add -g for ~/.copilot/skills/.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .github/skills && cp -r skills-src/skills/compact-memory-implementation .github/skills/compact-memory-implementation && rm -rf skills-srcUse ~/.copilot/skills/ instead of .github/skills for a personal install. Commit .github/skills so cloud agent and code review can use it.
GitHub Copilot skills documentation · loads skills from .github/skills/, .claude/skills/, .agents/skills/
Install the "compact-memory-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/compact-memory-implementation into .github/skills/compact-memory-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "compact-memory-implementation", then confirm the skill loads.GitHub Copilot copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add simbajigege/book2skills --skill compact-memory-implementation -a opencodeOpenCode documents no install command of its own. Project install goes to .agents/skills/; add -g for ~/.config/opencode/skills/.
$ gh skill install simbajigege/book2skills compact-memory-implementation --agent opencodeProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/simbajigege/book2skills.git skills-src && mkdir -p .opencode/skills && cp -r skills-src/skills/compact-memory-implementation .opencode/skills/compact-memory-implementation && rm -rf skills-srcUse ~/.config/opencode/skills/ instead of .opencode/skills for a personal install.
OpenCode skills documentation · loads skills from .opencode/skills/, .claude/skills/, .agents/skills/
Install the "compact-memory-implementation" agent skill from https://github.com/simbajigege/book2skills/tree/main/skills/compact-memory-implementation into .opencode/skills/compact-memory-implementation/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "compact-memory-implementation", then confirm the skill loads.OpenCode copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
compact-memory-implementationA developer guide to adding compact memory to an agent: when to trigger compaction, how to fork a compactor sub-agent, what the summary holds, and how to restore it.
This skill walks a developer through building context compaction into an agent built on the Claude Agent SDK or the Anthropic API. It begins by clarifying the SDK and language, the agent architecture, whether sessions are long-running or short, and what must survive compaction: task state, decisions, tool results or conversation history.
It then covers three triggers. A token threshold, recommended at roughly 70 to 80 percent of the model's context limit, checks the previous response's input token usage. A turn count compacts every N turns. A phase boundary compacts between tasks such as research and implementation. Compaction itself runs in a separate forked agent call, which can use a cheaper model, so the main agent waits for a fresh structured summary instead of summarizing its own drifted context.
Later steps define the summary format and prompt and how the next session restores the stored memory. A script, scripts/pre_compact_extract.py, is bundled, and the code examples are in Python.
7 steps, taken from the step headings in SKILL.md.
Read from SKILL.md and the folder at commit e5ba66c. It shows what the files ask for, not the result of running them.
Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.
From allowed-tools in the SKILL.md frontmatter.
Ships 1 file in scripts/ (Python), which the agent can run.
From the folder's file list and the shell code blocks in SKILL.md.
No URLs in SKILL.md.
From URLs in SKILL.md, links to its own repository left out.
Names no API keys, tokens, secrets or passwords.
From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.
Compact Memory Implementation loads about 2.5k tokens when it runs. Until then it costs about 98 tokens; SKILL.md has 439 words of instructions outside code blocks.
Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.
The automated check found no risky patterns in SKILL.md.
Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); the scripts in this folder are not scanned.
The full file from simbajigege/book2skills at commit e5ba66c, republished under its MIT licence (© simbajigege). 439 words, ~2,492 tokens.
.claude/skills/compact-memory-implementation/SKILL.md (or your agent's skills folder). This skill also uses 2 other files; get the full folder from GitHub.A developer guide for building compact memory into an Agent: detect when to compress, fork a compactor sub-agent, produce a structured summary, and restore it in the next session.
Before designing anything, clarify:
This determines which pattern fits.
Three strategies, pick based on your session model:
1. Token threshold (recommended)
Check usage.input_tokens from the previous response. When it exceeds ~70–80% of your model's context limit, trigger compact.
COMPACT_THRESHOLD = 150_000 # adjust per model
if response.usage.input_tokens > COMPACT_THRESHOLD:
compact = compact_memory(history)
history = [] # reset — compact moves to system prompt2. Turn count Compact every N turns. Simpler but less adaptive — misses sessions with a few very long turns.
COMPACT_EVERY_N = 30
if turn_count % COMPACT_EVERY_N == 0:
compact = compact_memory(history)3. Phase boundary Compact at natural task boundaries (after research, before implementation). Requires the agent to detect phases. Produces summaries that align with meaningful milestones, but harder to implement reliably.
Recommended default: token threshold at 70%, with turn-count fallback at N=40.
The compactor is a separate agent call whose only job is to read the current state and return a structured summary. Fork it synchronously — the main agent waits for the result before continuing.
def compact_memory(history: list[dict]) -> dict:
response = client.messages.create(
model="claude-haiku-4-5-20251001", # cheaper model is fine for compaction
max_tokens=4096,
system=COMPACTOR_SYSTEM_PROMPT,
messages=[
{
"role": "user",
"content": format_history_for_compact(history),
}
],
)
return json.loads(response.content[0].text)Why fork instead of self-compact:
{
"task": "What the agent is working on and why — the goal, not the steps",
"current_state": "Exact status at compaction point: what is done, what is not, what is in progress",
"key_decisions": [
{ "decision": "...", "reason": "...", "constraint": "..." }
],
"eliminated_approaches": [
{ "approach": "...", "reason_ruled_out": "..." }
],
"open_questions": ["..."],
"next_steps": ["..."],
"relevant_tool_results": {
"key": "Only results future steps will need — summarized, not raw dumps"
},
"compacted_at_turn": 42
}You are a conversation compactor. Read the provided conversation and produce a JSON summary that captures everything a fresh agent needs to continue the work without asking what happened.
Include:
- Current task and goal (not the steps taken to get here)
- Exact current state — what is done and what is not
- Decisions made and WHY (reasoning, not just the choice)
- Approaches tried and ruled out with reasons (prevents re-exploration)
- Open questions and blockers
- Concrete next steps in priority order
- Tool results that future steps will need (summarize, don't dump raw output)
Omit:
- Intermediate reasoning that led nowhere
- Completed sub-tasks with no future relevance
- Raw tool output that has already been acted on
- Anything derivable by reading the code or running a command
Output valid JSON matching the schema provided. No prose outside the JSON.def format_history_for_compact(history: list[dict]) -> str:
lines = ["Conversation to compact:\n"]
for msg in history:
role = msg["role"].upper()
content = msg["content"] if isinstance(msg["content"], str) else "[tool use]"
lines.append(f"[{role}]: {content[:2000]}") # cap very long messages
return "\n".join(lines)The compact object becomes the "memory" for the next turn or session. Inject it into the system prompt so it's always visible to the agent.
MEMORY_BLOCK_TEMPLATE = """
## Restored memory (compacted at turn {turn})
**Task**: {task}
**Current state**: {current_state}
**Key decisions**:
{decisions}
**Ruled out approaches**:
{eliminated}
**Next steps**:
{next_steps}
Begin from current state above. Do not re-explore eliminated approaches.
"""
def build_system_with_memory(base_system: str, compact: dict | None) -> str:
if compact is None:
return base_system
memory = MEMORY_BLOCK_TEMPLATE.format(
turn=compact["compacted_at_turn"],
task=compact["task"],
current_state=compact["current_state"],
decisions="\n".join(f"- {d['decision']} (because {d['reason']})"
for d in compact["key_decisions"]),
eliminated="\n".join(f"- {e['approach']}: {e['reason_ruled_out']}"
for e in compact["eliminated_approaches"]),
next_steps="\n".join(f"- {s}" for s in compact["next_steps"]),
)
return base_system + "\n\n" + memorymessages = [
{
"role": "user",
"content": f"[Resuming from compacted state — turn {compact['compacted_at_turn']}]\n"
f"{json.dumps(compact, indent=2)}\n\n"
f"Continue from the next steps listed above.",
}
]import json, pathlib
MEMORY_DIR = pathlib.Path("memory")
MEMORY_DIR.mkdir(exist_ok=True)
def save_compact(session_id: str, compact: dict) -> None:
(MEMORY_DIR / f"{session_id}.json").write_text(json.dumps(compact, indent=2))
def load_compact(session_id: str) -> dict | None:
path = MEMORY_DIR / f"{session_id}.json"
return json.loads(path.read_text()) if path.exists() else Nonedef run_agent(session_id: str, user_input: str) -> str:
compact = load_compact(session_id)
system = build_system_with_memory(BASE_SYSTEM, compact)
history = []
turn = 0
while True:
response = client.messages.create(
model="claude-opus-4-7",
system=system,
messages=history + [{"role": "user", "content": user_input}],
max_tokens=8192,
)
# Trigger compact if context is growing too large
if response.usage.input_tokens > COMPACT_THRESHOLD:
compact = compact_memory(history)
save_compact(session_id, compact)
system = build_system_with_memory(BASE_SYSTEM, compact)
history = [] # reset history — compact is now in system
turn = 0
continue
if response.stop_reason == "end_turn":
return response.content[0].text
history.append({"role": "assistant", "content": response.content})
user_input = handle_tool_calls(response) # your tool dispatch
turn += 1If a session resumes multiple times, don't stack compacts — re-compact instead:
COMPACTOR_WITH_PRIOR = """
You are updating an existing memory compact with new information from a continuation session.
Prior compact:
{prior_compact}
New conversation turns since last compact:
{new_turns}
Produce an updated compact that:
- Merges both sources
- Removes resolved items and completed steps
- Adds new decisions, eliminations, and open questions
- Keeps next_steps current
Output valid JSON. No prose outside the JSON.
"""
def compact_memory_with_prior(history: list[dict], prior: dict) -> dict:
prompt = COMPACTOR_WITH_PRIOR.format(
prior_compact=json.dumps(prior, indent=2),
new_turns=format_history_for_compact(history),
)
response = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=4096,
system=prompt,
messages=[{"role": "user", "content": "Update the compact."}],
)
return json.loads(response.content[0].text)| Pitfall | Fix |
|---|---|
| Compact loses tool results needed later | Include summarized results in relevant_tool_results |
| Fresh session ignores compact | Inject into system prompt, not buried in messages |
| Compactor uses the same expensive model | Use Haiku for compaction, Opus for main work |
| Compact grows unbounded across sessions | Re-compact using "chaining compacts" pattern above |
| Compacting too often (every turn) | Use token threshold, not turn frequency |
| Compact JSON fails to parse | Add retry with explicit error feedback to compactor |
© simbajigege, MIT. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file
SKILL.md and 2 other files (scripts) in skills/compact-memory-implementation of simbajigege/book2skills.
Open the folder on GitHubat commit e5ba66c
Compact Memory Implementation next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.
| Skill | Stars | Used in | Tokens | Auto-check | Licence | Repo updated |
|---|---|---|---|---|---|---|
| Compact Memory Implementation this skillsimbajigege/book2skills | 184 | — | ~2.5k | Automated safety check: Pass | MIT | |
| Agent BuilderMathews-Tom/armory | 327 | — | ~1.7k | Automated safety check: Pass | MIT | |
| Pydantic AI Harnesspydantic/pydantic-ai | 20k | — | ~4.9k | Automated safety check: Pass | MIT | |
| Agent Squad Python Guide2FastLabs/agent-squad | 7.8k | — | ~4.7k | Automated safety check: Pass | Apache-2.0 | |
| Deep Agents Corelangchain-ai/langchain-skills | 1.3k | 1 repos | ~3.1k | Automated safety check: Pass | MIT | |
| Omnigent Framework Detectionomnigent-ai/omnigent | 11k | — | ~610 | Automated safety check: Pass | Apache-2.0 |
Mathews-Tom/armory
Build AI agents and automate Claude Code programmatically via the Claude Agent SDK and headless CLI mode.
pydantic/pydantic-ai
Adds optional capabilities to Pydantic AI agents from pydantic-ai-harness, led by Code Mode, which runs many tool calls as one sandboxed Python script.
2FastLabs/agent-squad
Map of the agent-squad Python framework for async multi-agent orchestration: which agent, classifier, storage and tool provider to pick, and the pitfalls to avoid.
langchain-ai/langchain-skills
Explains how to build agents with the Deep Agents framework: create_deep_agent, the built-in middleware, the harness, SKILL.md format and configuration options.
omnigent-ai/omnigent
Scans Python agent code for framework imports and recommends the matching Omnigent executor type, or says when the framework is not natively supported yet.
topoteretes/cognee
Explains how cognee stores session memory by session_id and bridges it into the permanent graph with improve(), including the stages, results and settings.
simbajigege/book2skills
Reorganizes an overgrown MEMORY.md into a short pointer index plus separate topic files, and fixes or deletes outdated memories instead of archiving them.
simbajigege/book2skills
Turns text, screenshots, or existing diagrams into minimal, accessible line-art SVGs for teaching material, with an optional Mermaid relationship spec.
simbajigege/book2skills
Helps define agent tools with a fail-closed pattern: one class holding name, schema, security flags and a validate, permission and call execution chain.
simbajigege/book2skills
Implements a production-style agent loop in your own AI product, with tool calling, tool results fed back, exit conditions and budget guards.
simbajigege/book2skills
Analyzes Chinese public company financial statements (balance sheet, income statement, cash flow) to assess asset quality, profit authenticity, cash flow health, solvency, and overall investment…
simbajigege/book2skills
Guides designing a layered permission pipeline for agent tools that decides which calls are allowed, need confirmation or are denied, with scopes and hooks.
Works with
Categories
A developer guide to adding compact memory to an agent: when to trigger compaction, how to fork a compactor sub-agent, what the summary holds, and how to restore it. This skill walks a developer through building context compaction into an agent built on the Claude Agent SDK or the Anthropic API. It begins by clarifying the SDK and language, the agent architecture, whether sessions are long-running or short, and what must survive compaction: task state, decisions, tool results or conversation history.
Compact Memory Implementation fits situations like: adding context compression to an agent that runs long sessions; choosing between token-threshold, turn-count and phase-boundary triggers; designing the summary format a compactor sub-agent returns; restoring compacted memory when a new session starts.
Run `npx skills add simbajigege/book2skills --skill compact-memory-implementation -a claude-code`. Or copy the skill folder (skills/compact-memory-implementation in simbajigege/book2skills) into .claude/skills/compact-memory-implementation in your project. Claude Code loads it when a task matches its description.
Run `npx skills add simbajigege/book2skills --skill compact-memory-implementation -a codex`. Or copy the skill folder (skills/compact-memory-implementation in simbajigege/book2skills) into .agents/skills/compact-memory-implementation in your project. Codex loads it when a task matches its description.
Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add simbajigege/book2skills --skill compact-memory-implementation -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/compact-memory-implementation, .gemini/skills/compact-memory-implementation, .github/skills/compact-memory-implementation and .opencode/skills/compact-memory-implementation in your project.
Going by SKILL.md and its folder, Compact Memory Implementation needs Python for the scripts in its folder. Our summary lists: An agent built with the Claude Agent SDK or the Anthropic API; Python, for the bundled extraction script.
SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.
Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. The check reads SKILL.md only: the scripts in the folder are not scanned, so read them before running anything.
Compact Memory Implementation is published under the MIT licence (the repository's licence). It allows redistribution, so the full SKILL.md is shown on this page.
About 2.5k tokens (SKILL.md is roughly 10k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full.
Skills that share tags, products or a category with Compact Memory Implementation: Agent Builder (Mathews-Tom/armory, 327 stars), Pydantic AI Harness (pydantic/pydantic-ai, 20k stars), Agent Squad Python Guide (2FastLabs/agent-squad, 7.8k stars) and Deep Agents Core (langchain-ai/langchain-skills, 1.3k stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.
simbajigege (a GitHub user) maintains it in simbajigege/book2skills, which has 184 GitHub stars. The repository holds 37 skills in this directory. The repository was last updated on August 26, 2026.
Source: simbajigege/book2skills on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.