Communitygithub.com

策略性壓縮 (Strategic Compact)

建議在邏輯間隔點手動壓縮上下文,以便在任務階段之間保留上下文,而非依賴任意的自動壓縮。當會話接近上下文限制且任務階段處於自然壓縮點時使用。

策略性壓縮 (Strategic Compact) 是什麼?

此技能透過在策略性間隔點建議執行 `/compact` 指令,而非依賴於任意的自動壓縮機制,來協助使用者管理 AI 代理程式的上下文視窗。在長時間執行的工作階段(超過 200K token)、多階段任務(研究→規劃→實作→測試)或切換任務時,它能主動提示使用者進行上下文壓縮,避免在任務中途失去關鍵資訊。此技能支援手動安裝(透過 `install.sh`)或作為插件使用,並透過 `suggest-compact.js` 掛鉤腳本監控兩種線索來觸發建議:上下文大小(讀取會話記錄中的 token 使用量,並根據模型視窗大小調整閾值,例如 200K 視窗為 160K token)和工具呼叫次數(預設 50 次後首次建議)。使用者可以透過多個環境變數進行設定,例如 `COMPACT_CONTEXT_THRESHOLD`、`COMPACT_CONTEXT_INTERVAL` 和 `ECC_CONTEXT_WINDOW_TOKENS`,以達到精確控制。此外,該技能還提供了壓縮決策指南和最佳實踐,例如在規劃完成後或除錯後進行壓縮,但避免在實作中途壓縮,並在壓縮前將重要上下文寫入檔案。它也提醒使用者注意任務清單可能不存在,因此建議將計劃寫入檔案以確保持久保存,並提供令牌優化模式(如觸發表延遲加載)以進一步減少基礎上下文。

相容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/strategic-compact

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

策略性壓縮技能

在工作流程中的策略性節點建議手動 /compact,而非依賴任意的自動壓縮。

啟動時機

  • 執行長時間工作階段,接近上下文限制(200K+ token)
  • 處理多階段任務(研究 → 規劃 → 實作 → 測試)
  • 在同一工作階段中切換不相關的任務
  • 完成一個主要里程碑並開始新工作後
  • 當回應速度變慢或連貫性變差時(上下文壓力)

為何策略性壓縮?

自動壓縮在任意時間點觸發:

  • 經常在任務中途觸發,遺失重要上下文
  • 不了解邏輯任務邊界
  • 可能會中斷複雜的多步驟操作

在邏輯邊界進行策略性壓縮:

  • 探索之後、執行之前 — 壓縮研究上下文,保留實作計劃
  • 完成一個里程碑之後 — 為下一階段提供全新開始
  • 在重大上下文轉換之前 — 在執行不同任務前,清除探索上下文

運作方式

suggest-compact.js 腳本在 PreToolUse(Edit/Write)時執行,並結合兩種訊號:

  1. 上下文大小(主要訊號) — 從工作階段記錄(掛鉤 payload 中的 transcript_path)讀取最新的 usage 記錄,並加總 input_tokens + cache_read_input_tokens + cache_creation_input_tokens(該次交談的真實上下文大小)。根據視窗縮放後的閾值建議 /compact — 在 200K 視窗中為 160K token,在 1M 視窗中為 250K token(從模型標記 [1m] 偵測,或當觀察到的 token 已超過 200K 時推斷)— 並且在每次上下文增加額外 60K token 後重新提醒。
  2. 工具呼叫次數(次要訊號) — 計算工作階段中的工具調用次數;在可設定的閾值(預設:50 次呼叫)時建議,之後每 25 次呼叫再次觸發。

單純工具呼叫次數是視窗壓力的弱代理指標:少量大型檔案讀取或 MCP 回應可能在極少呼叫次數下就填滿視窗,而許多小型呼叫可能在接近空的視窗時就超過 50 次。上下文大小訊號僅在真正重要時才會觸發。

掛鉤設定

作為插件安裝? 無需設定。該插件的 hooks/hooks.json 已註冊 suggest-compact.js(掛鉤 ID pre:edit-write:suggest-compact,在 standard 和 strict 掛鉤設定檔中啟用)。請勿將下面的區塊複製到 ~/.claude/settings.json 中 — 在插件安裝方式下,~/.claude/scripts/ 目錄不存在,且複製插件掛鉤會導致重複執行。

若手動安裝(執行 ./install.sh),請將其新增至您的 ~/.claude/settings.json:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit",
        "hooks": [{ "type": "command", "command": "node ~/.claude/scripts/hooks/suggest-compact.js" }]
      },
      {
        "matcher": "Write",
        "hooks": [{ "type": "command", "command": "node ~/.claude/scripts/hooks/suggest-compact.js" }]
      }
    ]
  }
}

設定

