Constraint quan trọng nhất
Context window đầy nhanh, performance giảm khi context đầy.
Mọi best practice xoay quanh điều này.
1. Cho Claude cách tự verify
"This is the single highest-leverage thing you can do."
| Trước | Sau |
|---|---|
| "implement a function that validates email" | "write a validateEmail function. test cases: [email protected] → true, invalid → false. RUN tests after implementing" |
| "make dashboard look better" | "[paste screenshot] implement this design. take a screenshot of result, compare to original, list differences and fix" |
| "the build is failing" | "build fails with: [paste error]. fix and verify build succeeds. ADDRESS ROOT CAUSE, don't suppress" |
Đầu tư vào verification (test suite, linter, bash check) — Claude tự sửa, bạn không phải làm feedback loop.
2. Explore → Plan → Code → Commit
4 phase rõ rệt:
- Explore (plan mode) —
read /src/auth and understand how we handle sessions - Plan (plan mode) —
What files need to change? Create a plan.(Ctrl+G để edit plan) - Implement (default mode) —
implement the OAuth flow from your plan - Commit —
commit with descriptive message and open a PR
Khi nào skip plan? Task nhỏ (typo, log line, rename) — chỉ cần 1 câu mô tả diff.
3. Specific context — đừng vague
| Vague | Specific |
|---|---|
| "add tests for foo.py" | "write test for foo.py covering edge case where user logged out. avoid mocks." |
| "fix the login bug" | "users report login fails after session timeout. check src/auth/, especially token refresh. write a FAILING test that reproduces, then fix" |
| "add a calendar widget" | "look at HotDogWidget.php for pattern. follow it. user selects month, paginate forward/back to pick year. no new libraries." |
4. CLAUDE.md — pruning kỷ luật
Mỗi line, hỏi: "Removing this would cause Claude to make mistakes?" Nếu không → cắt.
| ✅ Include | ❌ Exclude |
|---|---|
| Bash command Claude không đoán được | Cái Claude tự đọc code biết |
| Code style khác default | Convention chuẩn ai cũng biết |
| Test runner ưu tiên | API doc dài (link tới doc) |
| Branch naming, PR convention | Info thay đổi liên tục |
| Architectural decision đặc thù | Mô tả từng file |
| Dev env quirks (env var cần) | "Write clean code" |
Dấu hiệu CLAUDE.md quá dài: Claude bỏ qua rule, hoặc hỏi lại câu đã trả lời sẵn trong CLAUDE.md. → prune.
5. Course-correct sớm và thường xuyên
- Esc — dừng Claude giữa chừng, context giữ nguyên
- Esc Esc hoặc
/rewind— quay về checkpoint cũ - "Undo that" — Claude tự revert
/clear— reset context giữa task khác nhau
Quy tắc 2 lần sửa: nếu correct 2 lần cùng issue →
/clear+ viết prompt tốt hơn. Long session với failed attempts kéo Claude xuống.
6. Tránh failure pattern
- Kitchen sink session — nhiều task không liên quan trong 1 conversation →
/clear - Over-specified CLAUDE.md — rule chính bị nhấn chìm trong noise → prune
- Trust-then-verify gap — code "trông đúng" mà không verify → luôn có test/script check
- Infinite exploration — "investigate X" mà không scope → dùng subagent
Nguồn chính thống đầy đủ: code.claude.com/docs/en/best-practices