m
Mars AI Operator Module 4 ⭐ Working memory · Context engineering
60 min · hands-on
Module Structure
  1. Hook4m
  2. Mental model15m
  3. 3 techniques10m
  4. Demo Mars10m
  5. Exercise · 3 probe20m
  6. Quiz3m
  7. What's Next
Module 4 of 5 · Discernment × Working Memory

AI doesn't get dumber. It runs out of attention.

«AI nimani ko'rmoqda — va nimani unutib qo'ydi»

This technique is the most useful in the course. But it's up to you to decide. One skill: seeing what's in the agent's context, what it has already lost, and how to replenish it. Without this, Claude Code turns into a fortune teller by the third hour of work.

Duration
60 min
Hands-on
35 min
Prerequisite
M1 CLAUDE.md
Release
MVP together with M1

Hook — why powerful AI gets dumber by evening.Nega kuchli AI kechqurun ahmoqlashadi

Scene · Tuesday evening at Mars

In the morning, you opened claude in ~/mars/src/. The idea was to rewrite the notification router. The morning went smoothly: the agent read 12 files, found the right module, proposed a plan. You agreed. It did it. Tests are green.

Then you stayed in the same chat. Added a second task. A third. By lunch — refactoring migrations. By five — discussing another piece of the sales funnel.

At seven in the evening, you asked it to “run the tests we passed this morning.” And that's when it started rewriting a completely different file. It doesn't remember what you decided in the morning. Confidently heading for a wall.

This isn't a bug. It's not “the model is tired.” This is context rot — and it's the first thing an operator learns.

By the end of this module, you'll be able to

Three skills I'll check

  • To launch /compact before the agent started hallucinating in a long session — based on observed signals, not a timer.
  • Delegate Explore or Plan sub-agent (built-in or your own via /agents), when the task requires file exploration, not editing.
  • Explain what survives /compact, that there is no, and why project-root CLAUDE.md — the only reliable rule carrier for a long session.

Mental model — The context window as working memory.Context window — bu working memory

The main idea of the module: an agent has a finite attention budgetLet's break down how it's spent, why the agent "gets dumber" by evening, and three tricks that fix it.

1. Attention is a budget, not a shelf

The model has attention budget — final, like a human's. Each new token (word, symbol, piece of code) is a deduction from this budget. If you give 200,000 tokens, attention is spread thin. If you give 10,000 necessary ones, attention is concentrated. More context ≠ better.

The more text in the window, the harder it is for each word to maintain connection with all others — attention blurs. Important: this not a break at some number, and a smooth blurring.

// how much in the window → how the agent behaves ~10k tokens short session ✓ keeps everything neat ~50k tokens one hour of focused work ~ starts getting tense ~100k tokens half a day in one session ~ accuracy drops ~200k tokens I haven't closed it all day. ! loses the middle — "context rot // attention doesn't "turn off" at some number. // it smoothly blurs: the more there is in the window, the worse // the agent remembers what was at the beginning and in the middle. // you won't see an error — you'll see that the agent // confidently does the wrong thing.
→ swipe right if the table is cut off

2. Context rot — not "forgotten," but "blurred.

This has a name— context rot ("context rot"): the more tokens in the window, the worse the agent is at extracting what's needed from them. This isn't a bug of a specific model, but a general property – it's not cured by a "harsher prompt," but by techniques from the next section.

Symptom in Claude Code
What's actually going on
An agent in the 8th hour of a session breaks a convention from CLAUDE.md that it applied itself earlier in the morning
CLAUDE.md is at the beginning of the context, attention there is now weaker → the rule has blurred
Asked "continue where we left off" — it starts from the wrong place
"Stopping point" was discussed 20 turns ago, in the middle of the window → lost in the middle
Performs unnecessary Reads of the same files it has already read
Results of past Reads are deep in history, the agent doesn't trust them — re-reads
Confidently refers to a function you don't see in the code
Hallucination against the backdrop of "faded" true facts from the beginning

