Lesson 4 of 5 5 min

Claude Code Lesson 3: Advanced Tool Mastery (Grep, Replace, Shell)

Go under the hood of the agent's surgical toolbox. Learn how to use grep_search for symbol mapping and how to avoid the "Ambiguous String" error in the replace tool.

Mastering the Instrument

To a beginner, Claude Code is a sophisticated text box. To a Staff Engineer, it is a Surgical Workstation. Each tool in the default_api is a precision instrument optimized for a specific stage of the engineering lifecycle.

Mastering these tools is how you move from "AI suggestions" to "AI-driven delivery." In this lesson, we explore the internal mechanics of the three most powerful tools in the agent's arsenal.

1. grep_search: The Brain's Map

The grep_search tool is the most important tool for the Research Phase. It is essentially a high-performance, ripgrep-powered engine that allows the agent to build a mental map of your symbols.

Advanced Usage: RegEx and Logic

Don't just search for strings. Ask Claude to use regex to find anti-patterns.

"Find all instances of .subscribe() in the components folder that do not have a corresponding .unsubscribe() or takeUntil() call."

The "Context" Optimization

The grep_search tool supports context, after, and before parameters. Staff-Tier Tip: By requesting 5 lines of context around every match, the agent can often understand the surrounding logic without needing a separate read_file call. This saves thousands of tokens and reduces the number of turns required to finish a task.

2. replace: The Surgical Knife

The replace tool is the primary way Claude modifies your code. It is safer than write_file because it only targets specific lines, preserving the rest of the file's integrity and formatting.

The "Ambiguous String" Failure (And how to fix it)

The most common error in Claude Code is: "Failed to edit, multiple occurrences found for old_string."

This happens when the agent tries to replace a common line like return true; without providing enough context. If a file has five return true; statements, the tool doesn't know which one to "cut."

The Staff-Tier Solution: Significant Context

Always encourage the agent to include 3-5 lines of context in the old_string. Instead of: old_string: "return true;" Use: old_string: "if (user.isAdmin) {\n logger.info('Admin access granted');\n return true;\n }"

This makes the "String Fingerprint" unique, ensuring the replacement happens exactly where intended.

3. run_shell_command: The Reality Check

This tool allows Claude to step out of the "LLM Simulation" and interact with the Physical System. It is the bridge between a "Proposed Fix" and a "Verified Solution."

Critical Staff-Tier Workflows:

  • Linting as a Guardrail: After every refactor, the agent should run npm run lint or ruff check. This ensures the AI hasn't introduced stylistic debt.
  • Type-Check Verification: Running tsc (TypeScript Compiler) is mandatory for large refactors. It catches interface mismatches that the AI might have missed across different files.
  • Dependency Management: The agent can run npm install to add new tools, but it should always check the package.json first to avoid version conflicts.

The "Is Background" Flag

For long-running tasks like starting a dev server or running a heavy integration test suite, the is_background: true flag allows the agent to continue working on other tasks while the command runs in the background. You can check the output later using read_background_output.

4. Tool Comparison Table

Tool Best For... Staff-Tier Mastery Tip
read_file Deep understanding. Only read relevant line ranges (start to end).
grep_search Mapping symbols. Use include_pattern to narrow search to src/.
replace Surgical fixes. Provide at least 2 lines of prefix/suffix context.
write_file New files/Prototypes. Never use for large files (>500 lines). Use replace.
glob Structure analysis. Use it to find all .test.ts files to verify coverage.

5. Visualizing the Sychronization Logic

Claude Code executes tools in Parallel by default to maximize speed. However, engineering tasks are often Sequential.

sequenceDiagram
    participant A as Agent
    participant F as File System
    participant S as Shell
    
    Note over A,S: Task: Create a new module and test it
    A->>F: mkdir("new_module")
    A->>F: write_file("new_module/logic.ts") [Wait: true]
    Note right of F: Wait prevents writing before folder exists
    A->>F: write_file("new_module/logic.test.ts")
    A->>S: run_shell_command("npm test") [Wait: true]
    S-->>A: [Tests Passed]

6. Managing Tool Failures

When a tool fails, a senior engineer doesn't just retry the same command. They Investigate.

  • If replace fails: The agent should run read_file on the surrounding lines to see if the file content changed unexpectedly.
  • If run_shell_command fails: The agent should read the last 50 lines of the error log to find the root cause (e.g., a missing environment variable).

Final Takeaway

Treat the Claude Code tools like high-end CLI utilities. Understand their constraints, maximize their context efficiency, and always enforce a "Validation-First" workflow. A change is not "Done" until the compiler and the linter agree it is correct.

By mastering these surgical instruments, you transform Claude from a chatbot into a powerful extensions of your own engineering intent.

Want to track your progress?

Sign in to save your progress, track completed lessons, and pick up where you left off.