Skip to content
KoishiAI
ไทย
← Back to contents
Chapter 3 / 6 · April 24, 2026

ระบบ Memory และ Context

จัดการ context เก็บความทรงจำ ปรับ Claude ให้จำสิ่งสำคัญข้ามเซสชัน

This guide is All Rights Reserved — free to read, copying/republication requires permission.

บทที่ 3 | ออกแบบ Claude Code ให้จำดี ไม่หลุดแม้เปลี่ยน Session กลุ่มเป้าหมาย: ทุกระดับ — มือใหม่สร้างระบบพื้นฐาน มือเก่าใช้เทคนิค Advanced

ทำความเข้าใจ Context Window และความจำของ Claude

ก่อนจะออกแบบระบบความจำที่ดี ต้องเข้าใจก่อนว่า Claude ทำงานอย่างไร เพราะสาเหตุที่ Claude “ลืม” ไม่ใช่ Bug แต่เป็นการทำงานตามปกติของสถาปัตยกรรม LLM

Context Window คืออะไร?

Context Window คือ “กรอบหน้าต่างมองเห็น” ของ Claude ณ ขณะนั้น Claude อ่านได้เฉพาะข้อมูลที่อยู่ใน Context เท่านั้น ไม่สามารถ “จำ” สิ่งนอก Context ได้เลย

สิ่งที่อยู่ใน Context Windowสิ่งที่ไม่อยู่ใน Context Window
บทสนทนาปัจจุบันทั้งหมดSession ก่อนหน้าทุก Session
ไฟล์ที่ส่งให้อ่านในครั้งนี้ไฟล์ที่เคยส่งใน Session อื่น
ข้อความใน CLAUDE.md (ถ้ามี)งานที่เคยทำร่วมกันเมื่อวาน
System Prompt (ถ้ามี)ชื่อโปรเจค เว้นแต่จะบอกใน Session นี้

ขนาด Context Window ของ Claude

Claude รุ่นปัจจุบันมี Context Window ขนาด 200,000 Token ฟังดูใหญ่มาก แต่ในทางปฏิบัติมีข้อจำกัดสำคัญ:

200,000 tokensเทียบเท่าโค้ดประมาณ 150,000 บรรทัด หรือหนังสือ ~500 หน้า
ราคาเพิ่มขึ้นตาม tokensInput tokens ทุกตัวคิดเงิน Context ที่ยาวขึ้นเรื่อยๆ = แพงขึ้น
ประสิทธิภาพลดลงClaude ที่ Context ยาวมากจะ “สนใจ” ข้อมูลตรงกลางน้อยลง
ล้างเมื่อเริ่ม Session ใหม่ปิด Terminal หรือรัน claude ใหม่ = Context หายหมด

💡 เข้าใจ Token ให้ชัด
ภาษาอังกฤษ: ~4 ตัวอักษร = 1 token | ภาษาไทย: ~2-3 ตัวอักษร = 1 token
โค้ด 1 บรรทัด ≈ 10-30 tokens | ไฟล์ 100 บรรทัด ≈ 500-1,500 tokens
Prompt “สรุปโปรเจค” ≈ 10 tokens | แต่ถ้าโปรเจคมี 50 ไฟล์ = อาจใช้ 50,000+ tokens

ระดับความจำของ Claude Code (4 ระดับ)

Claude Code มีระบบความจำ 4 ระดับ แต่ละระดับมีอายุและขอบเขตต่างกัน:

ระดับชื่ออยู่ที่ไหนอายุขอบเขต
1In-Context Memoryบทสนทนาปัจจุบันตลอด Sessionเฉพาะ Session นี้
2External FilesCLAUDE.md, .md filesถาวรโปรเจค / Global
3Tool ResultsMCP, Bash outputตลอด Sessionเฉพาะ Session นี้
4Model Weightsความรู้ที่ฝึกมา (Training)ถาวร (แต่เก่า)ทุกที่
บทนี้จะสอนวิธีออกแบบให้ระดับ 2 (External Files) ทำงานได้เต็มประสิทธิภาพ เพราะเป็นส่วนที่เราควบคุมได้มากที่สุด

CLAUDE.md — ระบบความจำถาวรของ Claude Code

CLAUDE.md เป็นไฟล์ที่ Claude Code อ่านอัตโนมัติทุกครั้งที่เริ่ม Session ใหม่ เปรียบเหมือน “Briefing Document” ที่บอก Claude ว่าตัวเองอยู่ที่ไหน กำลังทำอะไร และมีกฎอะไรบ้าง

ลำดับการอ่าน CLAUDE.md

Claude Code อ่าน CLAUDE.md ตามลำดับชั้นโฟลเดอร์ จากกว้างไปแคบ ข้อมูลที่อยู่ใกล้โปรเจคกว่าจะ override ข้อมูลทั่วไปได้:

~/.claude/CLAUDE.md          ← อ่านก่อน (Global: ใช้กับทุกโปรเจค)
~/Projects/CLAUDE.md         ← อ่านถัดมา (Parent folder)
~/Projects/myapp/CLAUDE.md   ← อ่านสุดท้าย (Project: เฉพาะโปรเจคนี้)
~/Projects/myapp/src/CLAUDE.md ← อ่านเมื่อทำงานในโฟลเดอร์ src/

