Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

节省

PRECC追踪每次拦截的估计token节省。使用 precc savings 查看PRECC阻止了多少浪费。

快速摘要

$ precc savings
Session Token Savings
=====================
Total estimated savings: <span data-stat="session_tokens_saved">8,741</span> tokens

Breakdown:
  Pillar 1 (cd prepends):         <span data-stat="session_p1_tokens">3,204</span> tokens  (<span data-stat="session_p1_count">6</span> corrections)
  Pillar 4 (skill activations):   <span data-stat="session_p4_tokens">1,560</span> tokens  (<span data-stat="session_p4_count">4</span> activations)
  RTK rewrites:                   <span data-stat="session_rtk_tokens">2,749</span> tokens  (<span data-stat="session_rtk_count">11</span> rewrites)
  Lean-ctx wraps:                 <span data-stat="session_lean_tokens">1,228</span> tokens  (<span data-stat="session_lean_count">2</span> wraps)

详细分类(Pro)

$ precc savings --all
Session Token Savings (Detailed)
================================
Total estimated savings: <span data-stat="session_tokens_saved">8,741</span> tokens

Command-by-command:
  #  Time   Command                          Saving   Source
  1  09:12  cargo build                      534 tk   cd prepend (cargo-wrong-dir)
  2  09:14  cargo test                       534 tk   cd prepend (cargo-wrong-dir)
  3  09:15  git status                       412 tk   cd prepend (git-wrong-dir)
  4  09:18  npm install                      824 tk   cd prepend (npm-wrong-dir)
  5  09:22  find . -name "*.rs"              387 tk   RTK rewrite (output truncation)
  6  09:25  cat src/main.rs                  249 tk   RTK rewrite (lean-ctx wrap)
  7  09:31  cargo clippy                     534 tk   cd prepend (cargo-wrong-dir)
  ...

Pillar Breakdown:
  Pillar 1 (context resolution):   <span data-stat="session_p1_tokens">3,204</span> tokens  <span data-stat="session_p1_pct">36.6</span>%
  Pillar 2 (GDB debugging):            0 tokens   0.0%
  Pillar 3 (mined preventions):        0 tokens   0.0%
  Pillar 4 (automation skills):    <span data-stat="session_p4_tokens">1,560</span> tokens  <span data-stat="session_p4_pct">17.8</span>%
  RTK rewrites:                    <span data-stat="session_rtk_tokens">2,749</span> tokens  <span data-stat="session_rtk_pct">31.5</span>%
  Lean-ctx wraps:                  <span data-stat="session_lean_tokens">1,228</span> tokens  <span data-stat="session_lean_pct">14.1</span>%

如何估算节省

每种修正类型都有基于没有PRECC时会发生什么的估计token成本:

修正类型估计节省原因
cd prepend~500 tokens错误输出 + Claude推理 + 重试
技能激活~400 tokens错误输出 + Claude推理 + 重试
RTK rewrite~250 tokensClaude需要阅读的冗长输出
Lean-ctx wrap~600 tokens大文件内容被压缩
挖掘预防~500 tokens已知的失败模式被避免

这些是保守估计。实际节省通常更高,因为Claude对错误的推理可能很冗长。

累计节省

节省数据在PRECC数据库中跨会话持久化。随着时间推移,您可以跟踪总体影响:

$ precc savings
Session Token Savings
=====================
Total estimated savings: <span data-stat="session_tokens_saved">8,741</span> tokens

Lifetime savings: <span data-stat="total_tokens_saved">142,389</span> tokens across <span data-stat="total_sessions">47</span> sessions

状态栏

安装后,PRECC 会在 ~/.claude/settings.json 中写入一条 statusLine 条目,使 Claude Code 状态栏显示实时会话指标:

$0.42 spent | 1.2M in/out | 📊 last cmd: −1.2K | PRECC: 7 fixes | 5.8ms avg | this session: 320 saved over 7 cmds (~$0.05) | lifetime: 8.9K saved over 217 cmds (~$2.85)

设置 PRECC_LANG 以你的语言显示标签 — 请参阅本地化章节。

每个分段:

