Context Management: Why your AI Keeps Forgetting What You Said

One of the main reasons I see people getting poor results from agentic coding is that they're not managing their context properly. They're getting results like:

What is context?

The "context" is the LLM's short-term memory of:

By "managing your context", I generally mean: you try to keep your context usage below ~60% where possible. The exact number will depend on the model used, but 60% seems to be a good rule of thumb. The poor results you get from high context usage are commonly called "context rot".

Why do you want to keep context usage low?

Tools for managing context in Claude Code

You can check your context usage in Claude Code with the /context command anytime to see what it currently is. You can reset your context by clearing all memory of the current conversation with /clear.

You can also try to reduce your context usage by clearing it and restarting with a summary of the current conversation with /compact as well. It's basically the same "compaction" that happens automatically when you get to 100% context usage, but you control the timing.

Tactics

Clear aggressively

The main thing you'll want to do is to run /clear as often as you can without losing valuable conversation.

Check your starting context

You also need to check what your context usage is when you first start a conversation. You can call /clear and then /context immediately after to see what you're starting with. Hopefully you haven't overburdened Claude Code with a massive amount of documentation, MCP servers, or skills that have you starting out every conversation already pretty high. You may have thought "I'll give Claude all the information and power that I can to get the best results", but that can often backfire.

Use sub-agents

Using sub-agents can be a help as well. When you spawn a sub-agent, it gets its own fresh context window and reports back only its results, saving the main agent from all that context-pollution.

Manually compact

I personally like more control and transparency than /compact, but I like the idea. If I'm in a long valuable conversation, I like to have it create a summary in a temporary summary.md file that I can check for completeness and accuracy. If it looks good, I /clear and have it read that file in to continue from. This works really well if it's in the middle of long list of tasks.

Use plan mode

Use plan mode! It's great for conversationally iterating with Claude Code about what you want to do (both asking and answering questions, and sometimes asking for questions) until you have a well-conceived plan. Claude Code will then let you accept the plan, clear context and start execution fresh.

Set up context-tracking

Set up better context usage tracking. In Claude Code, I set up a custom status line that shows me the context at all times:

Create ~/.claude/statusline.sh that shows context % with color-coded warnings (requires jq to be installed):

#!/bin/bash
input=$(cat)
PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0' | cut -d. -f1)

if [ "$PCT" -ge 70 ]; then
    echo -e "\033[31m⚠️ CONTEXT ${PCT}%\033[0m"  # Red
elif [ "$PCT" -ge 50 ]; then
    echo -e "\033[33mContext ${PCT}%\033[0m"     # Yellow
else
    echo -e "\033[32mContext ${PCT}%\033[0m"     # Green
fi

Make it executable with chmod +x ~/.claude/statusline.sh, then add this to ~/.claude/settings.json:

{
  "statusLine": {
    "type": "command",
    "command": "~/.claude/statusline.sh"
  }
}

Watch for symptoms

Ultimately you need to watch for symptoms: If it's doing reckless things, ignoring instructions and generally giving poor results, it's probably time for a /compact or /clear. Don't let yourself get stuck in a doom loop where it makes the same mistakes over and over and you keep telling it to fix the same things.

Managing context is a skill. At first you'll need to consciously check your context usage and remind yourself to clear. Over time you'll start to feel when the context is getting bloated—you'll notice the quality degrading before you even look at the percentage. That's when you know you've internalized it.

The goal isn't to obsess over the number. It's to get consistently good results by giving the LLM the space it needs to think clearly.


,
← Back home