💡 ใช้ลำดับชั้นให้เป็นประโยชน์
Global CLAUDE.md: ใส่ preference ส่วนตัว เช่น “ตอบเป็นภาษาไทยเสมอ” “ใช้ 2-space indent”
Project CLAUDE.md: ใส่ข้อมูลเฉพาะโปรเจค tech stack, conventions, ข้อห้าม
Subfolder CLAUDE.md: ใส่กฎเฉพาะ module เช่น src/api/ มีกฎการ validate ต่างจาก src/ui/

โครงสร้าง CLAUDE.md ที่สมบูรณ์ (Template)

ด้านล่างคือ Template ที่ครอบคลุมทุกส่วนสำคัญ ใช้เป็นจุดเริ่มต้นแล้วปรับตามโปรเจคได้เลย:

# CLAUDE.md — [ชื่อโปรเจค]
> อัปเดตล่าสุด: 2025-01-15 | Version: 1.4
 
## 🎯 Project Overview
[ชื่อโปรเจค] คืออะไร: ระบบ [อธิบาย 2-3 ประโยค]
Stage ปัจจุบัน: [MVP / Beta / Production]
ทีม: [จำนวนคน / solo]
 
## 🛠 Tech Stack
- Runtime: Node.js 20 LTS
- Framework: Next.js 14 (App Router)
- Language: TypeScript 5.3 (strict mode)
- Database: PostgreSQL 16 + Prisma ORM
- Auth: NextAuth.js v5
- Styling: Tailwind CSS 3.4
- Testing: Vitest + Playwright
- Deploy: Vercel (staging) + AWS ECS (production)
 
## 📁 Project Structure
src/
  app/          ← Next.js App Router pages
  components/   ← React components (ui/ และ features/)
  lib/          ← Utilities, helpers, constants
  server/       ← Server Actions, API handlers
  types/        ← TypeScript type definitions
prisma/         ← Database schema และ migrations
tests/          ← E2E tests (Playwright)
 
## ✅ Coding Conventions
### Naming
- Components: PascalCase (UserCard.tsx)
- Files/folders: kebab-case (user-profile/)
- Functions: camelCase (getUserById)
- Constants: SCREAMING_SNAKE_CASE (MAX_RETRY)
- Types/Interfaces: PascalCase + อธิบาย (UserWithProfile)
 
### Code Style
- Indent: 2 spaces (ห้ามใช้ Tab)
- Quotes: single quote สำหรับ JS, double สำหรับ JSX
- Semicolon: ไม่ใช้ (no-semi)
- Line length: สูงสุด 100 ตัวอักษร
- ทุก async function ต้องมี try/catch
 
