20%

3.4 Plan Mode vs Direct Execution

Plan Mode คืออะไร

Plan Mode คือ mode ที่ Claude Code สร้างแผนก่อนทำงาน — แทนที่จะลงมือแก้ code ทันที Claude จะเขียน plan ออกมาให้ user review และ approve ก่อน เหมาะสำหรับ tasks ที่ซับซ้อนหรือมี risk สูง ที่เราอยากเห็นแผนก่อนทำ

เปิด Plan Mode

/plan      → เข้า plan mode

เมื่ออยู่ใน plan mode, Claude จะ:

  1. วิเคราะห์ task
  2. เขียน plan (ขั้นตอนที่จะทำ, ไฟล์ที่จะแก้, approach ที่จะใช้)
  3. รอ user approve
  4. Execute ตาม plan (หลัง approve)

Plan File Format

Plan ถูกเก็บเป็นไฟล์ markdown:

# Implementation Plan: Add user authentication

## Context
User wants JWT-based auth for the REST API.

## Steps
1. Install dependencies (jsonwebtoken, bcrypt)
2. Create `src/auth/` directory with:
   - `jwt.ts` — token generation/verification
   - `middleware.ts` — auth middleware
   - `routes.ts` — login/register endpoints
3. Add User model to `src/models/user.ts`
4. Update `src/routes/index.ts` to include auth routes
5. Add tests in `tests/auth/`

## Files to Modify
- `package.json` (new deps)
- `src/routes/index.ts` (import auth routes)

## Files to Create
- `src/auth/jwt.ts`
- `src/auth/middleware.ts`
- `src/auth/routes.ts`
- `src/models/user.ts`
- `tests/auth/auth.test.ts`

## Risks
- Need to decide token expiry time
- Password hashing rounds (recommend 12)

Plan Mode vs Direct Execution

Plan ModeDirect Execution
ดู plan ก่อนทำทำเลยทันที
เหมาะ task ซับซ้อนเหมาะ task ง่าย
User approve ก่อน executeอาจไม่ตรงที่ต้องการ
ช้ากว่า (2 rounds)เร็วกว่า (1 round)
Predictable outputอาจ surprise

เมื่อไหร่ใช้ Plan Mode

ควรใช้ Plan Mode

  • Refactoring ใหญ่ (หลายไฟล์)
  • Architecture changes
  • งานที่มี ambiguity สูง (อยากให้ Claude ถามก่อน)
  • Code ที่มีผลกระทบ broad (shared libraries)

ใช้ Direct Execution ได้

  • Bug fix ที่ชัดเจน
  • เพิ่ม test เดียว
  • แก้ typo / formatting
  • Task ที่เคย plan แล้ว (ทำตาม pattern เดิม)

Programmatic Plan Mode

ใน SDK/API สามารถ control plan mode:

EnterPlanMode → Claude creates plan
ExitPlanMode → Claude proceeds to execute (after user approval)

Key Concepts

  • Two-phase — Plan Mode แบ่งงานเป็น: Plan phase → Execution phase
  • User Agency — User สามารถ modify plan ก่อน approve ได้
  • Plan as Contract — Plan ที่ approve แล้วเป็น “contract” ที่ Claude ควรทำตาม
  • Iterative Planning — User สามารถ reject plan แล้วให้ Claude plan ใหม่ได้

Exam Tips

  • /plan = เข้า plan mode, Claude เขียนแผนแทนที่จะ execute ทันที
  • Plan mode เหมาะกับ complex tasks ที่ต้อง alignment ก่อนลงมือ
  • ข้อสอบอาจถามว่า scenario ไหนควรใช้ plan mode — ตอบ: tasks ที่ affect หลายไฟล์, มี risk, หรือ ambiguous
  • Direct execution เหมาะกับ simple, well-defined tasks
  • Plan ที่ดีต้องมี: steps, files affected, risks, decisions needed