logo
34

Claude Code Examples

⏱️ 30分钟

Claude Code Examples

本章汇总 Claude Code 的真实使用模式,把它们翻译成可操作的工程习惯。目标是帮助你理解:为什么 Claude Code 在长任务里更稳定,以及如何借鉴它的 Context 管理方式。

  • Claude Code 使用 CLAUDE.md 作为 procedural memory。
  • 超过 95% Context window 会触发 auto-compact。
  • file-system-as-memory + grep/head/tail 是核心能力。
  • 本地 MCP server 可用 claude mcp add 快速接入。
  • Tool responses 默认限制 25,000 tokens,避免 Context 被吞噬。

你将学到什么

  • Claude Code 的 Context 管理模式与适用场景
  • 如何把 CLAUDE.md 作为项目级规则入口
  • 如何接入 MCP server 并做工具评测
  • 如何设计 token-efficient tool responses

Example 1: CLAUDE.md as Procedural Memory

Claude Code 会把 CLAUDE.md 作为项目级规则与操作规范(procedural memory)。这是一种“固定装载”的 Context,让模型在每次推理时都有统一的行为基线。

实践建议:

  • 用 CLAUDE.md 记录关键规范与约束
  • 不要把细节塞进 system prompt,而是放在 CLAUDE.md
  • 规则要“可执行、可验证”,避免空话

Example 2: Auto-Compact at 95% Context Window

Claude Code 在 Context 使用超过 95% 时触发 auto-compact,对完整对话进行总结与压缩。这是一种“自动化 Context hygiene”。

你可以在自己的系统里实现类似机制:

  • 设置阈值(如 70-80% 或 90-95%)
  • 生成结构化 summary(Files/Decisions/Next Steps)
  • 保留最近 N turns 以维持连续性

Example 3: File-System-as-Memory (Just-in-Time Context)

Claude Code 借助 file system 操作来“按需加载”信息,而不是把所有内容塞进 Context。常用方式包括 grep/head/tail 等命令,快速定位并读取文件片段。

核心原则:

  • 保留轻量引用(file paths / query strings)
  • 需要时再读取,不做一次性全量加载
  • 小步检索比一次性大块加载更稳定

Example 4: MCP Server Integration

当你有自己的 tool 或 MCP server 时,可以用 Claude Code 直接接入:

claude mcp add <name> <command> [args...]

这让本地 MCP server 可以直接在 Claude Code 中调用,快速做 tool usability 测试。

建议做法:

  • 先跑通本地 prototype
  • 用 Claude Code 做探索式调用
  • 收集失败模式,反向改进 tool description

Example 5: Token-Efficient Tool Responses

Claude Code 默认限制 tool responses 为 25,000 tokens,避免 Context 被 tool outputs 吞噬。这个策略的关键是:

  • 使用 pagination / filtering / truncation
  • 提供 concise 与 detailed 两种 response format
  • 通过 error responses 引导更精确的调用

这对减少 Context 扩散非常有效。

Example 6: Evaluation with Claude Code

Claude Code 可以快速生成 prompt/response pairs,帮助你搭建 evaluation tasks。建议:

  • 任务要贴近真实场景,而不是 toy example
  • 任务应包含多步工具调用
  • verifier 不要过度严格,避免因格式差异判错

Quick Checklist

  • 是否有 CLAUDE.md 作为项目级规则?
  • 是否设置了 auto-compact 或 compaction triggers?
  • 是否使用 file-system-as-memory?
  • MCP server 是否能用 Claude Code 直接调用?
  • tool responses 是否有默认 token 限制?

Practice Task

  • 建一个 CLAUDE.md,写 5 条项目规则
  • claude mcp add 接入一个本地 MCP server
  • 设计一个 3 步工具调用任务,测试其 Context 成本

Integration

This page complements Context Engineering topics by showing production patterns used in Claude Code. It pairs well with:

  • context-fundamentals
  • context-compression
  • tool-design

References

  • Agent Skills for Context Engineering: blogs.md
  • Agent Skills for Context Engineering: claude_research.md

Skill Metadata

Created: 2025-12-24 Last Updated: 2025-12-24 Author: JR Academy Version: 1.0.0