Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

How Claude Code Works

How Claude Code Works

In short: Claude Code is like a super-smart programming partner that automatically helps you write code, fix bugs, run commands, and search files on your computer... almost anything you can do in the terminal, it can do too!

This section helps you fully understand its underlying principles (agent loop, models, tools, etc.); afterward you'll know why it's so powerful and how to use it more smoothly.


Claude's workflow

When you give Claude a task, it, like a human,loops throughthree steps:

  1. Gather context(look at your code, files, error messages)
  2. Take action(edit files, run commands, search, etc.)
  3. Verify results(check whether it succeeded and what needs adjustment)

These three steps loop continuously until the task is complete; of course, we can also interrupt it at any time, give new instructions, or say "try a different approach."

Although Claude can work autonomously, italways follows your commands!



Prompt → 获取上下文 → 执行动作 → 验证结果 →(循环)→ 完成

The entire loop is driven by two things:


Models (Claude's brain)

Claude Code uses Anthropic's Claude models to understand code and plan tasks. It can understand almost all programming languages, knows your project structure, and can break complex tasks into small steps to execute one by one.

Recommended common models:

However, using the official models domestically is not very friendly; we can switch to other domestic models.

Switching models:


Tools (the reason Claude can actually take action)

Without tools, Claude can only chat; with tools, it can actually modify your code!

Claude has five built-in categories of tools:

Real example(when fixing a failing test):

  1. Run tests to see what failed
  2. Read the error logs
  3. Search relevant source files
  4. Read 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 (sub-assistants) to make it more powerful.


What Claude can access

Claude Code can see your entire project (unlike ordinary AI, which can only see the currently open file):

Because of this, it can coordinate changes across multiple files, run tests automatically, and even commit code for you.


Execution environments and interfaces

Claude Code can run inthree environments:

Interface support: Terminal, VS Code extension, JetBrains, claude.ai/code, Slack, CI/CD, etc. The underlying working principle is exactly the same!


Working across branches (seamless use even when switching Git branches)

Each conversation in Claude Codeis bound to the current folder(rather than to a Git branch). This brings very practical features:

A great tool for parallel multi-branch development:
it is recommended to use git worktrees Create aseparate directoryfor each branch, so you can open multiple Claude Code sessions at once, each branch having its own completely independent, clean conversation history without interfering with each other.


Resume or fork a session (continue chatting / start a new line)

Claude Code supports three flexible session operations, letting you pick up where you left off or try different approaches at any time:

1. Resume a session directly (most common)

Command:

claude --continue

# 或者

claude --resume

effect:

2. Fork a session (recommended! Trying a new approach doesn't affect the original conversation)

Command:

claude --continue --fork-session

effect:

Applicable scenarios: use this when you want to try "another implementation" without messing up the original conversation!

3. Using the same session in multiple terminals simultaneously (use with caution)

- All messages get **interleaved together** (like two people writing on the same notebook at once) - the conversation becomes chaotic - each terminal can only see the messages it typed at the time, but later resume will show all the interleaved content

Correct approach: when you want to work in parallel,be sure to use --fork-session, giving each terminal a clean, independent new session.


Context window (Claude's memory capacity)

Claude has a context capacity limit. When it's nearly full:

Tips for saving space:


Security mechanisms (checkpoints + permission control)

**1. Checkpoints (undo anytime)** Claude automatically backs up files before modifying them. Just press twice Esc, or say "undo the last change," and you can return to the previous state.

2. Permission modes(press Shift + Tab to switch quickly):

You can also .claude/settings.json whitelist trusted commands in ... (such as npm test never asking again).


6 Practical Tips for Using Claude Code Effectively

  1. Ask it directly how to use itself


Type: "How do I set up hooks?" or "/init" and it will guide you step by step.

2.

It's a conversation, not a one-shot command
First give a rough requirement → it tries → you correct: "That's not right, the problem is session handling"
No need to rewrite the prompt every time!

3.

Interrupt it anytime
Is it going off track? Just type a new instruction and press Enter; it stops immediately and adjusts.

4.

The more specific the prompt, the better
Bad example: "Fix the login error"
Good example: "The checkout flow for users with expired cards is broken; check src/payments/, especially token refresh. Write a failing test first, then fix."

5.

Let it verify itself
Add test cases or expected results, and it will run tests to confirm.

6.

Explore first, then implement
First usePlan mode(Shift+Tab twice) let it analyze the codebase and give a plan → you review, then let it take action.