Claude Code session, context, /compact, /clear ทำงานยังไงจริงๆ ฉบับ dev ไทยใช้ทุกวัน

นั่งเขียน code กับ Claude Code มาเป็นชั่วโมง คุยไปประมาณ 30 รอบ จู่ๆ Claude เริ่มลืมไฟล์ที่เพิ่งอ่านไปเมื่อกี้ ตอบช้าลง terminal เด้งเตือนว่า context ใกล้เต็ม คนส่วนใหญ่ตอนนี้จะกด /compact หรือ /clear ไปแบบเดาๆ ว่าตัวไหนเหมาะ บางคนใช้ทั้งสองตัวสลับๆ กันโดยไม่รู้ว่ามันต่างกันยังไงจริงๆ
จริงๆ Anthropic เขียน docs อธิบายเรื่องนี้ไว้ครบหมดแล้ว แต่กระจายอยู่ในหลายหน้า บทความนี้เลยเปิด docs ทั้ง 6 หน้าให้ครบแล้วเล่าใหม่เป็นภาษาไทยรอบเดียว เป้าหมายคือให้ dev ที่ใช้ Claude Code ทุกวันเห็น mental model ของ session กับ context ก่อน แล้วเรื่อง /compact /clear /resume จะตามมาเองโดยไม่ต้องท่องจำ
Session คืออะไร ไม่ใช่ magic เป็นไฟล์ JSONL บน disk
ก่อนจะเข้าใจ context ต้องเริ่มจาก session ก่อน เพราะ context ทุกตัวอยู่ใน session อะไรบางอย่าง mental model ที่ผิดบ่อยที่สุดคือคิดว่า session เป็นของ abstract ลอยๆ บน cloud จริงๆ session คือไฟล์ธรรมดาบนเครื่องของคุณเอง
ตาม How Claude Code works Anthropic เขียนชัดว่าทุก message ทุก tool use ทุก tool result ถูก append เข้าไฟล์ JSONL plaintext ใต้ ~/.claude/projects/ quote เต็มจาก docs
"Each message, tool use, and result is written to a plaintext JSONL file under ~/.claude/projects/, which enables rewinding, resuming, and forking sessions." (Anthropic docs, P3)path เต็มของไฟล์ session เป็น pattern แบบนี้
~/.claude/projects/<encoded-cwd>/<session-id>.jsonl
<encoded-cwd> คือชื่อ working directory ที่ encode เป็นชื่อโฟลเดอร์ ส่วน <session-id> คือ UUID ของ session นั้น เปิดไฟล์ดูได้ตรงๆ ด้วย cat หรือ editor อะไรก็ได้ จะเห็น JSON object บรรทัดละ event เรียงตามเวลา
ของพวกนี้ไม่ใช่แค่ log เฉยๆ ก่อน Claude แก้ไฟล์ใดๆ Claude Code จะ snapshot ไฟล์เก่าไว้ก่อนเพื่อให้ rewind กลับมาได้ ถ้าแก้ไม่ถูกใจก็ revert ได้ตรงจาก session
session แต่ละตัวเป็นอิสระจากกัน Anthropic ระบุชัดเจน
"Sessions are independent. Each new session starts with a fresh context window, without the conversation history from previous sessions." (Anthropic docs, P3)
แปลว่าทุกครั้งที่เริ่ม session ใหม่ context window จะว่างเปล่าทุกครั้ง ของจาก session ก่อนหน้าจะไม่ติดมาด้วย ของที่จะ persist ข้าม session ได้มี 2 ทางคือ auto memory (ไฟล์ MEMORY.md) กับ CLAUDE.md ที่เก็บไว้ใน project หรือ global
session มี retention default 30 วันแล้วลบทิ้งอัตโนมัติ ปรับได้ผ่าน setting ชื่อ cleanupPeriodDays ส่วนความปลอดภัย transcripts ทั้งหมดเก็บ plaintext ไม่ encrypt at rest อาศัย OS file permission อย่างเดียว ถ้าใช้เครื่อง share กับคนอื่นต้องระวังเพราะ Claude Code ไม่มี layer เข้ารหัสเพิ่มให้
Context window คือ working memory ของ session ปัจจุบัน
พอเข้าใจว่า session เป็นไฟล์แล้ว มาที่ context window context window คือ working memory ที่ Claude ใช้ตอบคุณในรอบปัจจุบัน ขนาด default ของทุก model ในตอนนี้คือ 200K tokens ส่วน 1M tier เฉพาะบาง model เท่านั้น (จะคุยใน section 6)
ตาม Explore the context window ตอน Claude Code เปิด session ใหม่ก่อน user พิมพ์ตัวแรกแม้แต่ตัวเดียว context ถูกใช้ไปแล้วก้อนใหญ่จากของพวกนี้
- System prompt ราว 4,200 tokens
- Auto memory จาก MEMORY.md ของโปรเจกต์
- Environment info เช่น OS shell git status
- MCP tool index (deferred mode default)
- Skill descriptions ของ skill ที่ install
~/.claude/CLAUDE.mdglobal instructions- Project CLAUDE.md ใน working directory
ตัว auto memory กับ CLAUDE.md auto load ไม่ใช่ทั้งไฟล์ docs เขียนชัดว่าโหลดแค่ 200 บรรทัดแรก หรือ 25KB whichever comes first
"The first 200 lines or 25KB of MEMORY.md, whichever comes first, load at the start of each session." (Anthropic docs, P3)
ถ้าไฟล์ MEMORY.md ของคุณยาวเกิน บรรทัดที่ 201 ขึ้นไปจะไม่โหลด ของพวก hierarchy memory ที่ลึกกว่านั้นต้องไปอาศัย mechanism อื่น
ส่วน MCP tool full schemas เป็น default deferred mode ไม่โหลดทั้งหมดตั้งแต่ต้น Claude จะโหลดเฉพาะ tool ที่ต้องใช้จริงผ่าน tool search ทำให้ MCP server ที่มี tool เยอะๆ ไม่ระเบิด context ตั้งแต่ startup
วิธีดู context จริงในเครื่องของตัวเองคือพิมพ์คำสั่ง
/context
CLI reference อธิบายว่าคำสั่งนี้แสดง grid สีของ context usage พร้อม optimization suggestions ลองเปิด terminal ของคุณตอนนี้พิมพ์ดูได้ จะเห็นเลยว่า session ปัจจุบันใช้ context ไปกี่ token แต่ละก้อนอยู่ในหมวดอะไร
Context เต็มจะเกิดอะไรขึ้นก่อนเรากด /compact เอง
หลายคนคิดว่า Claude Code จะรอให้ user กด /compact เองตอน context ใกล้เต็ม จริงๆ ไม่ใช่แบบนั้น ตอน context ใกล้ limit Claude Code จัดการให้อัตโนมัติก่อน
"Claude Code manages context automatically as you approach the limit. It clears older tool outputs first, then summarizes the conversation if needed. Your requests and key code snippets are preserved; detailed instructions from early in the conversation may be lost." (Anthropic docs, P3)
ลำดับการทำงานคือ Claude Code จะ clear tool outputs เก่าก่อนเป็นอันดับแรก เพราะ tool result มักเป็น text ขนาดใหญ่ที่กินที่เยอะ เช่น output ของ ls -la ของโฟลเดอร์ที่มีไฟล์เยอะ หรือเนื้อไฟล์ที่อ่านไปแล้วเมื่อหลายรอบก่อน ถ้า clear tool output ยังไม่พอจะค่อย summarize conversation ทั้งหมดต่อ
สิ่งที่ docs ยืนยันว่า preserve คือ user requests กับ key code snippets ส่วนที่อาจหายคือ detailed instructions ตอนต้น conversation พวก rule ที่คุณบอก Claude ตอน prompt แรกๆ อาจถูก summarize หายไป เพราะ Claude มอง user request เป็นสิ่งสำคัญสุด ส่วน instruction รายละเอียดเป็นสิ่งที่ผ่อนได้
นี่คือเหตุผลที่ Anthropic แนะนำตรงๆ ว่า rule ที่ persistent ต้องอยู่ใน CLAUDE.md ไม่ใช่ฝากไว้ใน chat
"Put persistent rules in CLAUDE.md rather than relying on conversation history." (Anthropic docs, P3)
อีก edge case ที่ต้องรู้คือกรณีที่ไฟล์เดียวหรือ tool output เดียวใหญ่จน context เต็มทันทีหลัง summarize ทุกครั้ง Claude Code จะหยุด auto-compact หลังลองหลายรอบ แล้วโชว์ error แทนการ loop ต่อ
"Claude Code stops auto-compacting after a few attempts and shows an error instead of looping." (Anthropic docs, P3)
ส่วนคำถามที่ dev ไทยถามบ่อยว่า Claude Code เตือนตอน context ใช้ไปกี่ % ตอบตรงไปตรงมาว่า docs ของ Anthropic ปัจจุบันไม่ได้ระบุเลข % ที่ trigger warning ระบุแค่ "as you approach the limit" ใครเห็นบทความที่บอกเลข % เป๊ะๆ ให้ตั้งคำถามไว้ก่อนว่ามาจาก source ไหน เพราะ primary docs ไม่ได้พูด
/compact ทำอะไรจริงๆ และอะไรรอด อะไรหาย
ทีนี้พอ context ใกล้เต็มแล้วอยากกดเองล่ะทำงานยังไง Slash commands reference อธิบาย /compact ตรงไปตรงมา
"Free up context by summarizing the conversation so far. Optionally pass focus instructions for the summary." (Anthropic docs, P1)
syntax ใช้ได้ 2 แบบ
/compact
/compact focus on the API changes
แบบแรกคือสรุปแบบทั่วไป แบบที่สองคือบอก Claude ว่าตอน summarize ให้ focus เรื่องไหนเป็นพิเศษ ตัวอย่างคือถ้ากำลัง debug API endpoint อยู่ก็สั่งให้ focus แค่เรื่อง API changes summary จะเก็บรายละเอียดส่วนที่ relevant ไว้ดีกว่าปล่อยให้สรุปแบบ generic
ตอน /compact รันเสร็จ user จะเห็น "Conversation compacted" ขึ้นใน terminal ส่วนตัว summary ทำเงียบๆ ไม่โผล่ออกมาให้เห็น ของที่ Claude เก็บไว้ก็คือ summary ที่ compact แล้ว Claude จะแทน conversation เก่ายาวๆ ด้วย summary block เดียว
แล้วของอะไรรอดจาก /compact บ้าง ตามตาราง verbatim จาก Explore the context window
| Mechanism | After compaction |
|---|---|
| System prompt and output style | Unchanged; not part of message history |
| Project-root CLAUDE.md and unscoped rules | Re-injected from disk |
| Auto memory | Re-injected from disk |
Rules with paths: frontmatter |
Lost until a matching file is read again |
| Nested CLAUDE.md in subdirectories | Lost until a file in that subdirectory is read again |
| Invoked skill bodies | Re-injected, capped at 5,000 tokens per skill and 25,000 tokens total; oldest dropped first |
| Hooks | Not applicable; hooks run as code, not context |
ตารางนี้สำคัญมาก อ่านช้าๆ เพราะมันบอกว่าทำไมบางที่ /compact ไปแล้ว Claude ยังจำ rule บางอย่างของโปรเจกต์ได้ ส่วน rule บางอย่างกลับลืม
ของที่ "Re-injected from disk" คือ Project CLAUDE.md กับ Auto memory พวกนี้ Claude Code จะอ่านจาก disk ใหม่หลัง compact เสมอ เลยไม่หายแม้ context เก่าถูก summarize ไปแล้ว
ของที่ "Lost" คือ rule ที่มี paths: frontmatter (เช่น rule ที่ apply เฉพาะไฟล์ TypeScript) กับ nested CLAUDE.md ใน subdirectory ของพวกนี้จะกลับมาเฉพาะตอน Claude อ่านไฟล์ที่ match อีกครั้งเท่านั้น ถ้า session หลัง compact ไม่ได้แตะไฟล์ใน subdir นั้น rule ก็จะไม่กลับมา
skill bodies ที่ user invoke ไปแล้วจะ re-inject กลับมาแต่มี cap ชัดเจน 5,000 tokens ต่อ skill รวมทั้งหมดไม่เกิน 25,000 tokens ถ้าเกิน Claude จะ drop skill ที่ใช้เก่าสุดก่อน
อีกจุดที่ surprise dev หลายคนคือเรื่อง skill descriptions Anthropic ระบุชัดเจน
"Unlike the rest of the startup content, this listing is not re-injected after /compact. Only skills you actually invoked get preserved." (Anthropic docs, P4)แปลว่าบรรทัด listing ของ skill ที่อยู่ใน startup index จะไม่ reload หลัง /compact เฉพาะ skill ที่คุณ invoke จริงในรอบที่ผ่านมาเท่านั้นที่ preserve เลยอาจเจอกรณีว่า skill ที่ install ไว้แต่ยังไม่เคยใช้ในรอบนี้ Claude อาจไม่รู้ว่ามีหลัง compact
ถ้าอยาก control ว่า /compact เก็บอะไรเป็นพิเศษ Anthropic แนะนำให้ใส่ section ชื่อ "Compact Instructions" ใน CLAUDE.md ของโปรเจกต์ Claude จะอ่านเป็น guideline ตอน summarize ทุกครั้ง
/clear ทำอะไร และต่างจาก /compact ตรงไหน
มาที่คำสั่งคู่แข่ง /clear ตามตำราของ Anthropic เอง
"Start a new conversation with empty context. The previous conversation stays available in/resume. To free up context while continuing the same conversation, use/compactinstead." (Anthropic docs, P1)
quote เดียวนี้ตอบทุกอย่าง /clear มี alias /reset กับ /new ทั้ง 3 ตัวทำงานเหมือนกัน หน้าที่หลักคือเริ่ม conversation ใหม่ context ว่างเปล่าเหมือนเปิด session แรก
จุดสำคัญที่หลายคนเข้าใจผิดคือ /clear ไม่ได้ทำลาย session เก่า ของเดิมยังอยู่ใน disk ใน path ~/.claude/projects/<encoded-cwd>/<session-id>.jsonl ปกติ resume กลับมาผ่าน /resume ได้เสมอ คำว่า clear หมายถึง clear context window ของรอบปัจจุบัน ไม่ใช่ delete session
mental model ที่จำง่ายคือ
/compact= ทำงานต่อใน topic เดิม สรุปเก็บใจความสำคัญ ลด token ใช้ พื้นที่ทำงานเหลือมากขึ้น/clear= topic switch สมบูรณ์ session ใหม่ context ว่าง ของเก่ายัง resume ได้ทีหลัง
เคสปกติที่ใช้ /compact คือกำลัง debug bug ตัวเดียวกันมา 2 ชั่วโมง context ใกล้เต็ม แต่ยังไม่อยากเริ่มใหม่ เพราะ Claude เพิ่งเข้าใจ business logic ของส่วนนี้ ใช้ /compact focus on the bug we are debugging เก็บ context ที่ relevant ทำงานต่อได้
เคสปกติที่ใช้ /clear คือ debug bug แรกเสร็จแล้ว จะเริ่ม implement feature ใหม่คนละส่วน ไม่ต้องการ context เก่าเลย ใช้ /clear เริ่ม session ใหม่ ของเก่าถ้าจะกลับมาก็ /resume ได้
โบนัส subagent context isolation, 1M tier, session resume
อีก 3 เรื่องที่ dev ใช้ Claude Code ทุกวันควรรู้คือ subagent isolation, 1M context tier, session resume commands
Subagent isolation ลด context bloat ของ main session
Subagent มี mechanism ที่ช่วย save context ของ main session อย่างมีนัยสำคัญ Anthropic อธิบายตรงๆ
"Subagents get their own fresh context, completely separate from your main conversation. Their work doesn't bloat your context. When done, they return a summary. This isolation is why subagents help with long sessions." (Anthropic docs, P3)
ตัวอย่าง concrete จาก Explore the context window ถ้า subagent อ่านไฟล์ขนาด 6,100 tokens แล้วกลับมา summary 420 tokens main session โตขึ้นแค่ 420 tokens เท่านั้น ส่วน 5,680 tokens ที่เหลือไม่กระทบ main session เลย เพราะ subagent ทำงานใน context ของตัวเองที่แยกออกไป
subagent มี system prompt ของตัวเอง (สั้นกว่า main session) มี Project CLAUDE.md, MCP tools, skills ของตัวเอง แต่จุดที่ต่างจาก main session คือ subagent ไม่ได้ auto memory ของ main session ดังนั้นถ้ามี state ใน MEMORY.md ที่ subagent ต้องรู้ ต้องส่งให้ผ่าน prompt arg แทน
นี่คือเหตุผลว่าทำไมงานที่ค้นข้อมูลเยอะๆ อ่านไฟล์หลายไฟล์ หรือทำงานยาวๆ ที่ output สุดท้ายเป็น summary ควร delegate ให้ subagent ทำ main session จะไม่บวมเร็ว
1M context tier เปิดยังไง
ตาม Model configuration model ที่รองรับ 1M context tier มี 3 ตัวคือ Opus 4.7, Opus 4.6, และ Sonnet 4.6
"Opus 4.7, Opus 4.6, and Sonnet 4.6 support a 1 million token context window for long sessions with large codebases." (Anthropic docs, P5)
วิธีเปิดใช้ขึ้นกับ plan ของคุณ
"On Max, Team, and Enterprise plans, Opus is automatically upgraded to 1M context with no additional configuration." (Anthropic docs, P5)
แปลว่าใครใช้ Max, Team, Enterprise plans ก็ได้ Opus 1M ฟรีไม่ต้องตั้งค่า ส่วน Pro plan ต้อง extra usage ทั้ง Opus กับ Sonnet ถ้าจะเปิด 1M เอง
วิธี select manual
/model opus[1m]
/model sonnet[1m]
claude-opus-4-7[1m]
ราคาของ token เกิน 200K ใช้ standard pricing ไม่มี premium บวกเพิ่ม ปิด 1M ทั้งหมดผ่าน env var
CLAUDE_CODE_DISABLE_1M_CONTEXT=1
Session resume / continue / fork
CLI reference รวมคำสั่งสำหรับกลับไป session เก่าครบทุกแบบ คำสั่งหลักที่ใช้บ่อย
claude -c
claude --continue
claude -r "<session>"
claude --resume
claude --session-id <UUID>
claude -c หรือ claude --continue คือ resume conversation ล่าสุดใน working directory ปัจจุบัน เปิด terminal มาแล้วอยากต่อจากเมื่อวานพิมพ์ตัวนี้ตัวเดียว
claude --resume ถ้าใส่ argument คือ resume ตาม ID หรือ name ถ้าไม่ใส่จะเปิด interactive picker ให้เลือก session ที่อยากต่อ ใช้ตอนหา session ที่ลืมว่าทำอะไรไว้
claude --session-id <UUID> ใช้ resume เฉพาะ session ที่รู้ UUID แน่นอน ปกติ script หรือ tool ภายนอกเรียกใช้
ภายใน session ก็มี slash command สำหรับ resume ได้เหมือนกัน คือ /resume [session] หรือ /continue
อีก feature ที่ powerful แต่หลายคนไม่รู้คือ --fork-session ใช้คู่กับ --resume หรือ --continue Anthropic อธิบาย
"creates a new session ID while preserving the conversation history up to that point. The original session remains unchanged." (Anthropic docs, P3)
แปลว่า fork สร้าง branch ใหม่จากจุดที่อยู่ session เดิมไม่กระทบ เหมือน git branch แต่กับ conversation เคสที่เห็นคือ resume session แล้วอยากลองทาง 2 แบบโดยไม่กระทบของเดิม fork มาทดลองในอีก branch ได้
ข้อควรระวัง 2 ข้อหลัง resume
- session-scoped permissions ไม่ restore ต้อง re-approve
"Your full conversation history is restored, but session-scoped permissions are not. You'll need to re-approve those." (Anthropic docs, P3)
- resume session เดียวกันใน 2 terminal พร้อมกัน writes interleaved กัน ไม่ corrupt แต่จะมั่ว Anthropic แนะนำให้ใช้
--fork-sessionแทนถ้าต้องการ branch ออกไปทำงานคู่ขนาน
Decision rule สั้นๆ จำง่าย ใช้ตัดสินใจหน้างาน
สรุปเป็น list สั้นๆ ที่เอาไปใช้ตัดสินใจหน้างานได้เลย
- context ใกล้เต็ม ทำงานต่อใน topic เดิม →
/compactใส่ focus instruction ถ้าอยาก control summary - งานปัจจุบันเสร็จแล้วจะเริ่ม topic ใหม่ →
/clearของเก่ายัง/resumeได้ - งานที่ค้นข้อมูลหรืออ่านไฟล์เยอะๆ → ส่งไปให้ subagent ทำ main context จะไม่บวม
- codebase ใหญ่ session ยาวๆ → พิจารณาเปิด 1M tier ตาม plan
- rule ที่อยากให้ persist ข้าม session → ใส่ใน CLAUDE.md ไม่ใช่ฝากไว้ใน chat
- ลืมว่าทำ session ไหนไป →
claude --resumeเปิด picker หรือclaude -cresume ล่าสุด
จะรู้ว่า session ปัจจุบันใช้ context ไปเท่าไหร่ไม่ต้องเดา ลองพิมพ์ /context ใน Claude Code ตอนนี้เลย จะเห็น grid สีของ usage จริงพร้อมคำแนะนำ optimization
ปิดท้าย
session คือไฟล์ JSONL บน disk ที่เปิดดูได้จริง context window คือ working memory ของ session ปัจจุบัน /compact คือ summarize ทำงานต่อ topic เดิม /clear คือ restart ของเก่ายัง resume ได้ subagent คือ context แยกที่กลับมาแค่ summary
พอเห็น mental model 4 ก้อนนี้แล้ว /compact /clear /resume จะกลายเป็นเครื่องมือที่ใช้ได้ตามจังหวะของงาน ไม่ใช่ปุ่มกดมั่วตอน Claude เริ่มลืม เปิด terminal ตอนนี้แล้วลองพิมพ์ /context ดูได้เลย ครั้งหน้าตอน context ใกล้เต็มจะรู้แล้วว่ากดอะไร
แหล่งอ้างอิง
บทความที่เกี่ยวข้อง




ความคิดเห็น
ยังไม่มีความคิดเห็น เป็นคนแรกที่แสดงความเห็น!