Skip to content

About

Stop losing context on every /compact. Claude Anchor preserves what matters.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

⚓ claude-anchor

Stop losing context on every /compact. Claude Anchor preserves what matters.

One-line install. Zero configuration. Works with every Claude Code project.

License: MIT Claude Code


The problem

Claude Code's built-in /compact treats all context equally — summarizing file paths, error messages, and hard-won decisions with the same blunt force as tool output noise. After a few compactions in a long session, you're re-reading files you already analyzed and re-discovering bugs you already fixed.

What actually gets lost:

  • Exact file paths (paraphrased to "the config file")
  • Error messages and root causes (summarized away)
  • Decisions and their rationale (the WHY disappears)
  • User corrections and preferences (reset each compaction)

How it works

Claude Anchor hooks into Claude Code's PreCompact and PostCompact lifecycle events:

/compact triggered
       │
       ▼
  PreCompact hook ──► Tells Claude what to preserve verbatim in the summary
       │                (file paths, errors, decisions, corrections)
       ▼
  Summary generated ──► Better quality, structured retention
       │
       ▼
  PostCompact hook ──► Re-injects your ~/.claude/anchor-context.md rules
       │
       ▼
  Session continues ──► Context intact

It also sets CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=85, so compaction triggers at 85% capacity instead of the default — before context quality starts degrading.

Research basis: Factory.ai evaluated context compression strategies across 36,000 real agent sessions. Structured, instruction-driven summarization scored 8% higher on context retention vs. the default Anthropic approach. The gap widens with multiple compaction cycles. (source)


Install

Option A — Claude Code plugin (recommended: discoverable, auto-updates):

claude plugins marketplace add rodrigooler/claude-anchor
claude plugins install anchor

Then run /anchor install once to activate the hooks.

Option B — One-liner curl (no plugin system needed):

curl -fsSL https://raw.githubusercontent.com/rodrigooler/claude-anchor/main/install.sh | bash

Custom threshold (e.g. for 1M-context Opus models):

CLAUDE_ANCHOR_PCT=65 curl -fsSL https://raw.githubusercontent.com/rodrigooler/claude-anchor/main/install.sh | bash

What gets installed

Component Location Purpose
anchor-pre-compact.sh ~/.claude/hooks/ Instructs summarization before compact
anchor-post-compact.sh ~/.claude/hooks/ Re-injects your persistent rules after compact
anchor-context.md ~/.claude/ Your custom rules that survive every compaction
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE ~/.claude/settings.json env Compact threshold (default: 85%)

The anchor-context.md file

After install, ~/.claude/anchor-context.md is created with a template. Add anything here that should survive compaction:

# Anchor Context

## Tool preferences
- Always use ripgrep over grep
- Prefer TypeScript strict mode

## Project conventions
- Money values in cents (never floats)
- All IDs are UUIDs

## Ongoing work
- Refactoring auth module — do not touch adjacent files

Every time Claude Code compacts, this file is re-injected automatically.



Uninstall

curl -fsSL https://raw.githubusercontent.com/rodrigooler/claude-anchor/main/uninstall.sh | bash

Or if you installed locally:

bash uninstall.sh

How it compares

Default /compact claude-anchor
File paths Paraphrased or dropped Preserved verbatim
Error messages Summarized Preserved verbatim
Decision rationale Lost Preserved with WHY
User corrections Reset Re-injected via context file
Compact timing ~83% capacity Configurable (default 85%)
Persistent rules None ~/.claude/anchor-context.md
Setup Built-in One curl command

Configuration

All configuration is optional. The defaults work well for most setups.

Variable Default Description
CLAUDE_ANCHOR_PCT 85 Compact threshold % (set during install)

To change the threshold after install, edit ~/.claude/settings.json:

{
  "env": {
    "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE": "90"
  }
}

Threshold guidance:

  • 200K context models (Sonnet): 85–92% → triggers at 170K–184K tokens
  • 1M context models (Opus): 60–70% → triggers at 600K–700K tokens

Contributing

See CONTRIBUTING.md. PRs welcome — especially:

  • Additional hook templates for specific workflows (coding, writing, research)
  • Better anchor-context.md examples for different use cases
  • Shell completion for the /anchor skill

License

MIT — Rodrigo Oler

About

Stop losing context on every /compact. Claude Anchor preserves what matters.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages