Chapter 9: Claude Code Fundamentals — The CLI Agent That Rewrites Your Codebase

This is Part 9 of a series walking through the book Master Claude Chat, Cowork and Code — From Prompting to Operational AI. In the previous chapter, we completed the Cowork trilogy with scheduled tasks and autonomous execution. Now Part IV begins — and the audience shifts. Chapter 9 enters the developer's terminal with Claude Code, a CLI agent that doesn't just suggest changes but directly reads, analyzes, and modifies your codebase.


Not a Chatbot. A CLI Agent.

Claude Code is fundamentally different from Claude Chat. Rather than sending messages to a conversational interface, you invoke it as a command-line tool that operates directly on your files. The book opens Chapter 9 by making this distinction crystal clear — and explaining why it matters.

When you invoke Claude Code with a task, the CLI reads your codebase into memory, constructs a structured prompt that includes file contents and project structure, sends this to the Claude API, and processes the response to generate or modify files. This isn't free-form text requiring manual interpretation — it's a structured set of operations that the CLI executes programmatically.

The philosophy behind Claude Code differs fundamentally from Chat. While Chat prioritizes user control and step-by-step reasoning, Claude Code embraces autonomous file system manipulation, parallel operations, and multi-file refactoring. That power comes with responsibility, and Chapter 9 spends as much time on safety as it does on capability.


Under the Hood: How Claude Code Actually Works

The book dedicates a full section to the internal mechanics, and I found this essential reading for using the tool effectively. When you execute a command, Claude Code constructs a representation of your codebase — reading relevant files, building a directory tree, creating file summaries. This context, combined with your task instructions, is what Claude uses to generate structured changes.

Key Idea from the Book: One of Claude Code's most powerful capabilities is understanding relationships between files. When analyzing code for refactoring, Claude processes not just individual files but their interdependencies — enabling truly sophisticated refactoring that maintains correctness across module boundaries.

The execution layer then creates a plan of file operations: which files to create, modify, or delete. This plan respects your permission settings and can halt if it encounters operations you haven't authorized. For developers building automation, understanding this architecture is crucial because it means Claude Code can be constrained and controlled through permission management.


From Simple Commands to Intelligent Refactoring

The basic syntax is straightforward: claude-code "your task description here". But the range of what you can accomplish is vast. The book walks through several concrete examples, starting with a scattered utilities module that needs consolidation.

You invoke: claude-code "Consolidate the scattered utility files in src/utils/ into a single well-structured utils module with proper exports, add TypeScript types, and add comprehensive JSDoc comments"

Claude Code reads all the files, understands their structure and purpose, and generates a refactoring plan — grouping related functions, creating proper exports, adding input validation, comprehensive documentation, and better error handling. The book includes the before-and-after code, and what struck me was how Claude Code didn't just reorganize — it improved the code along the way.

Key Idea from the Book: Claude Code excels at tasks that would be tedious to perform manually. Updating dozens of API route files to use a new middleware pattern? Describe the task once and Claude Code handles the systematic updates — finding all matching files, understanding both the old and new patterns, applying the transformation consistently, and preserving any custom logic specific to individual routes.

The book also covers generating boilerplate — creating a new database model that analyzes your existing patterns and conventions and generates a file that perfectly fits your codebase architecture. This is far more efficient than copying and modifying existing files manually.


Git Worktrees: Parallel Development Without the Risk

One of the most powerful sections in Chapter 9 covers combining Claude Code with Git worktrees. A worktree gives you multiple working directories from the same repository, each on a different branch. This enables parallel development: Claude Code works on one branch while you work on another, and you merge changes systematically.

The workflow is clean: create a worktree for an experimental refactoring, run Claude Code on that branch, review changes locally, and merge only if satisfied. If Claude Code made mistakes, discard the worktree entirely — your main branch is untouched.

The book provides the exact commands:

git worktree add .claude/worktrees/refactor-auth auth-refactor-branch

Then run Claude Code in that isolated directory. Meanwhile, your main branch continues working unaffected. When ready, review with git diff main..HEAD and merge or discard.

Important from the Book: For large refactoring projects, consider a staging worktree where Claude Code iteratively improves code. Run it once to generate initial changes, review, then ask for further improvements on the same worktree. This iterative refinement is easier to control than trying to get everything right in one invocation.

This pattern scales beautifully — multiple worktrees for different refactoring initiatives, each with isolated risk and systematic code review before integration.


Permissions: The Safety Net You Must Understand

Chapter 9 closes with a critical section on permission management. By default, Claude Code operates in interactive mode — requesting approval for every file operation. This is the safest approach for most development work.

For CI/CD pipelines, auto-accept mode (--auto-accept) bypasses these prompts. But the book is emphatic about the danger: auto-accept without scope restrictions is "a recipe for disaster." The safer pattern uses restricted scope with glob patterns:

claude-code --scope="src/controllers/**" --auto-accept "Add error logging to all API endpoints"

This limits what Claude Code can read and modify, providing a safety guardrail even in automated pipelines. The book walks through a production-ready CI/CD configuration that combines scope restriction, auto-accept, proper git commits, and branch protection rules requiring code review before merging.

Key Idea from the Book: Never use auto-accept mode without scope restrictions. Even in automated pipelines, always scope Claude Code to the specific files it needs to modify. Use branch protection rules to require code review of any auto-generated changes before merging to main.

What Chapter 9 Sets Up

This chapter lays the foundation for Part IV — you now understand what Claude Code is, how it works internally, how to use it for everything from simple refactoring to complex multi-file operations, and how to keep it safe with worktrees and permissions.

But power without discipline is dangerous. Chapter 10: Safe Legacy Code Refactoring tackles the hardest problem in AI-assisted development: how do you refactor legacy code without introducing subtle bugs that slip past your test suite? The chapter opens with real horror stories — silent authorization bypasses, race condition amplifiers, implicit contract breakers — and then builds a disciplined methodology around characterization tests, incremental verification, and the safety practices that make large-scale AI refactoring trustworthy.


Master the CLI agent. Chapter 9 includes full installation and setup walkthroughs, under-the-hood architecture explanations, complete refactoring examples with before-and-after code, the Git worktree workflow, and production-ready CI/CD permission configurations. Get your copy of Master Claude Chat, Cowork and Code and start letting Claude Code handle the tedious refactoring work.
2026-03-10

Sho Shimoda

I share and organize what I’ve learned and experienced.