All posts
Hướng dẫn sử dụng Claude Code
aiclaude

Hướng dẫn sử dụng Claude Code

Hieu Tran H.'s avatarHieu Tran H.
Table of Contents45 sections

Hướng Dẫn Sử Dụng Claude Code (2026)

Claude Code là công cụ AI agentic dành cho lập trình, hoạt động ngay trong terminal của bạn. Nó hiểu toàn bộ codebase, chỉnh sửa file, chạy lệnh và xử lý git workflow — tất cả qua ngôn ngữ tự nhiên.


1. Tổng Quan

Claude Code không phải chỉ là autocomplete. Đây là một AI agent có khả năng:

  • Đọc và hiểu toàn bộ codebase của bạn

  • Chỉnh sửa nhiều file cùng lúc theo kế hoạch

  • Chạy lệnh shell, test, build trong terminal

  • Xử lý git: stage, commit, tạo branch, mở Pull Request

  • Tích hợp với VS Code, JetBrains, GitHub, Slack

Điểm khác biệt với Copilot hay Cursor: Claude Code mạnh ở kiến trúc, debug phức tạp và tác vụ multi-file. Cursor tốt hơn cho code hằng ngày trong IDE.


2. Cài Đặt

Yêu Cầu Hệ Thống

Thành phần

Yêu cầu

OS

macOS 13+, Ubuntu 20.04+, Windows 10 (WSL)

RAM

4GB tối thiểu (khuyến nghị 8GB)

Internet

Bắt buộc (xử lý trên server Anthropic)

Tài khoản

Claude Pro ($20/th), Max, Teams, Enterprise, hoặc API Key

Lưu ý: Không cần GPU — toàn bộ AI chạy trên cloud của Anthropic.

Cài Đặt (Khuyến Nghị)

macOS / Linux — cách nhanh nhất:

curl -fsSL https://claude.ai/install.sh | bash

macOS — dùng Homebrew (stable channel):

brew install claude-code

Windows — dùng WinGet:

winget install Anthropic.ClaudeCode

npm (legacy, cần Node.js):

npm install -g @anthropic-ai/claude-code

WinGet và Homebrew không tự cập nhật. Chạy brew upgrade claude-code hoặc winget upgrade Anthropic.ClaudeCode định kỳ.


3. Khởi Động Nhanh

# Di chuyển vào dự án của bạn
cd your-project

# Khởi động Claude Code
claude

Lần đầu chạy sẽ yêu cầu đăng nhập tài khoản Anthropic. Sau đó bạn sẽ thấy prompt tương tác.

Thử ngay:

> Gii thích codebase này cho mình hiu tng quan
> Viết test cho module auth, chy và fix li nếu có
> Fix bug: [dán error message vào đây]
> To branch feature/login, implement form đăng nhp và to PR

Claude Code sẽ tự đọc file liên quan, lên kế hoạch, và thực thi từng bước — hiển thị rõ từng tool call nó thực hiện.


4. Giao Diện & Môi Trường

Claude Code có mặt trên nhiều môi trường:

Môi trường

Cách dùng

Terminal CLI

claude trong bất kỳ thư mục nào

VS Code Extension

Tìm "Claude Code" trong Extensions (Cmd+Shift+X)

JetBrains Plugin

Cài plugin từ Marketplace (cần CLI riêng)

Desktop App

Tải tại claude.com/download (macOS & Windows)

Trình duyệt

claude.ai/code

Desktop App (April 2026)

Desktop App không phải wrapper terminal mà là môi trường chuyên biệt với:

  • Parallel sessions: Chạy nhiều tác vụ song song với Git worktrees (mỗi session là một bản copy độc lập của repo)

  • Integrated terminal: Chạy test/build mà không rời app

  • Sidebar: Xem tất cả session đang chạy

Desktop App chưa có trên Linux — dùng CLI thay thế.


5. Các Lệnh Slash Cơ Bản

/ trong session để xem danh sách đầy đủ. Một số lệnh quan trọng:

Quản Lý Session

Lệnh

Tác dụng

/clear

Xóa toàn bộ context, bắt đầu task mới

/compact

Nén context (giữ lại thông tin quan trọng)

/compact retain error handling patterns

Nén nhưng giữ lại pattern cụ thể

/context

Xem mức độ sử dụng context hiện tại

/usage

Xem quota đã dùng trong session

Quy tắc: Dùng /compact khi context > 80%, dùng /clear khi chuyển sang task hoàn toàn khác.

Memory & Dự Án

Lệnh

Tác dụng

/memory

Mở editor chỉnh sửa CLAUDE.md

# nội dung...

Thêm nhanh vào CLAUDE.md không cần lệnh

Code Review & Bảo Mật

Lệnh

Tác dụng

/review

Review code thay đổi

/security-review

Kiểm tra SQL injection, XSS, credentials lộ...

/simplify

Refactor code cho gọn gàng hơn

/debug

Debug có hệ thống: reproduce → isolate → fix

Model & Hiệu Suất

Lệnh

