Mastering 1M‑Token Context Windows in Claude Code: Preventing Context Rot

This guide explains how to manage Claude Code's 1 million‑token context window using commands like /rewind, /compact, subagents, and new sessions, offering practical strategies to avoid context rot, improve reliability, and reduce costs during long‑running programming tasks.

High Availability Architecture
High Availability Architecture
High Availability Architecture
Mastering 1M‑Token Context Windows in Claude Code: Preventing Context Rot

Introduction to Context, Compression, and Context Rot

The context window defines everything the model can see when generating a response, including system prompts, prior dialogue, tool calls, outputs, and read files. Claude Code supports up to 1 million tokens . As the window fills, performance degrades—a phenomenon called context rot , where excess tokens distract attention and stale information interferes with the current task.

Context window illustration
Context window illustration

Each Dialogue Turn Is a Fork Point

When a task is finished and the context contains tool calls, outputs, and instructions, you have five possible actions for the next step:

Continue – send the next message in the same session.

Rewind (/rewind or double‑Esc) – jump back to a previous message and restart from there.

Clear (/clear) – open a brand‑new session, optionally seeding it with a brief summary of the previous work.

Compact (/compact) – ask the model to summarize the current conversation and replace the history with that summary.

Subagents – delegate the next chunk of work to a fresh‑context agent and later retrieve only its final result.

While “Continue” feels natural, the other four options are essential tools for effective context management.

When to Start a New Session?

Rule of thumb: start a new session whenever you begin a distinct new task. The 1 M token window lets you reliably handle long‑running jobs, such as building a full‑stack application from scratch. If tasks are related and you need some shared context (e.g., documentation for a recently implemented feature), you may keep the same session, but be aware that re‑reading files in a new session incurs extra latency and cost.

Use “rewind” Instead of “fix”

The most robust habit is to use rewind . In Claude Code, double‑pressing Esc or running the /rewind command jumps back to any earlier message, removing all later messages from the context. This is usually better than simply appending a corrective instruction.

Example: after reading five files and trying a failing approach, instead of saying “That didn’t work, try method X,” you /rewind to the point right after the file reads, then issue a clarified command like “Don’t use method A; use B because the foo module lacks the required interface.”

Compact vs. Clear

When a session becomes too long, you can either /compact or /clear:

Compact – the model creates a lossily compressed summary that replaces the history. It requires no manual effort, and Claude can decide which details to retain. You can guide it, e.g., /compact focus on auth refactor, drop test/debug noise.

Clear – you manually write a concise brief (e.g., “We are refactoring the auth middleware; constraints X; files A and B; discarded approach Y”) and start with a clean slate. This is more work but gives you full control over what stays in context.

Why Bad Compression Happens

Poor compression often occurs when the model cannot predict the direction of your work. For instance, an automatic compaction after a long debugging session may discard a later “fix warning in bar.ts” because the summary focused on the earlier debugging narrative. With a large context window, you can proactively run /compact after you have a clear plan, reducing the chance of losing critical information.

Subagents: Fresh Context Space

Subagents provide an isolated, clean context window for heavyweight intermediate work that you do not need to keep afterward. When Claude spawns a subagent, it operates in its own context, completes the assigned task, and returns a final report to the parent.

Typical prompts include:

“Launch a subagent to verify the results of this spec file.”

“Ask a subagent to study another codebase and summarize how it implements the auth flow, then implement the same pattern.”

“Instruct a subagent to write documentation for this feature based on my recent git changes.”

Before delegating, ask yourself whether you will need the raw outputs later or only the conclusions.

Summary

Every time Claude finishes a round of dialogue and you are about to send a new message, you face a decision point. While future versions may automate these choices, mastering /rewind, /compact, subagents, and session boundaries today is the most effective way to keep Claude Code producing high‑quality code efficiently and cost‑effectively.

Author Thariq (@trq212) is a member of the Anthropic Claude Code team, a former YC W20 participant, and has a background at MIT Media Lab. He focuses on AI agents and context management, aiming to build tools that help developers make the most of long context windows.
Closing illustration
Closing illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AI agentsPrompt engineeringContext Managementtoken limitClaude Code
High Availability Architecture
Written by

High Availability Architecture

Official account for High Availability Architecture.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.