環境變數:

  • COMPACT_THRESHOLD — 首次建議前的工具呼叫次數(預設:50)
  • COMPACT_CONTEXT_THRESHOLD — 觸發上下文大小建議前的上下文 token 數(預設:200K 視窗為 160000,1M 視窗為 250000;設為 0 可停用上下文訊號)
  • COMPACT_CONTEXT_INTERVAL — 建議重複觸發前的額外上下文 token 數(預設:60000)
  • COMPACT_STATE_TTL_DAYS — 清除暫存目錄中過期的每個工作階段狀態檔案的天數(預設:14)
  • ECC_CONTEXT_WINDOW_TOKENS — 明確的上下文視窗大小(以 token 為單位),會覆蓋自動偵測。對於其報告的 ID 缺少 [1m] 標記的大型視窗模型(例如 400K Opus 4.x,或新的 1M 視窗模型系列)請設定此變數,以便閾值能根據真實視窗進行縮放,而不是預設為 200K 並高估上下文使用量。
  • CLAUDE_CODE_AUTO_COMPACT_WINDOW — Claude Code 的原生視窗大小覆寫值(以 token 為單位);當 ECC_CONTEXT_WINDOW_TOKENS 未設定時,以此作為備用。

上下文視窗會透過 [1m] 模型標記自動偵測,或在觀察到的 token 已超過 200K 時推斷。對於不具備這兩種訊號的大型視窗模型,請設定上述其中一個覆寫變數,以便 /compact 建議能在正確的時機觸發。

壓縮決策指南

使用下表來決定何時壓縮:

階段轉換壓縮?原因
研究 → 規劃是研究上下文長且繁雜;規劃是其提煉後的輸出
規劃 → 實作是規劃已寫入檔案(一個檔案,或您的任務清單(如果有的話));為程式碼騰出上下文空間
實作 → 測試或許如果測試參考了最近的程式碼則保留;如果轉換焦點則壓縮
除錯 → 下一個功能是除錯追蹤會污染不相關工作的上下文
實作中途否遺失變數名稱、檔案路徑和部分狀態的代價過高
在失敗的方法之後是在嘗試新方法之前,清除無解的路徑與推理

壓縮後保留與遺失的內容

了解哪些內容會保留,有助於您放心地進行壓縮:

保留遺失
CLAUDE.md 指令中間推理與分析
磁碟上的檔案您先前讀取的檔案內容
記憶體檔案 (~/.claude/memory/)多步驟對話上下文
Git 狀態(提交記錄、分支)工具呼叫歷史與計數
任務清單 — 僅當您有待辦事項工具時(見下方)口頭陳述的細微使用者偏好

不要依賴任務清單會保留 — 它可能不存在

Claude Code 2.1.233 已在 Opus 4.8、Sonnet 5、Fable 5、Mythos 5 及更新的模型上預設移除了待辦事項/任務工具(TodoWrite、TaskCreate/Get/Update/List)。設定 CLAUDE_CODE_ENABLE_TODO_TOOLS=1 可以將其恢復,但這是每台機器的環境設定 — 它不會隨此技能一同傳遞,因此您不能假設使用者有此設定。

這很重要,因為「我的待辦事項清單會在壓縮後保留」是人們選擇壓縮而不是將狀態寫下的原因。如果這些工具不存在,就沒有可供保留的清單,計劃也就完全消失了。在壓縮之前,先將計劃寫入檔案 — 檔案在所有版本和所有模型上都會保留。請將任務清單視為可能缺失的便利功能,切勿將其當作您的持久記錄。

最佳實踐

  1. 在規劃完成後壓縮 — 一旦計劃完成並寫入檔案,即可進行壓縮以重新開始
  2. 在除錯後壓縮 — 在繼續之前清除錯誤解決上下文的痕跡
  3. 不要在實作中途壓縮 — 保留相關變更的上下文
  4. 閱讀建議 — 掛鉤告訴您何時壓縮,而由您決定是否壓縮
  5. 在壓縮前先寫入 — 在壓縮前將重要上下文儲存到檔案或記憶體中
  6. 使用帶有摘要的 /compact — 新增自訂訊息:/compact 接下來專注於實作認證中介軟體

Token 優化模式

觸發表延遲加載

與其在工作階段開始時載入完整的技能內容,不如使用一個將關鍵字對應到技能路徑的觸發表。技能僅在觸發時載入,可將基礎上下文減少 50% 以上:

觸發詞技能載入時機
"test", "tdd", "coverage"tdd-workflow使用者提及測試
"security", "auth", "xss"security-review與安全相關的工作
"deploy", "ci/cd"deployment-patterns部署上下文

上下文組成感知

監控哪些內容消耗了您的上下文視窗:

  • CLAUDE.md 檔案 — 始終載入,請保持精簡
  • 已載入的技能 — 每個技能增加 1-5K token
  • 對話歷史 — 隨著每次交流而增長
  • 工具結果 — 檔案讀取、搜尋結果會增加內容量

重複指令偵測

常見的重複上下文來源:

  • 在 ~/.claude/rules/ 和專案 .claude/rules/ 中存在相同的規則
  • 重複 CLAUDE.md 指令的技能
  • 涵蓋重疊領域的多個技能

上下文優化工具

  • token-optimizer MCP — 透過內容去重實現自動化 95%+ 的 token 減量
  • context-mode — 上下文虛擬化(已展示從 315KB 降至 5.4KB)

相關資源

  • 長篇指南 — Token 優化章節
  • 記憶體持久化掛鉤 — 用於在壓縮後保留狀態
  • continuous-learning 技能 — 在工作階段結束前提取模式

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相關技能