← Alle Playbooks
Playbook· build

Managing the context window in long Claude Code sessions

When to /compact, when to /clear, when to /resume. Ten steps against drift in hours-long coding sessions.

Hour three into the session, Claude starts forgetting things you explained in hour one. Answers get vaguer. The last code change ignores a convention you explicitly set at the beginning. Classic context drift.

You can't "prompt this away". You have to actively manage the context. The tools for it are all there, but most people don't use them or use them wrong. Here are the ten steps I go through in every longer coding session, in the order in which they kick in.

1. Recognize when the context tips over

Three signals that reliably arrive before things really go wrong. First: Claude repeats itself, suggests things you already discarded two hours ago. Second: code changes get more frantic, more files at once, less focused. Third: you have to name conventions you set at the beginning again explicitly, because otherwise wrong patterns come out.

When one of these three signals arrives, it is time for step 2. Not later. The further the drift, the more expensive the reset.

2. /usage first, then decide

Before you reset anything: type /usage. You see the current token level of the session. /cost is an alias for it and does the same.

Under 50 percent utilization: drift has a different cause, maybe your last prompt is unclear, maybe the task is simply too big. A reset gets you nothing.

At 50 to 75 percent: still room, but /compact makes sense now if you carry on for a while longer.

Over 75 percent: here you have to act, otherwise the session tips over in the next 20 minutes.

3. Use /compact correctly

/compact writes a summary of the session and replaces the long history with it. Token-wise you often save 60 to 80 percent. What stays: the summary plus your CLAUDE.md plus the last few turns.

Danger: what does not land in the summary is gone. If you are right in the middle of a delicate refactor and Claude "knew" a convention that was stored nowhere explicitly, that can be gone after /compact. Write important things into CLAUDE.md beforehand or have Claude write the convention down explicitly.

Practice tip: give the /compact a hint about what absolutely has to go in. "Compact aber behalte alle Entscheidungen zur Auth-Architektur" works.

4. /clear when the job is done

When a task is really finished, that is committed, tested, deployed: /clear. Starts a new conversation with empty context. The old one stays available in /resume in case you have to go back.

Classic mistake: people leave the session open all day, jump between tasks, and wonder why after 6 hours everything goes off the rails. Every new task deserves /clear. That is free and makes hours 4 to 8 more productive than hours 1 to 3.

5. /resume instead of a panicked restart

You cleared, then you notice that something important was still in the old context. /resume shows you the last sessions, you jump back into it. No data loss.

Important: /resume is not for "I need the whole context again". It is for "I need one thing from the old context". Grab the info, write it as a note into CLAUDE.md or into your memory, then back to the fresh session.

6. CLAUDE.md as an anchor

This is the only place that survives resets. CLAUDE.md sits in the project root and is loaded automatically into every session. Everything that is project-constant belongs in there, not in the conversation context.

Concretely in: coding conventions, stack versions, routes map, "we use X not Y", glossary for domain terms, "PostgreSQL runs on port 5433 not 5432". What you otherwise have to explain again after every /compact belongs in the file.

Mind the limit: Anthropic explicitly warns against CLAUDE.md files that are too big. If yours goes over 500 lines, that is a code smell. Split into a CLAUDE.md for the core plus separate doc files that get loaded on demand via a Read instruction.

7. Split long tasks with /fork

Some tasks are so big that they fill a whole session by themselves. Refactor through 40 files, migration to a new API version, feature implementation with five dependencies. A single sub-agent or a /fork branch makes sense there.

/fork starts a branch of the current session. You keep working there separately, the original context stays clean. You come back with /resume. This way you avoid the research part of a task cluttering up your implementation session.

CLAUDE_CODE_FORK_SUBAGENT as an env variable flips it so /fork spawns a sub-agent instead of a session branch. That is the variant for "I want parallel work, not sequential".

8. Memory MCP for cross-session

CLAUDE.md is project-scoped, but some things are cross-project. "Matthias hasst em-dashes", "Wir deployen über Docker, nie direkt", "Postgres läuft Port 5433 weil 5432 schon besetzt war". Stuff like that into a memory MCP server.

StudioMeyer Memory MCP or a comparable server (Mem0, Letta, your own) stores facts persistently and gives them back on request. The lessons in Level 4 (memory-einrichten, memory-hygiene) show the setup. Effect: your facts survive not only /clear, but also completely new machines.

9. Plan mode reduces context load

Before you go into a big task, have Claude plan it in plan mode. Plan mode generates only the plan, no code. The plan lands as compact text in the context, not as 40 file reads and 200 diff lines.

With the plan in the context you can then implement piece by piece in a targeted way, possibly /compact after each piece. That is 5x more efficient than "implement the feature completely". More on this in the playbook plan-mode-richtig-nutzen.

10. Daily reset routine

End of the working day: close the session cleanly, don't leave it open until tomorrow. Concretely:

First, commit all open tasks or explicitly mark them as draft. Second, important decisions made today into CLAUDE.md or memory. Third, /clear plus a line "Tomorrow: pick up X". Fourth, start the next morning with a fresh /clear and possibly /resume from yesterday if needed.

This sounds like overhead, but 5 minutes a day invested gives you back 30 to 60 minutes of drift pain per week. Plus you recognize patterns: which tasks really need long sessions, which are 2-hour pieces. That helps you plan tasks better in the long run.

What comes next

If the topic is generally burning your fingers, two next steps. Lesson 04-kontext-und-tokens in Level 1 explains how context windows work technically, in case you need the mental model. Playbook claude-code-cost-controls-für-daily-driver shows how you limit the cost of your sessions, which is directly related to context management. And if you work a lot with sub-agents in multi-hour tasks, the playbook erster-sub-agent-in-30-min plus the L5 lessons on multi-agent patterns are worth it.

Source

This playbook leans on the official Claude Code documentation:

  • Slash Commands Reference: https://code.claude.com/docs/en/commands (last checked 2026-05-04)
  • Memory & CLAUDE.md: https://code.claude.com/docs/en/memory
  • Skills & Built-in Commands: https://code.claude.com/docs/en/slash-commands

Verified: /clear, /compact, /resume, /fork, /usage, /cost (alias for /usage), CLAUDE.md, CLAUDE_CODE_FORK_SUBAGENT.