Operator's rule: if you notice a convention violation, unnecessary Reads, or "confident nonsense" — this is not a reason to write a harsher prompt. It's a signal to apply a technique from the next section.

3. Anatomy of effective context — what to keep in the window at all

Definition in one phrase: good context is the minimal set of necessary tokens, which gives the maximum chance of getting the desired result. Not "more context," but less noise and more density.

The sweet spot is between two extremes: overly strict rules "for every case" (fragile, breaks on new) and overly general "figure it out yourself" (the agent guesses). You need to be in the middle: specific enough to be verifiable, and flexible enough for the agent to think.

For the operator, this means: CLAUDE.md is not "everything I know about the project". It's a set of specific rules, each of which you can verify via a diff to see if it was applied. If a rule cannot be verified, it's taking up space in the context unnecessarily.

Three techniques long-horizon from the Anthropic blog.Uzun masofa uchun uchta texnika

This is the heart of the module. Three techniques for tasks that don't fit into a single window — each has its own command in Claude Code. Learn them in pairs: "technique → command."

01 · compaction

Compress the dialogue, keep the meaning

We take a long chat at the window boundary, summarize it, and start a new window with this summary. In Claude Code — the command /compact.

02 · structured note-taking

Write notes to disk

The agent or you keeps notes NOTES.md / todo-list outside of context. Loaded back when needed. This is agentic memory.

03 · sub-agents

Delegate to a clean window

A sub-agent works in its own context window, reads 15 files in its window, and returns only a compressed summary (1-2K tokens) to the main window.

Compaction — /compact in Claude Code

/compact compacts the session history: the model itself retells the dialogue, keeping what's important and discarding the noise. What's preserved: architectural decisions, unresolved bugs, important details + the last 5 files read. What's discarded: redundant command outputs, repetitive messages.

Another important fact from Claude Code docs (docs/04-claude-code-memory.md): project-root CLAUDE.md survives compaction. After /compact Claude re-reads it from disk and re-injects it into the session. This means:

Survives /compact
Does NOT survive
✓ project-root CLAUDE.md (re-injected from disk is the only confirmed survivor according to the docs)
✗ Conversation history — compacted into a summary
✓ Last 5 files read (according to the Anthropic blog)
✗ Raw tool outputs (logs, long diffs) — discarded
— user-level ~/.claude/CLAUDE.md and nested CLAUDE.md from subdirectories are automatically loaded by Claude Code at startup, but this is a different mechanism, not a /compact survivor
✗ Everything that was only said in chat (without being written to CLAUDE.md)

