Skip to main content

Context Window Management

Module: Memory & Context | Lesson: 4 of 4 | Time: ~10 minutes

What You Will Learn

  • How to use /compact to summarize and free up context
  • How to manage long conversations effectively
  • When to start a fresh session vs continuing

Prerequisites

What Is the Context Window?

Every time you have a conversation with Claude, everything you say and everything Claude responds with gets stored in a temporary space called the context window. Think of it like a desk — it can only hold so many papers at once. The more you talk, the more papers pile up, and eventually the desk runs out of room.

The context window has a fixed size, measured in tokens (roughly 3/4 of a word each). Once it fills up, Claude can no longer see the earliest parts of your conversation. Important instructions, decisions, and context from the beginning of your session may get pushed out.

This is not a bug — it is a fundamental part of how large language models work. The good news is that Claude Code gives you tools to manage it effectively.


Why Context Management Matters

Here is what happens when you do not manage your context:

  1. You start a session and explain your project.
  2. You work on several tasks over 30-60 minutes.
  3. Claude's responses start to feel "off" — it forgets things you told it earlier.
  4. You repeat yourself, wasting time and tokens.
  5. Eventually, Claude may not even remember the original project setup.

With good context management, you can avoid all of this. You will have longer, more productive sessions where Claude stays sharp and focused.


Monitoring Context Usage with /cost

The first step in managing context is knowing where you stand. The /cost command shows your current token usage:

/cost

The output tells you:

  • Input tokens: How many tokens Claude has read (your prompts plus context).
  • Output tokens: How many tokens Claude has generated (its responses).
  • Session cost: The dollar amount for the current session.
Watch for rising input tokens

If your input token count is growing quickly, that means the context window is filling up. This is your signal to compact or start fresh.


Using /compact to Free Up Space

The /compact command is your primary tool for context management. It summarizes the entire conversation into a concise form, replacing the full history with just the summary.

Basic Usage

/compact

Claude reads everything in the conversation, writes a summary, and replaces the full history. You keep the essential information while freeing up a large chunk of the context window.

Guided Compaction

You can tell Claude what to focus on when summarizing:

/compact Focus on the database changes and API endpoints we discussed

This is useful when your conversation covered many topics but you only need to continue working on one of them.

When to Compact

Here are the best times to run /compact:

SituationWhy Compact
After completing a subtaskClear out the details of the finished work
Before starting a new subtaskGive yourself room for the new topic
When Claude starts "forgetting" thingsThe context window is probably full
After 15-20 minutes of active workProactive maintenance
When /cost shows high input tokensThe window is filling up

What Happens After Compacting

After /compact, Claude has a condensed summary instead of the full conversation. This means:

  • Claude remembers the key points and decisions.
  • The specific wording of earlier prompts is gone.
  • Code snippets from earlier may be summarized rather than preserved verbatim.
  • You have significantly more room for new conversation.
Compact is not perfect

The summary is good, but it is not a perfect record. Very specific details (like exact line numbers or precise error messages from 20 minutes ago) might be lost. If you know you will need specific details later, save them to a file or to your CLAUDE.md before compacting.


When to Start a Fresh Session

Sometimes /compact is not enough. Here are signs that you should exit and start a completely new session:

  • You are switching to a completely different project. Compacting a web development conversation before working on a data analysis project just adds noise.
  • The conversation has become very confused. If Claude is giving contradictory answers or seems lost, a fresh start is cleaner than trying to compact the confusion.
  • You have compacted multiple times already. Each compaction loses some detail. After 2-3 compactions in one session, the accumulated summaries can become muddled.
  • You have been working for over an hour. Long sessions tend to accumulate a lot of context. Starting fresh with a clear CLAUDE.md is often more productive.

To start fresh:

/exit

Then start a new session:

claude

Claude will load your CLAUDE.md and any memory files, giving you a clean starting point with your project knowledge intact.

Your CLAUDE.md is your safety net

This is why good project memory (from Lessons 1-3) is so important. When you start a fresh session, Claude does not remember your conversation — but it does read your CLAUDE.md. If your important decisions and conventions are saved there, starting fresh costs you almost nothing.


Practical Context Management Tips

Tip 1: Front-Load Important Information

Put your most important instructions at the beginning of a session. If you have specific requirements, state them in your first prompt or put them in your CLAUDE.md. Information at the start of the conversation stays in context the longest.

Tip 2: One Task at a Time

Instead of asking Claude to do five different things in one sprawling conversation, break your work into focused tasks:

  1. Start a session.
  2. Work on one task.
  3. Compact or exit.
  4. Start the next task.

This keeps context clean and focused.

Tip 3: Save Before You Compact

If your conversation produced important information — decisions, code patterns, conventions — save them before compacting:

You: Save the API endpoint naming convention we decided on to CLAUDE.md

Then compact:

/compact

Tip 4: Use /cost as a Habit

Get in the habit of running /cost every 10-15 minutes during active work. It takes one second and gives you early warning before context becomes a problem.

Tip 5: Combine Compact with a Focus Shift

When you finish one subtask and are about to start another, compact with focus:

/compact I'm done with the header component. Next I'll work on the footer.

This tells Claude to summarize the header work and set up context for footer work.


Context Management Workflow

Here is a complete workflow for a typical work session:

1. Start Claude Code
└── Claude loads CLAUDE.md automatically

2. State your task clearly
└── "I need to add a contact form to the website"

3. Work on the task (10-20 minutes)
└── Ask questions, make changes, iterate

4. Check usage
└── /cost

5. Task complete? Save important decisions
└── "Save the form validation rules to CLAUDE.md"

6. Compact before the next task
└── /compact Focus on the completed contact form

7. Start the next task
└── "Now let's add email sending functionality"

8. Repeat steps 3-7

9. End of session
└── /exit

Try It Yourself

Practice context management with a hands-on exercise:

  1. Open Claude Code in your practice project folder.
  2. Run /cost at the very beginning. Note the starting numbers (they should be low).
  3. Have a conversation — ask Claude 4-5 questions about your project files, request a few small changes, and discuss some design decisions. Spend about 5 minutes on this.
  4. Run /cost again. Notice how the numbers have grown.
  5. Save something important: Ask Claude to save one key decision to your CLAUDE.md.
  6. Run /compact with a focus instruction: /compact Summarize the changes we made and decisions we reached
  7. Run /cost once more. Compare to step 4 — your input tokens should be significantly lower.
  8. Test Claude's memory: Ask "What have we been working on?" — Claude should know the key points from the compacted summary.
  9. Start fresh: Run /exit, then start a new claude session. Ask "What do you know about this project?" — Claude should know what is in CLAUDE.md but not the conversation details.

What You Learned

  • The context window is a fixed-size space that holds your entire conversation. When it fills up, Claude loses track of earlier content.
  • /cost shows your current token usage — use it every 10-15 minutes to monitor context.
  • /compact summarizes the conversation to free up space. You can guide the summary with a focus instruction.
  • Start a fresh session when the conversation is too confused, you are switching projects, or you have compacted multiple times.
  • Save important information to CLAUDE.md before compacting, so it persists across sessions.
  • A good workflow is: work on a task, save key decisions, compact, then start the next task.

Help Us Improve

How was this lesson? Take 2 minutes to share your feedback — it helps us make the tutorials better for everyone.

Give Feedback →

Next Up

Next: Capstone: Build a Memory System