Best Practices — Cấu hình & Scale
Setup environment, chạy song song, automation, và tránh sai lầm phổ biến.
Cấu hình environment
Vài bước setup giúp Claude Code hiệu quả hơn đáng kể cho mọi session.
CLAUDE.md — viết ngắn, viết đúng
Chạy /init để tạo CLAUDE.md starter từ codebase, rồi tinh chỉnh dần.
# Code style
- Use ES modules (import/export) syntax, not CommonJS (require)
- Destructure imports when possible
# Workflow
- Be sure to typecheck when you're done making code changes
- Prefer running single tests, not the whole test suite
| Nên có | Không nên có |
|---|---|
| Bash commands Claude không đoán được | Thứ Claude tự suy ra từ code |
| Code style khác default | Convention chuẩn ngôn ngữ |
| Test instructions, preferred runners | API docs chi tiết (link thay vì copy) |
| Branch naming, PR conventions | Thông tin hay thay đổi |
| Architectural decisions đặc thù | Giải thích dài dòng |
| Common gotchas | ”write clean code” |
💡 Tip: Mỗi dòng trong CLAUDE.md, tự hỏi: “Bỏ dòng này Claude có mắc lỗi không?” Không → xóa. CLAUDE.md quá dài → Claude bỏ qua instruction quan trọng. Thêm emphasis (“IMPORTANT”, “YOU MUST”) cho rule hay bị bỏ qua.
Cấu hình permissions
Mặc định Claude hỏi permission cho mọi action modify system. Sau lần thứ 10 bạn chỉ click through mà không review.
Hai cách giảm interruptions:
- Permission allowlists — cho phép tool an toàn:
npm run lint,git commit - Sandboxing — OS-level isolation, Claude làm tự do trong boundary
Dùng /permissions để cấu hình, hoặc /sandbox cho isolation.
⚠️ Lưu ý:
--dangerously-skip-permissionschỉ dùng trong sandbox không có internet. Cho Claude chạy arbitrary commands có thể gây data loss hoặc data exfiltration qua prompt injection.
CLI tools
Bảo Claude dùng CLI tools: gh, aws, gcloud, sentry-cli. Đây là cách tương tác external services hiệu quả nhất về context.
Dùng 'foo-cli-tool --help' để học cách dùng foo tool,
rồi dùng nó giải quyết A, B, C.
Claude học CLI tools mới nhanh. Cài gh nếu dùng GitHub — không có gh, unauthenticated requests hay bị rate limit.
MCP Servers
claude mcp add
Kết nối Notion, Figma, database, monitoring. Claude implement features từ issue trackers, query databases, phân tích data.
Hooks — hành động chắc chắn xảy ra
Khác CLAUDE.md (advisory), hooks luôn chạy — deterministic.
Viết hook chạy eslint sau mỗi lần edit file
Viết hook chặn ghi vào thư mục migrations
Dùng /hooks để cấu hình, hoặc sửa .claude/settings.json trực tiếp.
Skills và Subagents
Skills (.claude/skills/) — domain knowledge + reusable workflows:
---
name: fix-issue
description: Fix a GitHub issue
disable-model-invocation: true
---
Analyze and fix the GitHub issue: $ARGUMENTS.
1. Use `gh issue view` to get the issue details
2. Search the codebase for relevant files
3. Implement changes, write tests, verify
4. Commit and create a PR
Chạy /fix-issue 1234 để invoke.
Subagents (.claude/agents/) — assistants chuyên biệt, chạy trong context riêng:
---
name: security-reviewer
description: Reviews code for security vulnerabilities
tools: Read, Grep, Glob, Bash
model: opus
---
You are a senior security engineer. Review code for injection
vulnerabilities, auth flaws, secrets in code, insecure data handling.
Automation và Scale
Non-interactive mode
# One-off query
claude -p "Explain what this project does"
# Structured output cho scripts
claude -p "List all API endpoints" --output-format json
# Streaming cho real-time processing
claude -p "Analyze this log file" --output-format stream-json
Dùng trong CI pipelines, pre-commit hooks, hoặc bất kỳ automated workflow.
Chạy song song nhiều sessions
Ba cách chính:
- Desktop app — quản lý visual, mỗi session có isolated worktree
- Claude Code on the web — chạy trên cloud Anthropic, isolated VMs
- Agent teams — nhiều sessions phối hợp tự động với shared tasks
Writer/Reviewer pattern:
| Session A (Viết code) | Session B (Review) |
|---|---|
Implement rate limiter cho API endpoints | |
Review rate limiter trong @src/middleware/rateLimiter.ts. Tìm edge cases, race conditions. | |
Đây là feedback từ review: [output]. Fix các issues này. |
Fan out across files
Phân tán work qua nhiều Claude invocations song song:
# 1. Generate task list
# 2. Loop through
for file in $(cat files.txt); do
claude -p "Migrate $file from React to Vue. Return OK or FAIL." \
--allowedTools "Edit,Bash(git commit *)"
done
Test 2-3 file trước, refine prompt, rồi chạy full set.
Sai lầm phổ biến — và cách tránh
| Sai lầm | Fix |
|---|---|
| Kitchen sink session — task A xong hỏi B không liên quan, quay lại A | /clear giữa các task |
| Sửa đi sửa lại — Claude sai, bạn sửa, vẫn sai, sửa tiếp | Sau 2 lần sửa → /clear + prompt mới tốt hơn |
| CLAUDE.md quá dài — Claude bỏ qua nửa instruction | Prune ruthlessly. Chuyển sang hook nếu Claude đã tự làm đúng |
| Trust rồi mới verify — code trông đúng nhưng thiếu edge cases | Luôn cho verification (tests, scripts, screenshots) |
| Explore vô tận — “investigate” không scope, Claude đọc trăm files | Scope hẹp hoặc dùng subagents |
Phát triển trực giác
Các pattern trên là điểm khởi đầu, không phải quy tắc cứng.
Đôi khi bạn nên để context tích lũy — khi đang deep trong một vấn đề phức tạp. Đôi khi skip planning — khi task là exploratory. Đôi khi prompt mơ hồ mới đúng — khi muốn xem Claude hiểu bài toán thế nào.
Chú ý điều gì hiệu quả. Khi Claude output tốt → nhớ prompt structure, context đã cho, mode đang dùng. Khi Claude struggle → hỏi tại sao: context quá noisy? Prompt quá vague? Task quá lớn cho một pass?
💡 Tip: Theo thời gian, bạn sẽ biết khi nào cần specific vs open-ended, khi nào plan vs explore, khi nào clear context vs để tích lũy. Trực giác này không guide nào dạy được — chỉ có qua thực hành.