Tác dụng

/model

Chuyển model (Opus 4.8, Sonnet 4.6, Haiku 4.5)

/effort high

Tăng reasoning effort cho task phức tạp

/effort auto

Reset về mặc định (medium)

Phím Tắt

Phím

Tác dụng

Shift+Tab

Cycle: Normal → Auto-accept → Plan Mode

@path/to/file

Đính kèm file vào context

!lệnh shell

Chạy lệnh shell trực tiếp (không qua AI)


6. CLAUDE.md — Bộ Nhớ Dự Án

CLAUDE.md là "constitution" của agent — file được đọc mỗi session để Claude hiểu context dự án mà không cần giải thích lại.

Tạo File

# Tạo tại root dự án
touch CLAUDE.md

Hoặc dùng /memory trong session.

Cấu Trúc Đề Xuất

# Project: my-api

## Stack
- Python 3.12, FastAPI, PostgreSQL
- Hexagonal Architecture (domain/application/infrastructure/presentation)
- Test framework: pytest

## Build & Run
npm run build
npm test
python -m pytest tests/

## Conventions
- Dùng snake_case cho Python
- Type hints bắt buộc cho tất cả function
- Commit format: feat/fix/chore(scope): description
- Không import trực tiếp từ infrastructure vào domain

## Don't
- Không sửa file trong vendor/
- Không dùng `print()` — dùng `logger`
- Không commit trực tiếp vào main

Tips Quan Trọng

  • Giữ ngắn ~100 dòng — file dài ăn hết context budget

  • Cập nhật liên tục: Khi Claude làm sai và bạn sửa, thêm rule vào CLAUDE.md ngay

  • Dùng reference: Thay vì chép toàn bộ docs vào, viết "When working with payments, first read docs/payment-architecture.md"

  • Có phần "Don't": Anti-patterns và điều cấm hoạt động tốt không kém rule tích cực


7. Plan Mode — Lập Kế Hoạch Trước Khi Viết Code

Plan Mode là chế độ read-only: Claude phân tích codebase và lập kế hoạch chi tiết, không chỉnh sửa file cho đến khi bạn cho phép.

Kích Hoạt

# Bật trong session đang chạy
Shift+Tab (nhấn 2 lần)

# Bắt đầu session ở Plan Mode
claude --permission-mode plan

# Headless (cho CI/script)
claude --permission-mode plan -p "Analyze auth flow and list risks"

Khi Nào Dùng Plan Mode

Dùng khi chi phí của một thay đổi sai cao hơn chi phí phân tích thêm:

  • Tính năng chạm nhiều file (routes, components, services, DB)

  • Refactor xuyên module

  • Thay đổi auth, billing, database, deployment

  • Khám phá codebase lạ (mới join team)

  • Migration lớn (REST → GraphQL, sessions → JWT)

  • Tìm root cause bug không rõ nguyên nhân

Workflow Đề Xuất: 80/20

[Plan Mode] Phân tích 80% → Hiểu scope, mapping file, identify risks
[Normal Mode] Execute 20% → Claude viết code, bạn review từng diff

8. Skills & Custom Commands

Slash Commands (User-invoked)

Bạn tự gọi bằng cách gõ /tên-lệnh. Lưu tại .claude/commands/<name>.md (project) hoặc ~/.claude/commands/<name>.md (global).

<!-- .claude/commands/commit.md -->
---
description: Tạo git commit với message chuẩn
allowed-tools: Bash(git *)
---
## Context
- Status hiện tại: !`git status`
- Diff: !`git diff HEAD`

## Task
Tạo commit message theo format: feat/fix/chore(scope): description

Gọi bằng: /commit

Skills (Model-invoked)

Claude tự động gọi dựa trên context. Lưu tại .claude/skills/<name>/SKILL.md.

<!-- .claude/skills/code-review/SKILL.md -->
---
name: code-review
description: Review staged changes theo team standards. Use when reviewing changes before commit.
---
Review staged changes. Flag risks. Suggest concrete fixes.

Khác biệt: Slash commands = bạn gọi thủ công. Skills = Claude tự nhận biết khi nào cần dùng.

Dùng Cho Dự Án Agentic (cmdn_agentic)

<!-- .claude/skills/ddd-review/SKILL.md -->
---
name: ddd-review
description: Review DDD layer separation. Use when creating or modifying domain/application/infrastructure files.
---
Kiểm tra:
1. Domain layer không import từ infrastructure
2. Application services chỉ dùng ports (interface)
3. Infrastructure implements đúng ports
4. Không có business logic trong presentation layer

9. MCP Servers — Tích Hợp Công Cụ Ngoài

MCP (Model Context Protocol) cho phép Claude Code kết nối với công cụ ngoài: database, browser, API, GitHub, Slack...

Thêm MCP Server

# Thêm filesystem server
claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem ~/projects

# Thêm qua SSE transport
claude mcp add --transport sse my-server https://my-server.com/sse

# Thêm với env variable
claude mcp add airtable --env AIRTABLE_API_KEY=YOUR_KEY -- npx -y airtable-mcp-server

