Get the FREE Ultimate OpenClaw Setup Guide →

claude-code-tool-patterns

Scanned
npx machina-cli add skill shimo4228/claude-code-learned-skills/claude-code-tool-patterns --openclaw
Files (1)
SKILL.md
3.2 KB

Claude Code Tool Patterns & Gotchas

Extracted: 2026-02-09 (consolidated 2026-02-10) Context: Claude Code のツール使用時に発生しがちな問題とその回避策


1. Large File Write Performance

Problem

Very large files (>3000 lines) in a single Write tool call become extremely slow and may appear to hang.

Symptoms:

  • Write operation takes >60 seconds
  • User interrupts thinking it's stuck
  • No feedback on progress

Solution

Split large files into modular components:

  1. Identify logical boundaries (chapters, sections, domains)
  2. Create multiple focused files (200-800 lines each)
  3. Add navigation file (README/index) to tie them together
  4. Write files in parallel when possible
# BAD: Single monolithic file
/docs/architecture.md (4000 lines) → 90+ seconds

# GOOD: Modular files
/docs/architecture/
├── README.md (200 lines)
├── 00-overview.md (600 lines)
├── 01-adr-backend.md (500 lines)
└── ...
→ Each write completes in ~5-10 seconds

2. File Edit Refresh Pattern

Problem

Edit tool fails with "File has been modified since read" when file state has changed since last Read.

Solution

Always Read immediately before Edit:

# BAD: Edit without recent Read
- Earlier: Read file.py
- (Multiple other operations)
- Later: Edit file.py  # May fail

# GOOD: Read immediately before Edit
- Earlier: Read file.py
- (Multiple other operations)
- Later: Read file.py   # Refresh state
- Then: Edit file.py    # Safe to edit

When to Apply

  • Before every Edit call in sessions with multiple file operations
  • After file state might have changed through other tools
  • In long-running sessions (context near limits)
  • When you see "File has been modified" errors

3. Hook Command JSON Escape Trap

Problem

settings.json の hooks にシェルコマンドを直接書くと、\", $, \n などの 特殊文字が JSON パースエラー("Bad control character in string literal")を引き起こす。

Solution

外部スクリプトファイルに切り出す:

# ~/.claude/hooks/my-hook.sh
#!/bin/bash
file=$(jq -r '.tool_input.file_path // empty')
if [ -n "$file" ] && echo "$file" | grep -q '\.py$'; then
  ruff format "$file" 2>/dev/null
fi
exit 0
// settings.json — シンプルなコマンドだけを記述
{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Edit|Write",
      "hooks": [{
        "type": "command",
        "command": "bash ~/.claude/hooks/my-hook.sh"
      }]
    }]
  }
}

When to Apply

  • Hook コマンドに引用符、変数展開、パイプ、条件分岐が含まれる場合
  • settings.json の JSON validation エラーが出た場合

When to Use

  • Planning to write a file >1000 lines
  • Editing files after many intervening tool calls
  • In complex multi-step workflows with frequent file operations
  • Hook に複雑なシェルコマンドを設定する場合

Source

git clone https://github.com/shimo4228/claude-code-learned-skills/blob/main/skills/claude-code-tool-patterns/SKILL.mdView on GitHub

Overview

Claude Code users often run into slow writes on very large files, stale edits after many operations, and JSON-escaping issues when configuring hooks. This skill provides practical patterns to split big files, refresh state before edits, and move complex hook logic to external scripts for reliability.

How This Skill Works

Split very large files into modular components (200-800 lines each) and add a central README to tie them together to improve write performance. Before editing, read the latest file state to avoid modified-since-read errors. For hooks, move complex shell commands to external scripts and reference them from settings.json to prevent JSON parsing errors.

When to Use It

  • Planning to write a file >1000 lines
  • Editing files after many intervening tool calls
  • In complex multi-step workflows with frequent file operations
  • Hook commands with complex shell syntax in settings.json
  • Facing slow, monolithic file writes and wanting faster feedback by modularization

Quick Start

  1. Step 1: Plan modularization by splitting large files (>1000 lines) into 200-800 line modules and add a README index
  2. Step 2: Implement modular files, write in parallel when possible, and ensure a central index ties them together
  3. Step 3: Externalize complex hook commands to scripts and call them from settings.json; always read the latest file state before edits

Best Practices

  • Split large files into modular components (200-800 lines) and add a navigation README
  • Identify logical boundaries and create focused files
  • Write files in parallel when possible
  • Always Read immediately before Edit in long-running sessions or after state changes
  • Externalize hook logic into scripts and keep settings.json simple to avoid JSON errors

Example Use Cases

  • Slow writes on a 4000-line architecture.md; fix by modularizing under /docs/architecture with README.md and smaller files, bringing writes to ~5–10 seconds
  • Edit without a fresh Read caused a modified-since-read error; solution is to Read immediately before Edit
  • Hook JSON Escape Trap: JSON validation error when including shell commands in settings.json; solved by external shell script and simple JSON in settings
  • Complex hook logic in a multi-step workflow benefits from external scripts, reducing JSON complexity
  • Plan to write a file >1000 lines and use modularization; maintain a central index for navigation

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers