In this categoryClaude Code ยท 45
Claude CodeBeginner

How to Keep an Agent Session Focused by Clearing Context

Clear stale context between unrelated tasks so the agent stops dragging earlier work into a fresh request.

6 minBeginner

A long session accumulates context, every file read and every earlier task. When you switch to something unrelated, that history can leak into the new work and confuse the agent. Clearing context between distinct tasks keeps each request clean and the responses sharp.

What you need

  • An agent session you have used for a while
  • A new, unrelated task to start
  • About 5 minutes

Step 1: Notice the leak

The tell is an answer that references earlier, irrelevant work, like the agent mentioning a module you finished an hour ago when you ask about something new.

Agent - context bleed
You
Add a health check endpoint.
Agent
Should this reuse the invoice formatter we built earlier? (That work is unrelated to a health check.)
Old work leaks into an unrelated request.

Step 2: Clear before the new task

Run the clear command to reset the conversation. Your files and settings stay; only the chat history is wiped, so the next request starts clean.

session
/clear

Step 3: Restate the new task fresh

After clearing, give the new request with the context it needs on its own. The agent now reasons from a blank slate instead of a tangle of old threads.

Clear vs compact
Use clear when switching tasks entirely. If you want to keep the thread but trim its size, use compact instead, which summarizes the history rather than dropping it.

Result: each task runs on its own clean context, so answers stop dragging in unrelated work and the agent stays fast and on point.

Watch related tutorials

Free weekly email

New guides in your inbox

Fresh step-by-step how-to guides as we publish them. One email a week, no more.

Tags
#context#session#workflow#performance#tips