## 🚫 DO NOT (ข้อห้ามเด็ดขาด)
- อย่าแก้ไฟล์ใน /legacy/* โดยไม่ได้รับอนุมัติ
- อย่าใช้ any ใน TypeScript ใช้ unknown แทนถ้าจำเป็น
- อย่า commit .env หรือ secret ใดๆ
- อย่าใช้ moment.js (deprecated) ใช้ dayjs แทน
- อย่าเพิ่ม dependency ใหม่โดยไม่คุยในทีมก่อน
- อย่าแก้ database schema โดยตรง ต้องทำผ่าน Prisma migration เสมอ
 
## 🔧 Common Commands
```bash
npm run dev          # Start development server (port 3000)
npm run build        # Build for production
npm run test         # Run all Vitest tests
npm run test:e2e     # Run Playwright E2E tests
npm run lint         # ESLint check
npm run typecheck    # TypeScript check
npx prisma studio    # Open Prisma GUI
npx prisma migrate dev --name [ชื่อ]  # Create migration

🔑 Key Files

  • src/lib/auth.ts ← Authentication config (อย่าแก้โดยไม่ระวัง)
  • src/lib/db.ts ← Prisma client singleton
  • src/server/actions/ ← Server Actions ทั้งหมด
  • .env.local ← Environment variables (ดู .env.example)

🎯 Current Focus

[อัปเดตทุก sprint] กำลังทำ: Feature X

  • Sprint 3 (2025-01-15 ถึง 2025-01-29)
  • เป้าหมาย: ระบบ Payment ด้วย Stripe
  • Blocked: รอ API key จาก Stripe (ETA: วันพุธ)

📝 Recent Decisions

  • 2025-01-10: เปลี่ยนจาก REST → Server Actions (ลด boilerplate)
  • 2025-01-08: ใช้ Zustand แทน Context API (performance)
  • 2025-01-05: ไม่ใช้ GraphQL (over-engineering สำหรับขนาดนี้)

⚠️ Known Issues

  • User image upload ช้ากว่าที่ควรบน mobile (TODO: optimize)
  • Search debounce ยังไม่ smooth (fix ใน Sprint 4)

### ให้ Claude สร้าง CLAUDE.md ให้อัตโนมัติ

ไม่ต้องเขียน CLAUDE.md เองทั้งหมด สั่ง Claude วิเคราะห์โปรเจคแล้วร่างให้ได้เลย:

สั่ง Claude วิเคราะห์โปรเจคและสร้าง CLAUDE.md

วิเคราะห์โปรเจคนี้ทั้งหมด แล้วสร้างไฟล์ CLAUDE.md ที่ครอบคลุม: tech stack, โครงสร้างโฟลเดอร์, conventions, คำสั่งสำคัญ, และข้อควรระวัง ดูจากไฟล์ package.json, tsconfig.json, และโครงสร้างโฟลเดอร์

หรือให้ Claude อัปเดต CLAUDE.md หลังทำงานเสร็จ

อัปเดต CLAUDE.md ให้สะท้อนสิ่งที่เราเพิ่งทำ:

  • เพิ่ม dependency ใหม่ที่เพิ่งติดตั้ง
  • อัปเดต Current Focus
  • เพิ่ม Known Issues ที่พบวันนี้

### ระบบไฟล์ .md สำหรับควบคุม Claude Code

นอกจาก CLAUDE.md แล้ว Claude Code ยังอ่านไฟล์ Markdown หลายประเภทที่มีวัตถุประสงค์ต่างกัน การรู้จักทุกประเภทช่วยให้ควบคุม Claude ได้ละเอียดและทรงพลังขึ้นมาก

|   | ประเภทที่ 1: CLAUDE.md — Memory & Rules |
|---|---|

ไฟล์หลักที่กล่าวถึงแล้ว ใช้บันทึกความจำและกฎของโปรเจค Claude อ่านทุก Session โดยอัตโนมัติ

|   | ประเภทที่ 2: Custom Slash Commands (.md ใน .claude/commands/) |
|---|---|

สร้างคำสั่ง / ที่กำหนดเองได้ โดยสร้างไฟล์ .md ในโฟลเดอร์ .claude/commands/ Claude จะรู้จักคำสั่งเหล่านี้ทันที

โครงสร้างโฟลเดอร์: .claude/ commands/ review.md ← สร้างคำสั่ง /project:review deploy-check.md ← สร้างคำสั่ง /project:deploy-check new-feature.md ← สร้างคำสั่ง /project:new-feature daily-summary.md ← สร้างคำสั่ง /project:daily-summary


### ตัวอย่าง: .claude/commands/review.md

Code Review Command

รีวิวโค้ดที่เปลี่ยนแปลงใน Git staging area ตาม checklist ด้านล่าง แล้วให้คะแนนและรายการ action items ที่ต้องแก้ก่อน merge:

Checklist

  • Security: มี SQL Injection, XSS, CSRF ไหม?
  • Error Handling: ทุก async มี try/catch ไหม?
  • TypeScript: ไม่มี any ที่ไม่จำเป็น?
  • Performance: มี N+1 query หรือ memory leak?
  • Tests: เพิ่ม test case ครอบคลุม edge cases?
  • Naming: ชื่อตัวแปร/ฟังก์ชัน สื่อความหมายชัดเจน?
  • Comments: อธิบาย “ทำไม” ไม่ใช่แค่ “ทำอะไร”?

Format ผลลัพธ์

✅ ผ่าน (X/7)

⚠️ ต้องแก้ก่อน merge

💡 ข้อแนะนำเพิ่มเติม


ใช้งานโดยพิมพ์ /project:review ใน Claude Code Claude จะรันคำสั่งนั้นทันที

### ตัวอย่าง: .claude/commands/new-feature.md

New Feature Template

สร้างโครงสร้างพื้นฐานสำหรับ Feature ใหม่: $ARGUMENTS (ใช้ $ARGUMENTS เพื่อรับชื่อ feature จากผู้ใช้)

สร้างไฟล์ต่อไปนี้:

  1. src/features/{name}/index.ts
  2. src/features/{name}/{Name}Page.tsx
  3. src/features/{name}/components/ (โฟลเดอร์เปล่า)
  4. src/features/{name}/tests/{name}.test.ts
  5. src/server/actions/{name}.ts (Server Actions)

เนื้อหาในแต่ละไฟล์:

  • ใส่ TypeScript types ที่เกี่ยวข้อง
  • ใส่ TODO comments ในจุดที่ต้อง implement
  • ใส่ error boundary สำหรับ Page component
  • เชื่อมต่อกับ auth ตาม pattern ใน src/lib/auth.ts

ใช้งาน: /project:new-feature UserProfile  หรือ  /project:new-feature PaymentHistory

|   | ประเภทที่ 3: Context Files — เอกสาร Reference |
|---|---|

ไฟล์ .md ทั่วไปในโปรเจคที่ Claude อ่านได้เมื่อ @ อ้างอิง ไม่ได้อ่านอัตโนมัติ แต่มีประโยชน์มากเมื่อต้องการให้ Claude รู้ข้อมูลเฉพาะ

docs/ architecture.md ← อธิบายสถาปัตยกรรมระบบ api-reference.md ← API endpoints ทั้งหมด database-schema.md ← ER diagram และ field descriptions decisions.md ← ADR (Architecture Decision Records) onboarding.md ← วิธี setup โปรเจคสำหรับ Developer ใหม่ runbooks/ ← วิธีแก้ปัญหาซ้ำๆ เช่น deploy, rollback

วิธีใช้ใน Claude Code

อ่าน @docs/architecture.md แล้วออกแบบ Payment module ให้สอดคล้อง ดู @docs/database-schema.md แล้วเขียน Query ที่เหมาะสม อ้างอิง @docs/decisions.md ก่อนเสนอ library ใหม่


### ตัวอย่าง: docs/decisions.md (ADR Format)

Architecture Decision Records (ADR)

ADR-001: ใช้ Server Actions แทน REST API

วันที่: 2025-01-10 สถานะ: Accepted

Context

ต้องการลด boilerplate ของการสร้าง API endpoints

Decision

ใช้ Next.js Server Actions สำหรับ mutations ทั้งหมด

Consequences

  • ✅ ลดโค้ด 40% สำหรับ CRUD operations
  • ✅ Type-safe end-to-end โดยอัตโนมัติ
  • ❌ ยากกว่าในการ debug เพราะซ่อนอยู่

ADR-002: ไม่ใช้ GraphQL


|   | ประเภทที่ 4: .clodeignore — บอกให้ Claude ข้ามไฟล์ |
|---|---|

คล้าย .gitignore แต่ใช้บอก Claude ว่าไฟล์หรือโฟลเดอร์ไหนไม่ต้องอ่าน ช่วยลด Token และป้องกันการเผลอแก้ไฟล์สำคัญ

.claude/.clodeignore

Generated files

node_modules/ .next/ dist/ build/

Sensitive files

.env* *.pem *.key

Legacy code (อย่าแตะ)

legacy/ deprecated/

Large data files

*.csv *.sql data/

Test snapshots (ไม่จำเป็นต้องอ่าน)

snapshots/ *.snap


|   | ประเภทที่ 5: Settings Files — ตั้งค่าพฤติกรรม Claude |
|---|---|

นอกจาก CLAUDE.md (Markdown) แล้ว ยังมี settings.json ที่ควบคุมพฤติกรรมระบบ:

// ~/.claude/settings.json (Global) { “model”: “claude-sonnet-4”, “editor”: “code”, “theme”: “dark”, “autoApproveEdits”: false, “notifications”: true, “mcpServers”: { … } }

// .claude/settings.json (Project-level — override Global) { “model”: “claude-opus-4”, “autoApproveEdits”: false, “permissions”: { “allow”: [ “bash:npm run *”, “bash:git *”, “bash:npx prisma *” ], “deny”: [ “bash:rm -rf *”, “bash:sudo *” ] } }


⚠️ permissions ระดับ Project สำคัญมาก <br> deny list ป้องกัน Claude รัน command อันตรายโดยไม่ตั้งใจ <br> allow list จำกัดให้ Claude รันได้เฉพาะ command ที่อนุมัติ <br> ถ้าไม่ตั้ง permissions ไว้ Claude จะถามทุกครั้งก่อนรัน command ใดๆ

### Handoff Prompt — เครื่องมือสำคัญที่มือโปรใช้ทุกวัน

Handoff Prompt คือเทคนิคการสรุปงานอย่างเป็นระบบก่อนปิด Session เพื่อให้ Session ถัดไปต่องานได้ทันทีโดยไม่เสียเวลา Re-explain ทั้งวิธีนี้มีประโยชน์ทั้งในแง่ประสิทธิภาพและประหยัดค่า Token

## ทำไม Handoff ถึงสำคัญมาก?

ลองนึกภาพสถานการณ์นี้: คุณทำงานกับ Claude มา 3 ชั่วโมง มี Context เต็มไปด้วยข้อมูล วันรุ่งขึ้นเปิด Session ใหม่ Claude ไม่จำอะไรเลย ถ้าไม่มี Handoff คุณต้องอธิบายทุกอย่างใหม่หมด ซึ่งอาจใช้เวลา 10-15 นาที และอาจลืมบางอย่างที่สำคัญ

| ไม่มี Handoff | มี Handoff |
|---|---|
| อธิบาย context ใหม่ทุกครั้ง | Claude เข้าใจ context ทันที |
| เสี่ยงลืม decision สำคัญ | มีบันทึก decision ทุกอย่าง |
| Claude ต้องอ่านโค้ดใหม่ = Token สูง | Claude รู้ state ปัจจุบัน = Token ต่ำ |
| ต่องานได้ช้า | ต่องานได้ทันทีใน 1-2 นาที |
| Risk ทำผิดเพราะไม่รู้ context | Risk ต่ำเพราะมีประวัติชัดเจน |

### Handoff Prompt แบบ Basic (5 นาที)

เหมาะสำหรับมือใหม่ ใช้ Prompt นี้ก่อนปิด Session ทุกครั้ง:

ก่อนจบ Session นี้ สร้าง Handoff Summary ให้ผม ใส่ใน CLAUDE.md ในส่วน ”## Current Session Handoff” โดยครอบคลุม:

  1. งานที่ทำเสร็จวันนี้ (bullet points)
  2. งานที่ค้างอยู่และขั้นตอนถัดไปที่ชัดเจน
  3. ไฟล์ที่แก้ไขไป (list)
  4. Decision ที่ตัดสินใจและเหตุผล
  5. ปัญหาที่พบและวิธีแก้ที่ลองไปแล้ว
  6. คำสั่งที่ต้องรันต่อในขั้นตอนถัดไป

### Handoff Prompt แบบ Advanced (สำหรับมืออาชีพ)

ใช้เมื่อต้องการ Handoff ที่ละเอียดและพร้อมใช้งานสูงสุด:

สร้าง Handoff Document แบบสมบูรณ์ บันทึกลงไฟล์ .claude/handoffs/handoff-[วันที่]-[เวลา].md ตามโครงสร้างนี้:

📊 Session Summary

  • วันที่/เวลา: [ใส่อัตโนมัติ]
  • ระยะเวลา: [ประมาณ]
  • Feature/Task หลัก: [ชื่อ]

✅ Completed

  • [รายการงานที่เสร็จ พร้อม bullet ย่อยถ้ามี]

🚧 In Progress (ยังไม่เสร็จ)

  • [งานค้าง + % ที่เสร็จ + ขั้นตอนถัดไปที่ชัดเจน]

📝 Files Modified

[รัน git diff —name-only HEAD แล้วใส่ผลลัพธ์]

🔑 Key Decisions Made

  • [decision]: [เหตุผล] | [alternatives ที่ reject]

🐛 Issues & Blockers

  • [ปัญหา]: [สิ่งที่ลอง] → [ยังไม่แก้ / แก้ด้วยวิธีนี้]

💻 Next Session: Exact Commands to Run

# รันสิ่งเหล่านี้ทันทีที่เริ่ม session ใหม่
[คำสั่งจริงๆ]

🤖 Context for Next Claude Session

[paragraph 3-5 ประโยค อธิบาย context ที่ Claude ต้องรู้ทันที]

หลังจากบันทึกแล้ว อัปเดต CLAUDE.md ส่วน “Current Focus” ด้วย TL;DR 2-3 ประโยค


### Template เริ่ม Session ใหม่

หลัง Handoff ดี การเริ่ม Session ใหม่ก็ทำได้เร็วและตรงประเด็น:

Prompt เปิด Session ใหม่ (ใช้ template นี้ทุกครั้ง)

อ่าน @CLAUDE.md และ @.claude/handoffs/handoff-[วันที่ล่าสุด].md แล้วสรุปให้ผมว่าเราค้างอยู่ตรงไหน และขั้นตอนถัดไปคืออะไร จากนั้นรันคำสั่งที่ระบุไว้ใน “Next Session: Exact Commands” เลย

ถ้าไม่มี Handoff file

อ่าน @CLAUDE.md แล้วบอกผมว่า:

  1. โปรเจคนี้คืออะไร ทำอะไรอยู่
  2. มี TODO หรือ Known Issues อะไรบ้าง
  3. เราควรเริ่มจากตรงไหน

### ระบบ Handoff สำหรับทีม

ถ้าทำงานเป็นทีม Handoff Document สำคัญยิ่งกว่า เพราะสมาชิกคนอื่นต้องต่องานได้ด้วย:

.claude/ handoffs/ README.md ← อธิบาย format ของ Handoff 2025-01-15-john.md ← Handoff ของ John วันที่ 15 2025-01-15-jane.md ← Handoff ของ Jane วันที่ 15 2025-01-16-john.md ← วันที่ 16 latest.md ← Symlink หรือ copy ล่าสุด

.claude/handoffs/README.md

Handoff Protocol

วิธีสร้าง Handoff

  1. ก่อนจบการทำงานทุกครั้ง รัน: /project:handoff
  2. ไฟล์จะถูกสร้างที่ .claude/handoffs/{วันที่}-{ชื่อ}.md
  3. Commit ไฟล์นี้ก่อน Push เสมอ

วิธีรับ Handoff

  1. อ่าน .claude/handoffs/latest.md
  2. ถาม Claude: ”> อ่าน @.claude/handoffs/latest.md แล้วบอกว่าต้องทำอะไรต่อ”

### สร้าง Slash Command สำหรับ Handoff อัตโนมัติ

.claude/commands/handoff.md

Auto Handoff Command

สร้าง Handoff Document สมบูรณ์และบันทึกลงไฟล์ทันที:

  1. รัน git diff --name-only HEAD เพื่อดูไฟล์ที่แก้
  2. รัน git log --oneline -10 เพื่อดู commits ล่าสุด
  3. รัน date "+%Y-%m-%d-%H%M" เพื่อได้ timestamp
  4. สร้างไฟล์ .claude/handoffs/{timestamp}-handoff.md ตาม Advanced Handoff format
  5. อัปเดต .claude/handoffs/latest.md
  6. อัปเดต CLAUDE.md ส่วน Current Focus
  7. แสดง summary ให้ผู้ใช้เห็น

ใช้งาน: พิมพ์ /project:handoff แล้วกด Enter ทุกอย่างจะทำเองอัตโนมัติ

### การจัดการ Context อย่างมีประสิทธิภาพ

Context Window เต็มคือปัญหาที่ทุกคนเจอ การรู้วิธีจัดการ Context ดีๆ ช่วยประหยัดเงินและทำให้ Claude ตอบได้แม่นยำขึ้น

### สัญญาณที่บอกว่า Context กำลังจะเต็ม

Claude เริ่มลืมสิ่งที่คุยไปตอนต้น Session

Claude ตอบช้าลงอย่างเห็นได้ชัด

Claude เริ่มวน loop หรือเสนอวิธีที่เคย reject ไปแล้ว

มีข้อความเตือน "Context is getting long"

คำตอบสั้นลงผิดปกติหรือตัดทอนข้อมูล

### /compact — บีบอัด Context แทนการเริ่มใหม่

/compact คือคำสั่งให้ Claude สรุปบทสนทนาทั้งหมดเป็น Summary กระชับ แล้วลบประวัติเก่าออก เหลือแค่ Summary ใน Context ทำให้ประหยัด Token และ Claude ยัง "จำ" ใจความสำคัญได้

วิธีใช้ /compact

/compact

/compact พร้อมบอกสิ่งที่ต้องการให้จำเป็นพิเศษ

/compact โปรดเก็บ: decision เรื่อง auth architecture, bug ที่พบใน payment flow, และ list of TODOs

ดูขนาด Context ปัจจุบัน

/context


| สถานการณ์ | ควรทำอะไร |
|---|---|
| Context ยังไม่เต็ม แต่บทสนทนายาวมาก | /compact เพื่อประหยัด Token |
| ต้องการเริ่ม Feature ใหม่ในโปรเจคเดิม | /compact แล้วทำต่อ หรือเริ่ม Session ใหม่ |
| Claude วน loop ตอบผิดๆ ซ้ำๆ | เริ่ม Session ใหม่ดีกว่า |
| กำลังทำงานที่ต้องการ Context ยาว | ส่งไฟล์ผ่าน @ อ้างอิง แทนการ paste ใน chat |
| ทำงานกับ Codebase ใหญ่มาก | ใช้ RAG (บทที่ 6) แทนการส่งทุกไฟล์ |

### เทคนิคลด Token โดยไม่สูญเสียประสิทธิภาพ

### 1. อ้างอิงไฟล์แทนการ Paste

❌ แย่: paste โค้ดยาว 200 บรรทัดใน chat

นี่คือโค้ด: [paste 200 บรรทัด] แก้ bug ใน function validateUser

✅ ดี: ใช้ @ อ้างอิง

แก้ bug ใน function validateUser ใน @src/lib/auth.ts


### 2. เฉพาะเจาะจงในสิ่งที่ต้องการ

❌ แย่: กว้างเกินไป

ดูโปรเจคแล้วบอกว่ามีปัญหาอะไรบ้าง

✅ ดี: เฉพาะเจาะจง

รีวิวเฉพาะ @src/server/actions/payment.ts ตรวจสอบ error handling และ race conditions


### 3. ใช้ --max-tokens สำหรับ One-shot

จำกัดความยาวคำตอบเพื่อลด output tokens

claude -p “สรุปไฟล์นี้ใน 5 bullet” —file README.md

ตั้งค่า max tokens ใน settings

claude config set maxTokens 4096


### การใช้ /memory และ User Memory

นอกจาก CLAUDE.md แล้ว Claude Code มีระบบ Memory ที่จัดการผ่านคำสั่ง /memory ซึ่งให้ Claude บันทึกและดึงข้อมูลส่วนตัวของผู้ใช้ได้

### คำสั่ง /memory ที่ใช้บ่อย

ดู Memory ทั้งหมดที่บันทึกไว้

/memory

เพิ่ม Memory ใหม่

/memory add ผมชอบใช้ functional programming style /memory add ตอบเป็นภาษาไทยเสมอ ยกเว้น code และ technical terms /memory add ใช้ 4-space indent สำหรับ Python projects

แก้ไข Memory

/memory edit [id]

ลบ Memory

/memory delete [id]

ค้นหา Memory

/memory search “python”


### User Memory vs Project Memory

| หัวข้อ | User Memory (/memory) | Project Memory (CLAUDE.md) |
|---|---|---|
| อยู่ที่ไหน | ~/.claude/CLAUDE.md | โปรเจค/CLAUDE.md |
| ใช้กับ | ทุกโปรเจค ทุก Session | เฉพาะโปรเจคนี้ |
| เหมาะสำหรับ | Preference ส่วนตัว | Rules ของโปรเจค |
| ตัวอย่าง | "ตอบภาษาไทย", "ชอบ functional style" | "Tech stack", "Conventions", "DO NOT" |
| ใครแก้ไข | Claude ด้วย /memory | Claude หรือ Developer |

### ตัวอย่าง Global CLAUDE.md (User Memory) ที่ดี

~/.claude/CLAUDE.md

My Personal Claude Preferences

Communication

  • ตอบเป็นภาษาไทยเสมอ ยกเว้น code, technical terms, และ proper nouns
  • อธิบายสั้นๆ ก่อน แล้วค่อยขยาย ไม่ต้อง verbose เกินไป
  • ถ้าไม่แน่ใจ ให้บอกตรงๆ อย่า make up ข้อมูล

Coding Style

  • ชอบ Functional programming (pure functions, immutability)
  • TypeScript เสมอถ้าเป็น JavaScript project
  • Test-driven: เขียน test ก่อนถ้าเป็นไปได้
  • Prefer explicit over implicit

Review Style

  • รีวิวแบบ “Senior Developer mentoring Junior” — อธิบายเหตุผล
  • ชี้ปัญหา Critical ก่อน แล้วค่อย nice-to-have
  • ให้ code example ประกอบเสมอ

Workflow

  • ถามให้ชัดก่อนทำ ถ้า requirement ไม่ชัดเจน
  • ทำทีละขั้น แล้วรอให้ approve ก่อนทำต่อ สำหรับงานใหญ่
  • สรุปสิ่งที่ทำเมื่อเสร็จแต่ละ task

### เทคนิค Advanced สำหรับระบบความจำ

### เทคนิคที่ 1: Knowledge Base ใน CLAUDE.md

ใช้ CLAUDE.md เป็น mini knowledge base สำหรับข้อมูลที่ Claude ต้องรู้บ่อยๆ ลด Token ที่ต้องอธิบายซ้ำ:

Knowledge Base

Business Rules

  • User ระดับ Free: จำกัด 100 requests/วัน
  • User ระดับ Pro: ไม่จำกัด + priority queue
  • Admin สามารถ override limit ได้

Error Code Reference

  • E001: Unauthorized (redirect to login)
  • E002: Rate limit exceeded (แสดง upgrade prompt)
  • E003: Invalid input (แสดง field error)
  • E500: Server error (แสดง friendly error + log)

Third-party Service Info

  • Stripe: ใช้ Webhook ไม่ใช่ polling
  • SendGrid: template ID prefix = “d-”
  • AWS S3 bucket: myapp-{env}-uploads

### เทคนิคที่ 2: Living Documentation

ให้ Claude อัปเดต Documentation โดยอัตโนมัติหลังทุกครั้งที่ทำ Feature สำคัญ:

หลังเพิ่ม Feature ใหม่

สร้าง Feature เสร็จแล้ว ทำสิ่งต่อไปนี้:

  1. อัปเดต @docs/api-reference.md ด้วย endpoints ใหม่
  2. เพิ่ม entry ใน @docs/decisions.md สำหรับ technical decision
  3. อัปเดต CLAUDE.md ส่วน “Key Files” ถ้ามีไฟล์สำคัญเพิ่ม
  4. เพิ่ม known limitation ใน CLAUDE.md ถ้ามี

สร้าง Slash Command ให้ทำอัตโนมัติ

.claude/commands/update-docs.md

Update Documentation

หลังเสร็จ Feature ให้อัปเดต docs ทั้งหมดที่เกี่ยวข้อง

ตรวจจาก git diff ว่ามีการเปลี่ยนแปลงใน src/ ส่วนไหน

แล้วอัปเดต docs ที่สอดคล้อง


### เทคนิคที่ 3: Session State Pattern

สำหรับงานที่ใช้เวลาหลาย Session ให้ใช้ State file เก็บ progress:

.claude/state.md (สร้างโดย Claude อัปเดตทุก session)

Current State

Active Task

Implementing: Stripe Payment Integration PR: #47 (draft)

Checklist

  • สร้าง Stripe client singleton
  • Implement createPaymentIntent
  • Webhook handler skeleton
  • Handle payment_intent.succeeded event
  • Handle payment_intent.failed event
  • Write integration tests
  • Update API docs

Blocking Issues

  • ยังไม่ได้ test กับ Stripe test clock

Environment

  • Stripe test mode: ✅ configured
  • Webhook local testing: ✅ stripe-cli running

Last Updated

2025-01-15 16:30 by John

Prompt อัปเดต State อัตโนมัติ

อัปเดต @.claude/state.md:

  • tick checkbox ที่เสร็จแล้ว
  • เพิ่ม blocking issue ใหม่ถ้ามี
  • อัปเดต Last Updated

เริ่ม Session ใหม่

อ่าน @.claude/state.md แล้วบอกว่าต้องทำอะไรต่อ และเริ่มทำ checkbox ถัดไปได้เลย


### เทคนิคที่ 4: Multi-Agent Memory Sharing

ถ้าใช้หลาย Claude Agent ทำงานพร้อมกัน ใช้ Shared Memory ผ่านไฟล์:

โครงสร้างสำหรับ Multi-Agent

.claude/ shared/ context.md ← Context ร่วมที่ทุก Agent อ่าน decisions.md ← Decision log ร่วมกัน interfaces.md ← API contracts ระหว่าง modules agents/ frontend-agent.md ← Memory เฉพาะ Frontend Agent backend-agent.md ← Memory เฉพาะ Backend Agent test-agent.md ← Memory เฉพาะ Testing Agent

Frontend Agent เริ่มงาน

อ่าน @.claude/shared/context.md และ @.claude/agents/frontend-agent.md แล้วทำงาน UI ตาม interface ที่กำหนดใน @.claude/shared/interfaces.md เมื่อเสร็จให้ update @.claude/agents/frontend-agent.md


### เทคนิคที่ 5: Memory Versioning

สำหรับโปรเจคที่ทำนานหลายเดือน ให้ Version Control CLAUDE.md เช่นกัน:

CLAUDE.md ควร Commit เข้า Git เสมอ

git add CLAUDE.md git commit -m “docs(claude): update current focus to Sprint 4”

ดู history ของ CLAUDE.md

git log —oneline — CLAUDE.md

ดูว่า CLAUDE.md เปลี่ยนอะไรใน Sprint นี้

git diff HEAD~10 — CLAUDE.md

ให้ Claude วิเคราะห์ความเปลี่ยนแปลง

git diff HEAD~10 — CLAUDE.md | claude -p “สรุปว่าโปรเจคเปลี่ยนแปลงไปอย่างไรจาก diff ของ CLAUDE.md”


### การแก้ปัญหาระบบความจำที่พบบ่อย

### ปัญหา: Claude ไม่อ่าน CLAUDE.md

ตรวจสอบว่าไฟล์อยู่ถูกที่ไหม

ls -la CLAUDE.md ls -la ~/.claude/CLAUDE.md

ตรวจสอบว่า Claude อ่านไฟล์ไหนบ้าง

บอกหน่อยว่าคุณกำลังอ่านไฟล์ CLAUDE.md จากที่ไหนบ้าง และเห็นข้อมูลอะไรจากไฟล์นั้น

Force อ่าน CLAUDE.md

อ่าน @CLAUDE.md อีกครั้งแล้วสรุปว่าเห็นอะไร


### ปัญหา: CLAUDE.md ยาวเกินไป Context เต็ม

ถ้า CLAUDE.md ยาวเกิน 500 บรรทัด ควรแยกออกเป็นไฟล์ย่อย:

แยก CLAUDE.md ออกเป็นส่วนๆ

CLAUDE.md ← เหลือแค่ Overview + Links .claude/ tech-stack.md ← Tech stack ละเอียด conventions.md ← Coding conventions knowledge-base.md ← Business rules, error codes runbooks.md ← How-to guides

CLAUDE.md หลักจะอ้างอิงไฟล์ย่อย

Tech Stack

ดูรายละเอียดที่ @.claude/tech-stack.md

Conventions

ดูรายละเอียดที่ @.claude/conventions.md


### ปัญหา: Claude ทำสิ่งที่ CLAUDE.md ห้ามไว้

เพิ่ม emphasis ใน CLAUDE.md

🚫 CRITICAL: DO NOT (ห้ามเด็ดขาด)

MUST NEVER: แก้ไฟล์ใน /legacy/* MUST NEVER: ใช้ any ใน TypeScript MUST NEVER: Commit .env files

เพิ่ม rule ใน .claude/settings.json

{ “permissions”: { “deny”: [“bash:rm *”, “bash:sudo *”] } }

ถ้า Claude ยังไม่ทำตาม ให้เตือนใน Prompt

สำคัญ: ก่อนทำอะไร อ่าน CLAUDE.md ส่วน DO NOT ก่อนเสมอ


### ปัญหา: Handoff ไม่ครบถ้วน Claude ต่องานไม่ได้

แก้ด้วยการทำ Handoff ที่ละเอียดขึ้น และตรวจสอบก่อนจบ Session:

Checklist ก่อนจบ Session (รัน /project:handoff)

ก่อนจบ Session ตรวจสอบว่า Handoff ครบไหม: □ งานที่เสร็จครบถ้วน? □ งานค้างมีขั้นตอนถัดไปชัดเจน? □ ไฟล์ที่แก้ไปมี list ครบ? □ มี “Exact Commands” สำหรับ session ถัดไป? □ Context สำหรับ Claude ใหม่เขียนครบ? ถ้าข้อไหนขาด ให้เติมก่อน


## คำศัพท์ประจำบท

| Context Window | กรอบหน่วยความจำชั่วคราวของ Claude มีขนาด 200K tokens |
|---|---|
| Token | หน่วยข้อความที่ AI ประมวลผล ภาษาไทย ~2-3 ตัวอักษร = 1 token |
| CLAUDE.md | ไฟล์ Markdown ที่ Claude อ่านอัตโนมัติทุก Session เป็น "สมุดบันทึก" |
| Handoff Prompt | เทคนิคสรุปงานก่อนปิด Session ให้ Session ถัดไปต่อได้ทันที |
| /compact | คำสั่งบีบอัด Context โดยสรุปบทสนทนาเก่า ลด Token ที่ใช้ |
| User Memory | ความจำส่วนตัวที่ใช้ทุกโปรเจค จัดการด้วย /memory |
| Project Memory | ความจำเฉพาะโปรเจค เก็บใน CLAUDE.md ของโปรเจค |
| Slash Command | คำสั่งที่สร้างเองได้โดยใส่ไฟล์ .md ใน .claude/commands/ |
| ADR | Architecture Decision Record บันทึกว่าทำไมถึงตัดสินใจแบบนั้น |
| .clodeignore | ไฟล์บอก Claude ว่าโฟลเดอร์/ไฟล์ไหนไม่ต้องอ่าน |
| Permissions | การตั้งค่าควบคุมว่า Claude รัน command อะไรได้บ้าง |
| Living Documentation | เอกสารที่อัปเดตอัตโนมัติตาม code เปลี่ยนแปลง ไม่ล้าสมัย |
| Session State | ไฟล์ที่เก็บ progress ของงาน ช่วยให้ต่องานได้แม้เปลี่ยน Session |
| Multi-Agent | ระบบที่ใช้ Claude หลาย Instance ทำงานพร้อมกันแบบขนาน |