必装工具 ★★★★★
Claude Code 的架构本质是"大脑 + 工具"。大脑是 Claude 模型本身,工具是它在你机器上能调用的外部命令。每少装一个,Claude 的能力就少一块。
版本控制基础。启动时调用 getIsGit()、getBranch()、findGitRoot() 等数十个函数,整个工作流建立在 git 之上。
git --version
Grep 工具的实体。不装则 Grep 工具直接失效。源码中有专门的 RIPGREP_READ_ONLY_COMMANDS 安全白名单。
brew install ripgrep
所有 GitHub 操作的入口。源码 BashTool/prompt.ts 明确要求用 gh 处理 PR / Issue / CI / Releases。
brew install gh && gh auth login
Swarm 多 Agent 并行模式和后台任务必须。源码错误提示直接要求 brew install tmux。
brew install tmux
PDF 文件读取支持。源码 FileReadTool.ts 明确要求安装 poppler-utils。
brew install poppler
推荐工具 ★★★☆☆
这套工具解决的核心问题:让 Claude 用更少的 Token 找到更精确的信息。
结构化代码搜索,精度远超纯文本搜索。支持 AST 级别的精确匹配和重构规则。
brew install ast-grep
现代 find 替代品,比系统 find 快 3-5 倍,自动跳过 .gitignore 中的文件。
brew install fd
JSON 命令行处理。Claude 处理 API 返回、package.json、配置文件时频繁用到。
brew install jq
更好的 diff 显示。Claude 查看 git diff / git show 时更清晰。
brew install git-delta
语言服务器 ★★☆☆☆
LSP 解决的核心问题:让 Claude 不再需要"猜"类型和引用关系。没有 LSP 时 Claude 只能通过搜索文本来推断类型,有了 LSP 一次调用就能获得精确的类型信息。
npm install -g typescript typescript-language-server
npm install -g pyright
go install golang.org/x/tools/gopls@latest
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