0Pricing
Claude Architect · บทเรียน

คำสั่งหน่วยความจำและการย่อบริบท

/memory เก็บบันทึกถาวร ส่วน /compact คืนพื้นที่บริบท (แต่มีความเสี่ยง)

คำสั่งหน่วยความจำและการย่อบริบท เป็นบทเรียน Claude Architect ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Claude Architect และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Claude Architect มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Memory and Compact Matter

Claude Code runs inside a finite context window. As a session grows, two pressures appear: you want some notes to survive every session, and you want the running conversation to stay small enough to reason well.

Two commands handle these jobs:

  • /memory — edits CLAUDE.md so instructions persist across sessions.
  • /compact — compresses the current context to free space, at the cost of detail.

This lesson is from Domain 5 (Context Management & Reliability) and Domain 3 (Claude Code Config). Knowing exactly what each command does — and what it costs — is an exam favorite.

/memory Edits CLAUDE.md

/memory opens your CLAUDE.md for editing. Whatever you write there is loaded into context at the start of every session — it is durable, not tied to one conversation.

Use it for stable facts the model should always know: project conventions, build commands, architectural rules, coding standards. Think of CLAUDE.md as your project's long-term memory; /memory is just the editor that opens it.

# In an interactive Claude Code session:
/memory

# Opens CLAUDE.md in your editor so you can add durable instructions
# e.g. "Always run `yarn build` before committing."

The CLAUDE.md Hierarchy

/memory can target different scopes, and scope decides who sees the note:

  • User-level ~/.claude/CLAUDE.md — personal, NOT shared via version control. New teammates never see it.
  • Project-level ./CLAUDE.md or .claude/CLAUDE.md — committed to VCS, shared with the whole team.
  • Directory-level — scoped to a subtree of the repo.

Exam trap: if a rule must reach every teammate, it belongs in the project-level file. Putting a shared convention only in user-level memory means onboarding teammates silently miss it.

Keeping CLAUDE.md Lean with Imports and Rules

A giant CLAUDE.md burns context on every session. Two techniques keep it lean:

  • @path imports modularize content, e.g. @./standards/coding-style.md pulls a file in by reference.
  • .claude/rules/ files with YAML frontmatter paths load only when you edit matching files — saving tokens versus a monolithic memory file.

So /memory is not just "write everything down"; it is curating which durable instructions are always-on versus loaded on demand.

# CLAUDE.md
@./standards/coding-style.md

# .claude/rules/react.md (loads only when editing matching files)
---
paths:
  - "src/**/*.tsx"
---
Prefer function components and hooks. No class components.

/compact Compresses the Live Context

Where /memory is about persistence, /compact is about capacity. It compresses the current conversation — summarizing earlier turns — so you reclaim space and can keep working in the same session.

This is a form of progressive summarization. It is genuinely useful on long sessions before you hit the context limit. But summarization is lossy, and the losses are predictable enough to be examined.

# Long session getting full? Reclaim context in-place:
/compact

# Earlier turns are summarized; recent + summary remain.

The Risk: Numbers, Dates, and Percentages Go Vague

Here is the cost the exam wants you to know. Progressive summarization makes exact numbers, percentages, and dates vague. "Refund of $487.10 on 2026-03-14" can collapse into "a refund earlier."

For an architect this is a reliability bug, not a cosmetic one. If a downstream decision depends on a precise figure or timestamp, /compact can silently erode the very facts the task hinges on.

Protect Transactional Facts in a Verbatim Block

The architect-grade fix: do not trust the summary to hold critical figures. Pull transactional facts into a separate "case facts" block kept verbatim outside the summary.

Before compacting, persist exact values — order IDs, amounts, dates, thresholds — in a place that won't be summarized (e.g. a notes file or a verbatim facts section). Then /compact can compress the chatter while the hard facts survive intact.

## CASE FACTS (verbatim — do not summarize)
- order_id: ORD-99812
- refund_amount: $487.10
- transaction_date: 2026-03-14
- policy_threshold: $500

## Discussion (safe to /compact)
...

Compact vs. a Fresh Session with a Summary

/compact keeps you in the same session. Sometimes that is the wrong move. A related pattern: starting a fresh session with a structured summary can beat carrying a long, noisy history forward.

This mirrors the sessions trade-off: --resume <name> continues a named session, but resumed tool results can be STALE if the codebase changed underneath you. When history is both bloated and outdated, a clean session seeded with a tight summary is more reliable than compacting stale context.

# Continue a named session (history may be stale):
claude --resume my-refactor

# Or branch from a shared point:
# fork_session  → new line of work off a common base

Lost-in-the-Middle and Trimming Output

Context management is bigger than one command. Models attend to the start and end of context more than the middle — the "lost-in-the-middle" effect. So where a fact sits matters, not just whether it survived /compact.

Complementary habit: trim verbose tool output to the relevant fields before it enters context. Keeping context small and well-ordered reduces how often you need to compact at all, and keeps critical facts out of the neglected middle.

How They Work Together

These two commands solve different axes, and good architects use both deliberately:

  • /memory → persistence across sessions (durable instructions in CLAUDE.md).
  • /compact → capacity within a session (lossy summarization to free space).

A typical long task: keep conventions in project CLAUDE.md via /memory; pin exact figures into a verbatim facts block; /compact when context fills; and if the carried history is stale or huge, start fresh with a structured summary instead.

A Practical Long-Session Workflow

Put it together on a multi-hour refactor:

  • At setup, /memory records the build command and the rule "review in an isolated session."
  • Mid-session you extract the exact failing test names and line numbers into a verbatim block.
  • Context fills → /compact compresses the exploration narrative, leaving the verbatim facts and recent turns sharp.
  • Hours later the branch has drifted → you abandon the bloated history and open a fresh session seeded with a tight summary.

Notice: persistence, precision-protection, and capacity are managed as separate decisions — never assumed to be free.

Quick Check: Compact Before a Refund Decision

Apply the trade-off to a real scenario.

Recap: Persistence vs. Capacity

Key takeaways:

  • /memory edits CLAUDE.md — durable instructions that persist across sessions. Project-level is shared via VCS; user-level is personal and missed by new teammates.
  • Keep memory lean with @path imports and .claude/rules/ (frontmatter paths) that load only when relevant.
  • /compact compresses live context to free space, but progressive summarization makes numbers, dates, and percentages vague.
  • Protect critical figures in a verbatim 'case facts' block outside the summary before compacting.
  • When history is stale or bloated, a fresh session with a structured summary can beat /compact or --resume. Mind lost-in-the-middle and trim verbose tool output.

คำถามที่พบบ่อย

บทเรียน “คำสั่งหน่วยความจำและการย่อบริบท” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “คำสั่งหน่วยความจำและการย่อบริบท” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Claude Architect ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Claude Architect มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “คำสั่งหน่วยความจำและการย่อบริบท”

/memory เก็บบันทึกถาวร ส่วน /compact คืนพื้นที่บริบท (แต่มีความเสี่ยง) คุณปฏิบัติ Claude Architect ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Claude Architect หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Claude Architect บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “คำสั่งหน่วยความจำและการย่อบริบท” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Claude Architect นี้ได้ไหม

ได้ บทเรียน Claude Architect ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Claude Code คืออะไร
  2. แบบโต้ตอบเทียบกับแบบไร้ส่วนติดต่อ
  3. ลูป Read / Edit / Write
  4. คำสั่งหน่วยความจำและการย่อบริบท
← กลับไปที่ Claude Architect