Dùng MCP trong Session

Sau khi kết nối, các tool từ MCP server xuất hiện tự động. Với prompts của server, chúng xuất hiện là slash commands dạng /mcp__<server>__<prompt-name>.


10. Headless Mode — Tự Động Hóa & CI/CD

Chạy Claude Code không cần terminal tương tác, phù hợp cho CI/CD pipeline, GitHub Actions, scheduled jobs.

Các Flag Quan Trọng

Flag

Tác dụng

-p "prompt" hoặc --print

Chạy một lần và in output

--output-format json

Output dạng JSON (dễ parse)

--max-turns N

Giới hạn số turns agent thực hiện

--allowedTools "Read,Write,Bash"

Chỉ cho phép tool cụ thể

--permission-mode plan

Chạy ở Plan Mode (read-only)

Ví Dụ Thực Tế

# Review code trong CI
claude -p "Review staged changes for security issues" \
  --output-format json \
  --allowedTools "Read,Grep,Glob"

# Headless Plan Mode — phân tích không chỉnh sửa
claude --permission-mode plan -p "List all files that need updating for the new payment API"

# Tìm file lớn nhất trong src
claude -p "List largest files under src and explain why" --output-format json

# Kết hợp với MCP
claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem ~/projects
claude -p "List the largest files under src and explain why" --output-format json

GitHub Action (ví dụ)

- name: Claude Code Review
  run: |
    claude -p "Review this PR for security issues and code quality" \
      --output-format json \
      --allowedTools "Read,Grep,Glob"
  env:
    ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}

11. Quản Lý Context

Claude Code không có memory giữa các session. Context tồn tại trong session và được tích lũy theo turns.

Context Window (tính đến mid-2026)

Model

Context

Opus 4.8, Sonnet 4.6

1 triệu token (~555,000–750,000 từ)

Haiku 4.5

200K token

Claude không load toàn bộ codebase ngay — nó đọc file khi cần, theo dependency.

Chiến Lược Quản Lý Context

# Xem mức sử dụng
/context

# Khi > 80%: compact
/compact

# Khi chuyển task khác hoàn toàn
/clear

# Compact nhưng giữ lại thông tin cụ thể
/compact retain the error handling patterns we discussed

@ Mentions

# Đính kèm file cụ thể để Claude đọc chính xác
Refactor @src/auth/login.ts theo pattern trong @src/auth/signup.ts

# Có thể @ URL và Git reference

12. Workflow Thực Tế

Workflow Hằng Ngày

cd your-project
claude

# 1. Khám phá task
> Tôi muốn thêm tính năng X. Hãy phân tích scope.

# 2. Plan Mode trước khi code
[Shift+Tab x2] → bật Plan Mode
> List tất cả file cần thay đổi và rủi ro tiềm ẩn

# 3. Approve plan, chuyển sang Normal Mode
[Shift+Tab x1] → về Normal Mode
> OK, giờ implement theo plan

# 4. Review từng diff trước khi accept
# 5. Chạy test
> Chạy test suite và fix bất kỳ failure nào
# 6. Commit
> Tạo commit message chuẩn và commit

Workflow Onboard Codebase Mới

# Đọc tổng quan mà không sợ sửa nhầm
claude --permission-mode plan

> Giải thích kiến trúc tổng thể của project này
> Trace request từ HTTP vào đến database cho endpoint /api/users
> List các pattern phổ biến được dùng trong codebase

Workflow Debug

> /debug
# Hoặc
> Bug: [paste error]. Trace root cause, đừng sửa ngay — chỉ giải thích trước

13. Giới Hạn & Lưu Ý

Quota & Billing

  • Quota dùng chung giữa Claude.ai và Claude Code (rolling 5-hour window)

  • Dùng Claude.ai nhiều buổi sáng → quota Claude Code buổi chiều giảm

  • Pro plan ($20/th): phù hợp sprint ngắn, codebase nhỏ

  • Max plan ($100-200/th): cho heavy use hằng ngày

Bảo Mật

Claude Code có quyền truy cập trực tiếp vào filesystem và chạy shell command. Một số điều cần nhớ:

  • Không dùng Bash(*) trong allowed-tools — hãy whitelist cụ thể

  • Review từng diff thay vì auto-accept tất cả

  • Dùng PreToolUse hooks để chặn lệnh nguy hiểm

  • Không giao quyền truy cập infrastructure command trong môi trường production

Tips Cuối

  • Bắt đầu bằng task nhỏ trên project quen thuộc để hiểu tốc độ tiêu thụ quota

  • Update CLAUDE.md ngay khi Claude làm sai và bạn phải sửa

  • Dùng /model haiku cho task đơn giản để tiết kiệm quota

  • Plan trước, execute sau — 80% thời gian plan, 20% supervise execution


Tài Liệu Tham Khảo


Cập nhật: Tháng 6/2026 — Claude Code v2.1 với Opus 4.8, Sonnet 4.6, Haiku 4.5