Claude Code How It Work
**
**Simply put**: Claude Code is like a super smart programming partner that automatically helps you write code, fix bugs, run commands, search files on your computer... almost everything you can do in the terminal!
This section will help you thoroughly understand its underlying principles (agent loop, models, tools, etc.). After reading, you'll understand why it's so powerful and how to use it more effectively.
* * *
## Claude's Workflow
When you give Claude a task, it thinks in cycles like a human, following three steps:
1. **Gather context** (look at your code, files, error messages)
2. **Take action** (edit files, run commands, search, etc.)
3. **Verify results** (check if successful, what still needs adjustment)
These three steps keep cycling until the task is complete. Of course, we can interrupt it at any time, give new instructions, or say "try a different approach."
Although Claude can work autonomously, it always follows your command!
Prompt β Get Context β Execute Action β Verify Result β (Loop) β Complete
!(#)
The entire loop is driven by two things:
* **Model** (responsible for thinking and reasoning)
* **Tools** (responsible for actually getting things done)
* * *
## Model (Claude's Brain)
Claude Code uses Anthropic's Claude model to understand code and plan tasks. It can read almost all programming languages, knows your project structure, and can break complex tasks into small steps to execute one by one.
**Recommended Models**:
* **Sonnet**: Best for everyday coding tasks (fast and cost-effective)
* **Opus**: Suitable for particularly complex architectural design and deep reasoning
However, using official models in China is not very user-friendly, so we can switch to other domestic models.
**Switching Models**:
* Enter `/model` in the prompt box
* Or use `claude --model sonnet` at startup
!(#)
* * *
## Tools (What Enables Claude to Actually Get Things Done)
Without tools, Claude can only chat. With tools, it can actually modify your code!
Claude has five built-in categories of tools:
* **File Operations**: Read files, modify code, create new files, rename
* **Search**: Quickly find code by filename or regular expressions
* **Execute Commands**: Run npm, git, tests, start servers, etc.
* **Web Search**: Look up documentation, search for error messages
* **Code Intelligence**: View type errors, jump to definitions, find references (requires installing corresponding language plugins)
**Real-world Example** (when fixing a failing test):
1. Run tests to see what failed
2. Read the error logs
3. Search for related source files
4. Read the files to understand the logic
5. Modify the code
6. Run tests again to verify
This is what the agent loop actually looks like!
**Advanced Extensions**: You can also use skills, MCP (external services), hooks (automation), and subagents to make it even more powerful.
* * *
## What Claude Can Access
Claude Code can see your entire project (unlike regular AI that can only see the currently open files):
* Your project folder and all subfiles
* Any command that can be executed in the terminal (npm, git, build tools, etc.)
* Current Git status (branches, uncommitted changes, commit history)
* **CLAUDE.md** file (you can write project-specific rules here so Claude remembers them every time)
* **Automatic Memory** (it remembers your project habits, stored in the first 200 lines of MEMORY.md)
* Extensions you've configured (MCP, skills, etc.)
This is why it can coordinate modifications across multiple files, automatically run tests, and even help you commit code.
* * *
## Execution Environment and Interface
Claude Code can run in **three environments**:
* **Local** (default): Fully on your computer with maximum permissions
* **Cloud** (Anthropic virtual machine): Suitable for large projects or environments you don't have locally
* **Remote Control**: Operate from a browser, but code still stays on your local machine
Interface support: Terminal, VS Code extension, JetBrains, claude.ai/code, Slack, CI/CD, etc. The underlying working principles are exactly the same!
* * *
## Working Across Branches (Seamless Use Even with Git Branch Switching)
Each conversation in Claude Code is **bound to the current folder** (not bound to a Git branch). This brings a very practical feature:
* When you **switch Git branches** (`git checkout` or `git switch`), Claude will **automatically see the files from the new branch**, but your **chat history remains unchanged**.
* Claude still remembers all the content and decisions discussed before, even across branches you won't lose context.
* When you restore an old conversation, it will only show the session records **in the current folder** (if different branches are in the same folder, they share history).
**Parallel Multi-Branch Development Tool**:
It's recommended to use `git worktrees` to create an **independent directory** for each branch. This way, you can open multiple Claude Code sessions at the same time, each branch having its own completely independent and clean conversation records without interfering with each other.
* * *
## Resume or Fork Sessions (Continue the Conversation / Start a New Thread)
Claude Code supports three flexible session operations, allowing you to pick up from previous progress or try different solutions at any time:
#### 1. Resume Session Directly (Most Common)
Command:
claude --continue
# or
claude --resume
!(#)
**Effect**:
* Continue from where you left off using the **same session ID**
* Complete chat history is restored
* **Note**: Session-scoped permissions are not automatically restored and need to be approved again
#### 2. Fork Session (Recommended! Try New Solutions Without Affecting Original Conversation)
Command:
claude --continue --fork-session
**Effect**:
* Create a **brand new session ID**
* Preserve all chat history up to this point
* Original session is completely unaffected
* Also requires re-approving permissions
**Use Case**: When you want to try "another implementation approach" without messing up the original conversation, use this!
#### 3. Use the Same Session in Multiple Terminals Simultaneously (Use with Caution)
* If you use `claude --continue` in multiple terminal windows to resume the same session:
* All messages will **intermingle together** (like two people writing in the same notebook at the same time)
* The conversation will become chaotic
* Each terminal can only see the messages it input at the time, but when resumed later, you'll see all the interleaved content
**Correct Approach**: When working in parallel, **be sure to use `--fork-session`**, giving each terminal a clean, independent new session.
* * *
## Context Window (Claude's Memory Capacity)
Claude has context capacity limits. When it's almost full:
* It will **automatically compress** old content
* You can enter `/compact` to manually compress
* Enter `/context` to see current usage
**Space-Saving Tips**:
* Write important rules in **CLAUDE.md**
* Use skills and subagents to reduce unnecessary context usage
* * *
## Security Mechanisms (Checkpoints + Permission Control)
**1. Checkpoints (Rearrange Anytime)**
Claude automatically backs up files before modifying them. Just press **Esc** twice, or say "undo the last modification," and you can return to the previous state.
**2. Permission Modes** (Press Shift + Tab to quickly switch):
* **Default**: Ask you every time before modifying files or running commands
* **Auto-accept Edit**: Modify files without asking, but still ask about commands
* **Plan Mode**: Only analyze, don't take action, first give you a complete plan
You can also whitelist trusted commands in `.claude/settings.json` (like `npm test` never needs to ask).
* * *
## 6 Practical Tips for Using Claude Code Effectively
1. **Ask It How to Use It Directly**
Enter: "How do I set up hooks?" or "/init" and it will guide you step by step.
2. **This Is a Conversation, Not a One-Time Command**
First state your general needs β it tries β you correct: "That's not right, the issue is session handling"
You don't need to rewrite the prompt every time!
3. **Interrupt It Anytime**
It went off track? Just enter a new instruction and press Enter, and it will immediately stop and adjust.
4. **The More Specific the Prompt, the Better**
Bad example: "Fix login error"
Good example: "The checkout flow for users with expired cards is broken. Check src/payments/, especially token refresh. First write a failing test, then fix it."
5. **Let It Verify Itself**
Add test cases or expected results, and it will run tests to confirm.
6. **Explore First, Then Implement**
First use **Plan Mode** (press Shift+Tab twice) to have it analyze the codebase and give you a plan β review it β then let it get to work.
YouTip