Context, tokens and cost: keep AI coding sessions lean

Why long AI coding sessions get slower and pricier, and the simple habits that fix it: /clear between tasks, /compact with a focus, /usage to see where it goes, and the right model and effort for the job.

By Shakir Sajad & Claude (AI by Anthropic) · 2 min read · Updated 2026-11-19

hourglass cash illustration

Step 10 of the agentic coding learning path.

Why long sessions cost more

Anthropic's best practices explain it simply: every request sends the whole conversation. That's discounted while the cache is warm and full price once it expires. The docs say the cache lasts about an hour on a subscription and five minutes on an API key.

Running out is rare now (most current models have a 1M-token window, and Claude Code compacts on its own). Paying for context you don't need is the real problem.

The four commands

CommandWhat it doesUse it when
/clearStarts fresh, no historySwitching to an unrelated task
/compact {focus}Summarizes the chat, keeps what you nameSame task, long history
/usageShows where your tokens wentOnce a week, or when limits bite
/rename + claude --resumeNames a session to pick up laterWork that spans days

/clear costs nothing. /compact is itself a big request on a long session, since it reads everything to summarize it, so don't run it every few minutes.

Habits that keep it lean

  • One task per session. Done? Commit, then /clear.
  • Short memory file. Every line in CLAUDE.md loads on every task. See CLAUDE.md and AGENTS.md.
  • Two strikes, then restart. After two failed corrections, /clear and write one better prompt. The docs say this works better almost every time.
  • Watch background work. Loops, goals and subagents keep sending requests while you aren't typing.
  • Match model and effort to the task. Strong model and high effort for planning and hard bugs; lighter for small edits.

A prompt for a clean handover

Before you /clear in the middle of something big, save the state to a file so the next session can pick it up:

Prompt
Before I clear this session: write a short handover to docs/PENDING.md. What's done, what's left, the next step, and any decision I made. Keep it under 15 lines.

That's exactly why this site has a docs folder for agents.

How I keep it cheap

I'm on a budget, so my global rules tell Claude to remind me to run /compact at the end of every reply. It sounds small. It keeps long sessions from quietly getting expensive. I also keep heavy work sequential, one task at a time, which suits both my laptop and my token bill.

What's next

Stop retyping good prompts: build a prompt library.

Do this now

  1. Run /usage See what used your tokens this week. (5 min)
  2. Use /clear between unrelated tasks today New task, new context. It costs nothing. (2 min)
  3. Try /compact with a focus /compact keep the API changes and the test commands (5 min)
  4. Name a long-running session /rename it, then claude --resume <name> tomorrow. (5 min)
  5. Trim your CLAUDE.md Every line loads on every task. Cut what the agent can find in the code. (10 min)

Frequently asked questions

What is the context window?

Everything the model sees on each turn: your instructions, files it read, command output and the whole conversation so far. Every request sends all of it again.

What's the difference between /clear and /compact?

/clear starts fresh with no history, which is free and best between unrelated tasks. /compact summarizes the conversation so you can keep going on the same task with less history. On a long session /compact is itself a big request.

Will I run out of context?

Rarely now. Anthropic's docs say most current models have a 1M-token window and Claude Code compacts automatically near the top. You still pay for context you don't need, so keep sessions focused.

Does a cheaper model save money?

Often, for simple tasks. Use the strongest model and higher effort for hard problems and planning; use a lighter setting for small, clear edits.

Sources