37

Claude Code Examples

⏱️ 30 min

Claude Code Examples

This chapter collects real-world Claude Code usage patterns and translates them into actionable engineering habits. The goal: understand why Claude Code stays stable during long tasks, and how to borrow its context management approach.

  • Claude Code uses CLAUDE.md as procedural memory.
  • Exceeding 95% of the context window triggers auto-compact.
  • File-system-as-memory + grep/head/tail is the core capability.
  • Local MCP servers can be added quickly with claude mcp add.
  • Tool responses are capped at 25,000 tokens by default to prevent context from being consumed.

What You'll Learn

  • Claude Code's context management patterns and when to use them
  • How to use CLAUDE.md as a project-level rules entry point
  • How to integrate MCP servers and evaluate tools
  • How to design token-efficient tool responses

Example 1: CLAUDE.md as Procedural Memory

Claude Code treats CLAUDE.md as project-level rules and operational norms (procedural memory). It's a "fixed-load" context that gives the model a consistent behavioral baseline on every inference.

Practical tips:

  • Use CLAUDE.md to record key norms and constraints
  • Don't cram details into the system prompt -- put them in CLAUDE.md
  • Rules should be "actionable and verifiable" -- no fluff

Example 2: Auto-Compact at 95% Context Window

Claude Code triggers auto-compact when context usage exceeds 95%, summarizing and compressing the full conversation. It's automated context hygiene.

You can implement something similar in your own system:

  • Set a threshold (e.g., 70-80% or 90-95%)
  • Generate structured summaries (Files/Decisions/Next Steps)
  • Keep the most recent N turns for continuity

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

Claude Code uses file system operations to "load on demand" rather than stuffing everything into context. Common methods include grep/head/tail to quickly locate and read file fragments.

Core principles:

  • Keep lightweight references (file paths / query strings)
  • Read when needed -- don't do a one-time full load
  • Small incremental reads are more stable than one big bulk load

Example 4: MCP Server Integration

When you have your own tools or MCP servers, you can wire them into Claude Code directly:

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

This lets local MCP servers be called directly in Claude Code for quick tool usability testing.

Recommended approach:

  • Get the local prototype running first
  • Use Claude Code for exploratory calls
  • Collect failure patterns and improve tool descriptions based on them

Example 5: Token-Efficient Tool Responses

Claude Code caps tool responses at 25,000 tokens by default, preventing context from being consumed by tool outputs. Key strategies:

  • Use pagination / filtering / truncation
  • Provide both concise and detailed response formats
  • Use error responses to guide more precise calls

Very effective for reducing context sprawl.

Example 6: Evaluation with Claude Code

Claude Code can quickly generate prompt/response pairs to help you build evaluation tasks. Tips:

  • Tasks should be close to real scenarios, not toy examples
  • Tasks should include multi-step tool calls
  • Don't make the verifier too strict -- format differences shouldn't count as failures

Quick Checklist

  • Do you have a CLAUDE.md as project-level rules?
  • Is auto-compact or compaction triggers set up?
  • Are you using file-system-as-memory?
  • Can your MCP server be called directly from Claude Code?
  • Do tool responses have a default token limit?

Practice Task

  • Create a CLAUDE.md with 5 project rules
  • Use claude mcp add to integrate a local MCP server
  • Design a 3-step tool call task and measure its context cost

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

📚 相关资源

❓ 常见问题

关于本章主题最常被搜索的问题,点击展开答案

CLAUDE.md 在 Claude Code 里到底起什么作用?

Claude Code 把 CLAUDE.md 当 procedural memory — 项目级规则与操作规范,每次推理都默认加载。所以细节规则(命名、提交、API 风格)放 CLAUDE.md 而不是塞 system prompt;规则要可执行、可验证,避免空话("代码要简洁"是空话,"单文件 ≤ 500 行"是规则)。

为什么 Claude Code 在 95% 时才触发 auto-compact?

95% 是「再不压就要被截断」的临界点 — 早压会损失对话连续性,太晚会被硬截。auto-compact 不是删消息而是生成结构化 summary(Files / Decisions / Next Steps),保留最近 N 轮维持上下文。自己实现类似机制时阈值可调到 70-80% 更稳,95% 留给真的扛不住才动。

claude mcp add 这条命令是干嘛的?

把本地 MCP server 接入 Claude Code 的快捷方式:claude mcp add <name> <command> [args...],加完之后 Claude Code 直接能调用你的 tool,方便做 usability 测试 — 跑 prototype、看 tool description 好不好懂、收失败模式反向改 schema。比写 client 集成代码省一周。

Tool response 25,000 token 上限是从哪来的?

Claude Code 默认配置 — 防止 tool output 一口吞掉 context。常见场景:cat 大文件、ls 整个 monorepo、API 列表查询返回 1000 条。超过 25K 就被截断 + 提示。配套设计两套 response format:concise(默认)和 detailed(按需),错误响应里引导更精确的调用,避免反复全量重试。

用 Claude Code 搭 evaluation tasks 要注意什么?

三个关键点:(1) 任务贴近真实场景,别搞 toy example,否则评出来的能力跟生产无关;(2) 任务要包含多步工具调用,单步任务测不出 context 管理能力;(3) verifier 别过度严格,格式差异(多个空格、引号风格)就判错会让评测噪声大于信号,需要按语义判等。