You finish a bug fix, then ask the same AI agent session to write a new feature. The agent still carries every file, error, and failed attempt from the bug fix. It gets distracted by old details, forgets your newer instructions, and every message costs more than it should.
An AI coding agent's context window holds the whole conversation: every message, every file it reads, and every command output. The agent sends all of it to the model with every request. Leftover context from an old task hurts you in three ways:
Anthropic's Claude Code best practices put it plainly: "LLM performance degrades as context fills." The Claude Code cost guide adds: "Stale context wastes tokens on every subsequent message."
Start every new task with a fresh context. Run /clear, or start a new session.
Anything the agent needs on every task belongs in a file it loads at startup, such as AGENTS.md, not in a long-running conversation. See Do you write an AGENTS.md?
If a task goes wrong and you have corrected the agent more than twice, clear too. Start again with a better prompt that includes what you learned.
Compacting asks the agent to summarise the conversation so far and replace the history with that summary. It frees space but loses detail. The summary keeps what the agent thinks matters, not necessarily what you need.
Compact only when one task outgrows the window and its details still matter. Tell the agent what to keep:
/compact focus on the failing migration test and the fix we agreed
Claude Code also compacts automatically as the conversation nears the context limit. Don't rely on auto-compact to carry you from one task to the next. It keeps a summary of old work you no longer need.
Note: Compacting a large context is itself a large request, because the agent reads the whole conversation to summarise it. /clear costs nothing.
Figure: Compacting has a cost of its own
Big tasks often run in phases, such as plan, implement, and roll out. Don't compact across phases. Instead:
PLAN.mdThe new phase starts with clean context focused on its own job. The file is a precise handoff that you can read and edit, unlike an automatic summary.
One long session handles a bug fix, then a new API endpoint, then a docs update. The developer never clears the context and relies on auto-compact whenever the window fills. By the third task, the agent mixes up files from the first task and ignores the latest instructions.
❌ Figure: Bad example - One session carried through three unrelated tasks with auto-compact
The developer runs /clear after the bug fix and before the new endpoint. Midway through the endpoint, the window fills, so they run /compact focus on the failing migration test and the fix we agreed. For the larger docs overhaul, they ask the agent to write PLAN.md, clear, and start the implementation from the file.
✅ Figure: Good example - Clear between tasks, compact with a focus mid-task, and hand off phases with a plan file
| Tool | Clear the context | Compact the context |
|---|---|---|
| Claude Code | /clear | /compact [instructions] |
| OpenAI Codex CLI | /clear or /new | /compact |
| GitHub Copilot CLI | /clear or /new | /compact [focus instructions] |
Commands change often. Check each tool's documentation for the current list.