分段来源含义会话重启时重置?
$0.42 spentcost.total_cost_usdClaude Code 报告的累计会话费用
1.2M in/outtotal_input_tokens + total_output_tokens会话中非缓存的输入 + 输出 tokens
📊 last cmd: −1.2KPRECC 对最近一条 Bash 命令的测量通过重新运行原始命令测得的真实节省否(跨会话保留)
PRECC: 7 fixesmetrics.log本次会话的修正次数 — 仅计数,不含虚假 token 估算
5.8ms avgPRECC 钩子延迟 p50PRECC 处理每次工具调用所花的时间
bash 18% of totalpost_observations.log会话 token 中来自 Bash 输出的占比 — 解释为何 PRECC 的节省自然只是总成本的一部分(PRECC 仅优化 Bash 输出)
this session: 320 saved over 7 cmds (~$0.05).lifetime_summary.json − baseline每个会话的真实增量。当增量为零时隐藏(会话开始)是(基线重新快照)
lifetime: 8.9K saved over 217 cmds (~$2.85).lifetime_summary.json自首次安装 PRECC 以来累计节省的 tokens 和重新测量的命令,以及按当前每 token 费率估算的美元价值

lifetime: 分段放在最后,这样如果 Claude Code 的界面在右侧裁剪状态栏,它会最先被截断。

为何费用和 token 数不能相除

显示的 1.2M in/out 不是产生 $0.42 spent 的分母。Claude Code 的 cost.total_cost_usd 是根据 API 的完整 token 分项计算的——基础输入、输出,外加缓存读取和缓存创建。整个会话累计的缓存 token 数未在 statusline 模式中暴露,因此 PRECC 只能显示可见的(非缓存)部分。

在频繁重读文件的长会话中,缓存读取可能是可见 token 数的 10 倍。这就是为何将两者作为比率配对会产生误导——PRECC 改为将其显示为独立分段。

为何 PRECC 不计算费用

费用数字是权威的。PRECC 从 Claude Code 通过 stdin 传给状态命令的 JSON 中逐字读取 cost.total_cost_usd。这就是 Claude Code 对你订阅/用量预算扣费的同一个数字。你可以随时使用内置的 /cost 斜杠命令验证 — 两者应一致。

什么驱动费用

对于 Claude Opus 4.6:

Token typeStandard (≤200k context)1M context tier
Input$15 / MTok$30 / MTok
Output$75 / MTok$150 / MTok
Cache write$18.75 / MTok$37.50 / MTok
Cache read$1.50 / MTok$3 / MTok

长会话中最大的驱动通常是输出 tokens(每 token 最贵的类型,尤其在 1M 上下文层级)、重复的缓存读取(单次便宜但跨多轮快速累积),以及缓存创建(每次文件读取写入一次,约为基础输入费率的 1.25 倍)。PRECC 通过压缩 Bash 输出来降低可见 token 成本(📊 last cmd: 分段显示每条命令的节省),但无法减少 Claude 已加载文件的缓存读取。

稳定的会话计数

“PRECC: N fixes” 分段统计自持久化的会话开始以来的事件,会话开始在每个会话首次 statusline 刷新时写入 ~/.local/share/precc/sessions/<session_id>.start。这使计数单调 — 即使某次刷新中缺少 cost.total_duration_ms,会话中途也不会减少。

自动刷新的累计快照

lifetime: 分段读取 ~/.local/share/precc/.lifetime_summary.json,该文件在每次 PostToolUse 测量和每次 precc savings 调用时被重写。this session: 分段读取相同的 lifetime 文件,但减去在每个会话首次刷新时持久化的会话基线。无需手动刷新 — 文件会自动更新。

禁用状态栏

如果你更想保留现有的状态栏,请在 ~/.claude/settings.json 中设置你自己的 statusLine 命令。PRECC 的安装程序会检测到自定义值,并在后续更新中保持不变。

若只想抑制每次交互的 📊 PRECC 行(在 additionalContext 中),请在 shell 环境中设置 PRECC_QUIET=1

PRECC’s three savings mechanisms each have a counterpart in the recent literature. These are related work — the ideas PRECC’s design draws on. Their reported figures are their measurements, not PRECC’s: PRECC only ever quotes numbers measured on your own machine (see “measured, not estimated”, above).

  • Output/trajectory trimming (PRECC’s diet + bash-output compression) — Reducing Cost of LLM Agents with Trajectory Reduction (AgentDiet), FSE 2026, arXiv:2509.23586. Removes redundant/expired trajectory content post-hoc; reports −39.9–59.7% input tokens. PRECC applies the same idea pre-execution and deterministically (no extra LLM call).
  • Skills as programs (PRECC’s mined + builtin rewrite skills) — Harnessing LLM Agents with Skill Programs, arXiv:2605.17734. Frames reusable agent skills as executable program functions — the same analogy behind PRECC’s command-rewrite skills (a pattern → a deterministic rewrite).
  • Context compression (PRECC’s compress + lean-ctx wrapping) — Compress the Context, Keep the Commitments: A Formal Framework for Verifiable LLM Context Compression, arXiv:2605.17304. Recent work on compressing context without losing required information — the property PRECC’s deterministic, cache-stable rewrites aim to preserve.