News

How to manage context and prompt caching in Claude Code

A short question can still carry a long session’s history. Learn when to stay in the same Claude Code session, when to start fresh, and what happens to the prompt cache when you switch models or tools.

Finley · FoxAI World3 min read
18
Finley gestures toward document and storage icons beside a laptop, beneath the Anthropic name.Claude Code Context

Conceptual cover created with AI.

You’ve spent the morning fixing a failing test in Claude Code. After several file reads and test runs, you ask one line about the error. That one line can carry the messages, project instructions and tool results accumulated so far. To manage your usage, keep the history that the next task needs and start fresh for unrelated work.

What Claude Code reuses from earlier requests

The context window is the space available for information the model can work with in a request. It can contain your messages, file contents and command output. Those are measured in tokens, the units the model uses to process text. The more history the request carries, the more input there is to account for.

Claude Code normally uses prompt caching automatically. The API checks whether the beginning of the request exactly matches material it recently processed. It can reuse that portion and process new material in full. Cached reads still count towards usage and are charged at a lower input rate on the API.

Finley sits thoughtfully at a laptop with the Claude symbol, surrounded by workflow and chart icons.
AI-generated conceptual illustration.

When to keep, compact or clear a session

Stay in the same session if your next step still depends on the test results or decisions you’ve just collected. When you move to unrelated work, /clear starts a fresh conversation. If you’re continuing the same work with a long history, /compact can replace that history with a summary. For example, ask it to retain the failing test, the files you changed and the reason for the change. Summarising takes a separate request, and details can drop out along the way.

I’d make that decision at the end of a task. When the next step depends on a test result, carrying it forward saves having to find it again. When the next task starts in a different part of the project, a short brief gives Claude enough to begin without taking the old conversation along.

What changes when you switch models or tools

Each model has its own cache. After a /model switch, the next request processes the conversation history without the previous model’s cached input. A model with a lower input rate can therefore be a poor choice for a single quick question in a long session. If that question stands on its own, you can start a new session with the model and just the details it needs.

MCP, short for Model Context Protocol, lets Claude Code connect to external tools. With tools loaded on demand, connecting or disconnecting a server during a session leaves the cached beginning intact. When tool definitions are loaded upfront, adding or removing a definition changes the request and invalidates the cache.

Give each request less to carry

Run /context to see what takes up space. Keep rules you need across the project in CLAUDE.md, the project instruction file. A procedure you only need for releases can live in a skill, a reusable set of instructions whose full content loads when used. You can also check which servers are connected with /mcp and decide which you still use.

Read the usage figures for your account

For the main Claude Code conversation, the default cache lifetime is one hour within a subscription’s included usage and five minutes with an API key. Reusing a cached beginning refreshes its timer. If you call the Messages API directly and enable caching, the default is five minutes, with a one-hour option that costs more to write.

In Claude Code, /usage shows plan consumption for subscribers and estimated token charges for API users, including cache reads and writes. From version 2.1.251, the main conversation’s cache summary also shows the share of input served from cache and whether it’s still available. Check those figures when a short follow-up in a long session uses more than you expected.

Ask AI about this article

Opens in a new tab with the question filled in.

Sources and research

Written by FoxAI World. The cover and illustrations are AI-generated conceptual artwork.

Your favourites

This list stays in this browser on your device. No account is needed, and other visitors cannot see it.

Use a story’s bookmark button to add your first favourite.