← All articles

Blog

Claude Code: stop paying for old history

I kept one terminal open for days. When I went through my logs, roughly half of my costs turned out to go to sending old history along again and again. Here is how you fix that.

I work with Claude Code every day. And I did something a lot of people do: keep one terminal open and do everything in it. It feels efficient, because Claude seems to remember everything. When I went through a week of my own logs, it turned out to be an expensive habit.

The problem in one sentence

At every step, Claude Code sends the entire conversation so far to the model again. A short session is cheap. A session that has been open for days drags hundreds of thousands of tokens of history along, and pays for it again with every command.

What my own logs showed

Measured (18–28 November)Result
Total tokens in 10 days6.6 billion
Old history sent along again97.6%
What Claude actually wrote0.24%
Share of four sessions I never closed87%
Average history per step in those sessions370,000–420,000 tokens
Times the largest session was summarised automatically44
Steps after a tool (mostly terminal commands)about 90%

History that gets sent along sits in the cache and is much cheaper per token: on Opus 5.5, a cache read costs one twentieth of normal input. That is why it is not “97% wasted”. But because it is so many tokens, in my week it was still about half of the costs. With smaller sessions I could have saved an estimated one third, without doing less work.

And that long session wasn’t even smart: after 44 summaries, Claude is working from a summary of a summary. Details from a week earlier are long gone, while you keep paying.

Check your own numbers

  • /context shows how full the current history is and what is in it.
  • Status line: with /statusline you put the context size permanently below your input line. Ask for example: show the context in tokens and %, red above 100k.
  • ccusage reads your local logs and shows usage per day and per session: npx ccusage@latest daily or npx ccusage@latest session.

Rule of thumb: under 100k tokens of context is healthy. Above that, every step gets expensive and it is time to save and start over.

The routine: one task, one session

WhenYou typeWhat happens
Task done, or Claude asks at 100k/wrapupClaude puts rules in CLAUDE.md and ongoing work in status.md
Right after that/clearThe history is empty, the context drops to about 20k
Start of the new sessionRead status.md and continueClaude picks up exactly where you were
Need something old?/resumeEvery earlier session can be brought back

“But then Claude is dumb again”

Only if the knowledge lives in the conversation. Put it in files:

  • CLAUDE.md in your project: permanent knowledge, such as rules, paths, deploy steps and agreements. Claude Code reads this in every session. Keep it short, under 200 lines.
  • status.md: where you are right now. Open tasks, what is running, today’s decisions.
  • docs/<topic>.md: details per topic, which Claude only reads when the task calls for it.

A fresh session that reads these files in full is sharper than a ten-day-old session working from a summary. And it doesn’t get confused by plans you had already dropped.

How to set it up (10 minutes)

1. The /wrapup skill

Save this as ~/.claude/skills/wrapup/SKILL.md. With disable-model-invocation it only runs when you type /wrapup.

---
description: Save what matters to CLAUDE.md / status.md before /clear
disable-model-invocation: true
---
We're about to /clear this session. Before that:

1. Update CLAUDE.md with anything PERMANENT we learned (rules, paths,
   deploy steps, decisions). Keep it under ~200 lines; details go to
   docs/<topic>.md.
2. Overwrite status.md: open tasks, what is still running (agents,
   shells, cron jobs), today's decisions, key files. Max 60 lines.
3. Reply with one line: what you saved, then
   "Type /clear, then: Read status.md and continue."

2. The reminder at 100k (hook)

Save this as ~/.claude/hooks/ctx-check.py. The script runs on every message you send. Above 100k, and after that at every additional 50k, it has Claude ask whether you want to wrap up. It never blocks anything.

#!/usr/bin/env python3
import json, sys, os
try: data = json.load(sys.stdin)
except Exception: sys.exit(0)
path, sid = data.get("transcript_path", ""), data.get("session_id", "x")
LIMIT, STEP = 100_000, 50_000
ctx = 0
try:
    for line in reversed(open(path, errors="ignore").readlines()):
        e = json.loads(line); usage = (e.get("message") or {}).get("usage")
        if e.get("type") == "assistant" and usage and not e.get("isSidechain"):
            ctx = (usage.get("input_tokens", 0) + usage.get("cache_creation_input_tokens", 0)
                   + usage.get("cache_read_input_tokens", 0)); break
except Exception: sys.exit(0)
state = os.path.expanduser(f"~/.claude/hooks/.ctx-{sid}")
last = int(open(state).read()) if os.path.exists(state) else 0
if ctx < LIMIT or ctx < last + STEP: sys.exit(0)
open(state, "w").write(str(ctx)); k = ctx // 1000
msg = (f"Context is now {k}k tokens. BEFORE doing the task, ask the user in one "
       f"short line: 'Context is {k}k - run /wrapup and then /clear?' "
       "If yes, tell them to type /wrapup. If no, just do the task.")
print(json.dumps({"hookSpecificOutput": {"hookEventName": "UserPromptSubmit",
                                         "additionalContext": msg}}))

3. Turn the hook on

Add this to ~/.claude/settings.json, merged with what is already there, and restart Claude Code:

{
  "hooks": {
    "UserPromptSubmit": [
      { "hooks": [ { "type": "command",
                     "command": "python3 ~/.claude/hooks/ctx-check.py" } ] }
    ]
  }
}

Easiest way: paste this whole section into Claude Code and say: “Install these three parts exactly like this, merge them with my existing settings.json (make a backup first) and test the hook.”

Working with lots of agents? Keep the lead thin

With one lead terminal handing out work, you get the same problem. Every report from an agent ends up in the lead’s history. A lead only stays cheap if it stays thin:

  • The lead does no work itself. No terminal commands, no browser, no reading large files. It plans, distributes and keeps track.
  • Every worker is a fresh session for one task, for example claude -p "task…" --model sonnet, and stops when it is done.
  • Results go to files. The worker only reports “done, see file”.
  • The lead’s memory is status.md, so you can clear it every morning.

Save even more

  • Use a cheaper model for routine and scheduled tasks: --model sonnet or /model.
  • Put in CLAUDE.md: always limit command output (| tail -50, grep), never dump whole logs or pages. One large output is sent along again with every following step.
  • Let subagents do the heavy reading. Then only their conclusion ends up in your main session.
  • Use /compact with a hint at a quiet moment, instead of waiting for it to happen on its own.
  • Never clear in the middle of a task, only at natural moments.

Try it for a week

Then run npx ccusage@latest daily again and compare. The four sessions I never closed were 87% of my usage. Split into separate tasks, the same work would have been a lot cheaper.

Want to share this with your team? Download the complete guide as a PDF, including all the code.

Download PDF

Tell me about your project.

What do you have now, and what would you like to be able to do? Feel free to send a link to your current site. Then we can discuss where I can help.

info@gerdkoetje.nl