Practical implication. If, during a long session, you told the agent "never touch this function" — after /compact it will disappear. If you want it to survive — add it to CLAUDE.md using the command /memory (the old #-shortcut Anthropic declared deprecated in Academy lesson 9).

~/mars/srcsession t=4h 17min · 142k/200k tokens
$ /compact
⠋ Summarizing conversation…
⠙ Preserving: 3 architectural decisions, 2 open bugs, recent file edits
⠹ Discarding: 28 stale tool outputs, 41 redundant turns

✓ compacted  142k → 18k tokens (-87%)
✓ CLAUDE.md re-injected from disk  /Users/rus/mars/src/CLAUDE.md
✓ 5 recent files kept in context
  src/notify/router.py
  src/notify/handlers.py
  src/notify/__init__.py
  tests/test_notify.py
  CLAUDE.md

 we can continue. what's next?

Structured note-taking — NOTES.md and agentic memory

This is memory on disk, not in the window. A simple pattern: the agent maintains NOTES.md outside of context. A long task — recorded progress; a new session — read NOTES.md, continued. Context between sessions isn't wasted on retelling.

Three practical places where this already works in Claude Code (no configuration needed):

Where
Why
TODO-list that Claude Code maintains itself
For a long task, the agent sees its plan as a short list. Doesn't lose steps. This is write-back: writes to disk, reads back.
NOTES.md in the repo root (you maintain it manually or ask the agent)
Between sessions. Finished in the evening — wrote down "stopped at X, still need Y." In the morning, Claude reads this first thing.
YourselfCLAUDE.md using the command /memory
Right in the session, add a rule to CLAUDE.md using the command /memory — it's immediately in the window, will survive compression with /compact, and will load automatically in a new session.

Write-back vs in-context — the key difference. CLAUDE.md = in-context, always takes up space in the window. NOTES.md = write-back, stored on disk, loads on request. Use CLAUDE.md for rules, NOTES.md for progress.

Sub-agents — delegating to pure context

The main technique is noise isolation. A sub-agent spends its tens of thousands of tokens searching within its own window, and returns only a compressed summary (1-2K tokens) to the main session. The main window remains clean. This isn't "faster" — it's cheaper in terms of attention.

Built-in sub-agents in Claude Code — three of them, available immediately:

Sub-agent
When appropriate
Explore · Haiku · read-only
"Where in this repo are returns handled?" — optimized for search and navigation, doesn't edit. Fast and cheap.
Plan · inherits model · read-only
Activated in plan mode from the Claude Code UI. Explores the codebase to create a plan, doesn't make changes.
general-purpose · all tools
Multi-step tasks that require search, edits, and logic. More expensive, but more autonomous.

Which technique when:

Task
Technique
Large iterative work with a single goal
Compaction — continuous dialogue needed, just compressed
Iterations with clear milestones, pauses between sessions
Note-taking — recorded progress, continued tomorrow
Search/research in a large codebase
Sub-agent — isolate verbose output in a clean window

Demo — mentor refactors ~/mars/src/ all day.Mentor butun kun ~/mars/src/ refactor qiladi

A real-life scenario. Zukhra, a Python mentor from Yunusabad (evening, after her shift — helping Mars IT with an internal script): split a notification router into two modules. A large repo, a long day, everything we just discussed — here step-by-step.

Prediction · before reading the timeline

At 2:30 PM, Zukhra has 118k tokens in context (out of 200k). By 6:42 PM — even worse. At what step, do you think the agent will start making mistakes? And what will Zukhra do first, when she notices? Write down your two answers, then check below.

10:14 AM · Morning, clean context

~/mars/srcsession start · 0/200k tokens
$ claude

✓ CLAUDE.md loaded  ~/mars/src/CLAUDE.md (2,140 tokens)
✓ user CLAUDE.md loaded  ~/.claude/CLAUDE.md (1,820 tokens)

 split src/notify/router.py into router.py + handlers.py.
  don't break the public API. plan first, code later.

⠋ Using Plan sub-agent…    (doesn't flood main context)
✓ Plan ready:
  1. Move 4 handlers to handlers.py
  2. Keep router.py as a dispatcher
  3. __init__.py — preserve old names for backward compatibility
  4. tests/test_notify.py — fix imports, not logic

Note: Zukhra is in plan mode. Plan sub-agent doesn't flood main — main context only sees the final plan.

14:30 · Had lunch, added another task

After implementation, Zukhra didn't close the chat; instead, she added: “now fix the migrations.” Then, “check the warning in the sales funnel.” The chat grew to 118k tokens. CLAUDE.md is somewhere at the beginning, the notify-router discussion in the middle.

18:42 · Context rot caught

~/mars/srcsession t=8h 28min · 156k/200k tokens
 run the tests we passed this morning

⠋ Reading tests/test_billing.py…
⠋ Reading tests/test_billing_v2.py…
⚠ agent went into billing
  this morning it was notify. it doesn't remember.

 Escape  (stop)

 you're going the wrong way. this morning it was notify, not billing.

yes, sorry — I'll check tests/test_notify.py…
  ⚠ after 2 turns, it again suggested an f-string in child-friendly code
    which is explicitly forbidden in CLAUDE.md

What happened: CLAUDE.md at the beginning of the window, its rules “faded” under the weight of 156k tokens in the middle. The morning context about notify also faded. The agent is confidently heading for a wall.

18:45 · Treatment — /compact + Explore

~/mars/srcrecovery
 /compact
⠋ Summarizing… preserving: notify split (DONE), migrations (DONE),
                            sales warning (OPEN — root cause unclear)
✓ compacted  156k → 14k tokens (-91%)
✓ CLAUDE.md re-injected

 sales warning remained open. find out what's in the funnel via Explore,
  don't flood the main context.

⠋ Delegating to Explore subagent…
⠋ Explore reading: src/sales/*.py (~22 files)…
⠋ Explore: targeted grep for warning source…

✓ Explore returned summary  (28k tokens explored → 1.4k returned)
  warning source: src/sales/funnel.py:241
  unused import after refactor 12 days ago
  fix: delete line 11.

What Zukhra gained: main context — 14k instead of 156k. Explore scanned 22 files, but returned only the problem address and solution to main. The morning rules from CLAUDE.md are “fresh” again. We can continue.

Without techniques
With techniques
156k tokens of garbage, the agent confidently makes mistakes, you lose an hour of trust in it
14k tokens after /compact + isolated search via Explore. The main thing is — you see what returned to main
Close chat → explain “what we did” again in the morning
Write in NOTES.md “sales warning fix ready, only smoke-test in pre-prod left” → tomorrow the agent starts with this
Same module — different mentor

How it looks for Timur (Python olymp 12–14, Chilonzor)

Timur has a different scenario: a student solved an olymp.uz problem poorly — a long 200-line solution that works, but the judge gives TLE. Timur wants to show refactor using similar solutions already in his preparation repo.

Instead of letting the main agent read ~40 files past tasks and filling up the context — Timur delegates to a Explore sub-agent: "find prefix sum problems in ~/olymp-prep/ and return 2-3 reference solutions, explaining how they differ from the current one."

What main receives: 1.5K tokens of summary — file links and the core idea. What does NOT go into main: 35k tokens of raw solutions. Then, he discusses the summary with the student — the agent's attention isn't spread thin.

Same technique, different task. Timur hardly ever uses compaction — his sessions are shorter. But sub-agents? Every day.

Exercise — 3 probe Before-and-After.Uchta probe — Before va After

Three exercises for your long sessions. Do all three manually — otherwise, your next long session will be like everyone else's, a matter of luck.

Probe 1 · 5 min · cold start vs CLAUDE.md

Open claude in the same repo twice

Let's take your CLAUDE.md, which you created in Module 1.

  1. In the repo without CLAUDE.md, run claude. The prompt: "add function X, styled like this project". Record: how many clarifying questions the agent asked and if it matched the style.
  2. In the same repo with CLAUDE.md, run claude. The same prompt. Record the same two numbers.
  3. Compare. If there's no difference, your CLAUDE.md is weak; rewrite it more specifically.
What to record in the report: questions before / questions after, style match before / after, time to first diff before / after.
Probe 2 · 10 min · lost in the middle

Bury an important constraint in the middle

This probe will uncover "lost in the middle" — that drop in accuracy regarding what was discussed in the middle of a long context window.

  1. Long chat. 30+ turns: ask the agent to read different files, discuss refactoring, switch between topics.
  2. Around turn ~10–15, embed a critical constraint: "never use the library requests in this project, only httpx." Do not repeat it later.
  3. On turn 30+, ask for code that would naturally include requests (HTTP request). Record: did the agent use requests? or httpx?
  4. Record the same constraint in CLAUDE.md using the command /memory. Open a clean session claude in the same repo. Make the same request. Record the result.
Falsifiable hypothesis (one run yields yes/no): when a constraint lives in the middle of a long chat, the agent makes more mistakes with it than when the same constraint lives in a CLAUDE.md clean session. Step 3 vs step 4 — two independent results, compare them directly.
Probe 3 · 5 min · sub-agent isolation

See what's left in main

  1. A clean claude starts. Ask: “describe the structure of src/api/ (or any folder of yours with 10+ files)”. Let the agent read everything in main context. Do a /cost or /transcript — record how many tokens are in the window.
  2. A clean claude starts. Ask: “use the Explore sub-agent to describe the structure of src/api/». Let it run. Do a /cost. Record again.
  3. Compare the numbers. There should be a 3-10x difference.
What should happen: in case 1, the main context is filled with files. In case 2, main only sees a summary from Explore — you have a budget for the next hour of work.
Reflection · before quiz

Before you check yourself — pause for 60 seconds

These answers stay with you — we don't send them. This is just a way to anchor two moments where the module breaks out of “read and forget.”

Quiz — Test yourself in 3 minutes.O'zingni sina — 3 daqiqada

Three questions. If you get even one wrong, go back to the relevant section. This isn't a grade; it's your safety net before the next long session.

Question 01

You've been refactoring ~/mars/src/ for 3 hours straight in one session. At first, the agent accurately fixed files according to the CLAUDE.md convention. Now it's started violating it. What happened?

AThe agent is tired — LLMs have an equivalent of fatigue after long work
BThe context window filled up, the attention budget was exhausted, and early instructions from CLAUDE.md lost weight — context rot in action
CClaude Code switched to a smaller model on the fly to save costs
DThe instructions in CLAUDE.md expired; the file needs to be reloaded
Correct — B. Each new token consumes attention budget. CLAUDE.md is at the beginning of the window — after 3 hours, 100k+ tokens have been added on top of it, so attention to it is now weaker, and the rules have become 'blurred.' A, C, D are common misconceptions: the model doesn't 'get tired' (it has no sense of time), Claude Code doesn't change the model itself, and CLAUDE.md doesn't have an 'expiration date' — it's re-read from disk as is with each session.
Question 02

What does /compact do in Claude Code?

ADeletes all chat history and starts from scratch
BIncreases the context window size beyond the limit
CSummarizes history, preserving architectural decisions, open bugs, and the last 5 files; project-root CLAUDE.md re-injected from disk
DCompresses each message independently, one by one
Correct — C. From the Anthropic engineering blog: 'model preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant tool outputs.' From Claude Code docs: 'project-root CLAUDE.md survives compaction: Claude re-reads it from disk and re-injects it.' A is /clear, not /compact.
Question 03

When is a sub-agent appropriate (according to the Anthropic blog)?

AWhen you want to do the same work faster in parallel
BWhen a side task (searching, reading many files) would fill the main context with results you won't return to later
CWhen a different model is needed
DWhen the main agent made a mistake and needs to be replaced
Correct — B. Detailed search context remains within the sub-agent, and the main session only receives a compressed summary. A sub-agent can spend tens of thousands of tokens but returns 1,000–2,000. The main technique is noise isolation, not speed.
// select an answer for each question
All commands on one page: claude-code-cheatsheet.md /compact, /memory, /agents, /cost, sub-agents, hooks

What's next — Module 5.Keyingisi — Modul 5

Attribution and License

This module is adapted by Mars IT School (2026) based on:

— Anthropic Engineering Blog «Effective context engineering for AI agents» (Sep 2025) — central source, verbatim quotes.

«AI Capabilities and Limitations» — Anthropic Academy, 2026, section Working Memory (lessons 8, 9).

«AI Fluency: Framework & Foundations» — Dakan, Feller, Anthropic, 2025 (CC BY-NC-SA 4.0).

— Claude Code documentation (claude.com/docs) — definitions of /compact, sub-agents, what survives compaction.

«Claude Code in Action» and «Introduction to Subagents» — Anthropic Academy, 2026.

Our adaptation is CC BY-NC-SA 4.0. You can copy, remix, and use it in teaching — with attribution to Mars IT School and retaining the same license.

Final · in one phrase

How was the module overall?