工具链专题 · 子页 基于源码逆向 · 配置即投资

Claude Code
工具链配置指南

一个未经配置的 Claude Code 就像一个没带工具箱的工程师。 基于源码逆向分析,从代码层面还原 Claude Code 真正需要什么,以及为什么配置完善能省 Token、省时间、省心智负担。

5必装工具
4推荐套件
30-70%Token 节省
50%+轮次减少

必装工具 ★★★★★

Claude Code 的架构本质是"大脑 + 工具"。大脑是 Claude 模型本身,工具是它在你机器上能调用的外部命令。每少装一个,Claude 的能力就少一块。

🌿 git 必装

版本控制基础。启动时调用 getIsGit()getBranch()findGitRoot() 等数十个函数,整个工作流建立在 git 之上。

git --version
🔍 ripgrep (rg) 必装

Grep 工具的实体。不装则 Grep 工具直接失效。源码中有专门的 RIPGREP_READ_ONLY_COMMANDS 安全白名单。

brew install ripgrep
🐙 gh (GitHub CLI) 必装

所有 GitHub 操作的入口。源码 BashTool/prompt.ts 明确要求用 gh 处理 PR / Issue / CI / Releases。

brew install gh && gh auth login
🖥️ tmux 必装

Swarm 多 Agent 并行模式和后台任务必须。源码错误提示直接要求 brew install tmux

brew install tmux
📄 poppler 必装

PDF 文件读取支持。源码 FileReadTool.ts 明确要求安装 poppler-utils。

brew install poppler

语言服务器 ★★☆☆☆

LSP 解决的核心问题:让 Claude 不再需要"猜"类型和引用关系。没有 LSP 时 Claude 只能通过搜索文本来推断类型,有了 LSP 一次调用就能获得精确的类型信息。

📘 TypeScript
npm install -g typescript typescript-language-server
🐍 Python
npm install -g pyright
🚀 Go
go install golang.org/x/tools/gopls@latest
⚙️ Rust
rustup component add rust-analyzer

配置指南

1. 全局规则 ~/.claude/CLAUDE.md

# 我的全局 Claude 规则
## 代码风格
- 用中文写注释
- 函数不超过 50 行,超了就拆分
- 不用 any 类型,找合适的类型替代

## Git 规范
- commit message 用中文,格式:feat/fix/refactor: 描述
- 每个 commit 只做一件事

## 禁止事项
- 不要在生产代码里用 console.log
- 不要用 == 用 ===
- 不要用 var 用 const/let

## 工具偏好
- 搜索代码先用 Grep/Glob,不要用 Bash
- 写新文件前先 Read 同类文件参考风格

2. 权限预配置 ~/.claude/settings.json

这是降低 Token 消耗最立竿见影的配置。每次权限确认弹框都意味着一次额外的 API 轮次。

{
  "permissions": {
    "allow": [
      "Bash(git status)", "Bash(git diff*)", "Bash(git log*)",
      "Bash(git show*)", "Bash(git branch*)", "Bash(git stash*)",
      "Bash(gh pr*)", "Bash(gh issue*)", "Bash(gh repo*)",
      "Bash(rg *)", "Bash(fd *)", "Bash(sg *)", "Bash(jq *)",
      "Bash(cat *)", "Bash(ls *)", "Bash(find *)",
      "Bash(node --version)", "Bash(npm run *)", "Bash(pnpm *)", "Bash(yarn *)"
    ]
  }
}

Token 经济学

配置完善到底能省多少?从源码角度看,效率提升来自三个维度:

1. 减少 API 轮次(最直接)

场景未配置配置完善节省
读 500 行文件Bash cat → ~2000 token 原始输出Read 工具 → 结构化 + 行号范围~30-50%
搜索函数Bash grep -r → 大量冗余匹配Grep(rg) → 精确匹配 + 上下文控制~40-60%
查看 git 状态需确认 → 额外一轮 API 往返白名单内 → 零确认直接执行省一整轮
处理 PR + Issue手动拼 API URL → 多轮尝试gh 命令 → 一步到位省 2-5 轮
长会话 50+ 轮频繁 compact,丢失上下文输出精简,compact 间隔更长上下文更完整

2. Prompt Cache 机制

Claude API 的 prompt cache 让相同前缀(system prompt + 工具定义 + CLAUDE.md)走缓存读取,成本只有正常 input token 的 10%。配置改好后尽量不要频繁调整,避免打破缓存前缀导致一次完整的缓存重建(约 5000-10000 token 的 cache_creation)。

3. Auto-Compact 与 Microcompact

当 token 接近上下文窗口限制时触发 auto-compact,把历史对话浓缩成摘要。如果你的每次工具调用都产生大量冗余输出,compact 就会更频繁地触发,每次 compact 本身也要消耗 token。专用工具的结构化输出天然更容易被高效 compact。

源码中还有 microcompact 机制,在发送 API 请求前自动清理旧的工具结果,只保留最近几次。那些产生了大量输出的 Bash 命令,它们的结果最终也会被清理掉——但你已经为发送它们付过一次费了

一个真实场景的 Token 对比

假设在 TypeScript 项目中重构一个函数:

维度未配置配置完善
工具结果 Token~18,700~4,800
权限确认轮次3 次0 次
节省比例~74%

一键安装脚本

macOS

#!/bin/bash
set -e
echo "=== 安装 Claude Code 依赖工具 ==="

if ! command -v brew &>/dev/null; then
  echo "安装 Homebrew..."
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

echo "--- 必装工具 ---"
brew install git ripgrep tmux poppler gh ast-grep fd jq git-delta

echo "--- 登录 GitHub CLI ---"
gh auth login

echo "--- TypeScript LSP ---"
npm install -g typescript typescript-language-server pyright

echo "--- Playwright 浏览器 ---"
npx playwright install chromium

echo "=== 验证安装 ==="
for cmd in git gh rg tmux sg fd jq tsc; do
  if command -v "$cmd" &>/dev/null; then
    echo "✓ $cmd: $(command -v $cmd)"
  else
    echo "✗ $cmd: 未安装"
  fi
done

echo "✓ 全部安装完成!"

验证脚本 check-claude-tools.sh

#!/bin/bash
check() {
  if command -v "$1" &>/dev/null; then
    echo "✓ $1: $(command -v $1)"
  else
    echo "✗ $1: 未安装"
  fi
}

echo "=== Claude Code 工具检查 ==="
echo "--- 必须 ---"; check git; check rg; check gh; check tmux
echo "--- 推荐 ---"; check poppler; check sg; check fd; check jq
echo "--- LSP ---"; check typescript-language-server; check tsc; check pyright
echo "--- 体验 ---"; check delta; check lazygit

if command -v gh &>/dev/null; then
  if gh auth token &>/dev/null 2>&1; then
    echo "✓ gh: 已认证登录"
  else
    echo "⚠ gh: 已安装但未登录,运行 gh auth login"
  fi
fi

工具重要度总览

★★★★★ 必须安装 git · ripgrep · gh · tmux · poppler
★★★★☆ 强烈推荐 ast-grep · fd · jq · git-delta
★★☆☆☆ 语言服务器(按需) typescript-language-server · pyright · gopls · rust-analyzer
★★☆☆☆ 体验增强 git-delta · lazygit