The 50% Context Rule prompt
Set up the "50% context rule" in my Claude Code: a live context meter in the
status line, a /handoff command that saves my session to a file, and a /pickup
command that resumes it in a fresh session. The idea: once context fills past
~50% Claude Code starts forgetting things, so I save a handoff file, start a
clean session, and run /pickup to continue. Everything is USER scope
(~/.claude/) so it works in every project.
Take stock first - don't change anything yet. Read ~/.claude/settings.json and
list ~/.claude/commands/. For anything below that already exists and matches,
skip it. If something exists but differs - especially an existing "statusLine"
in settings.json, or a command file with the same name - STOP, show me the
difference, and ask before changing it. Never overwrite my files.
(If a bare "claude ..." command is blocked inside this session, re-run it with
CLAUDECODE cleared: "CLAUDECODE= claude ..." on macOS/Linux, or
"$env:CLAUDECODE=''; claude ..." in PowerShell.)
STEP 1 - The live context meter (status line).
Write this file to ~/.claude/statusline-context.mjs EXACTLY as below (create the
.claude folder if needed; on Windows the path is
%USERPROFILE%\.claude\statusline-context.mjs):
#!/usr/bin/env node
// Live context meter for Claude Code's status line. Reads the session JSON on
// stdin and prints a color-coded bar showing how full the context window is,
// so you can see the moment you cross ~50% and it is time to hand off.
let raw = "";
process.stdin.on("data", (c) => (raw += c));
process.stdin.on("end", () => {
let d = {};
try { d = JSON.parse(raw || "{}"); } catch { d = {}; }
const cw = d.context_window || {};
// Prefer Claude Code's pre-calculated percentage; fall back to computing it
// from the token counts; default to 0 very early in a session.
let pct = cw.used_percentage;
if (pct == null) {
const used = cw.total_input_tokens;
const size = cw.context_window_size || 200000;
pct = used != null ? (used / size) * 100 : 0;
}
pct = Math.max(0, Math.min(100, Math.round(pct)));
// ANSI colors: green under 50, amber 50-74, red 75+. 50 is the handoff line.
const ESC = String.fromCharCode(27);
const RESET = ESC + "[0m";
const color =
pct >= 75 ? ESC + "[31m" : pct >= 50 ? ESC + "[33m" : ESC + "[32m";
// 10-segment bar.
const filled = Math.round(pct / 10);
const bar = "█".repeat(filled) + "░".repeat(10 - filled);
const model = (d.model && d.model.display_name) || "";
const flag = pct >= 50 ? color + " <- HAND OFF" + RESET : "";
const tail = model ? " " + model : "";
process.stdout.write(color + bar + " " + pct + "% context" + RESET + tail + flag);
});
Then wire it into ~/.claude/settings.json. Read the file first, parse it, and
add ONLY the "statusLine" key, preserving everything else. If a "statusLine"
already exists, stop and show it to me before changing anything. Add:
"statusLine": {
"type": "command",
"command": "node $HOME/.claude/statusline-context.mjs",
"padding": 0
}
Use the script's real absolute path so it doesn't depend on shell expansion. On
Windows the command is "node %USERPROFILE%\.claude\statusline-context.mjs"
(remember JSON needs doubled backslashes).
Now TEST it before moving on. Pipe a sample payload through the command and
confirm a colored bar prints:
echo '{"context_window":{"used_percentage":55}}' | node $HOME/.claude/statusline-context.mjs
You should see an amber bar reading "55% context <- HAND OFF". If "node" is not
found on PATH, tell me - we'll point the command at your node binary, or rewrite
the script with jq instead (same logic: .context_window.used_percentage // 0).
STEP 2 - The /handoff command (save the session).
Write ~/.claude/commands/handoff.md EXACTLY as below (create the commands folder
if it doesn't exist):
---
description: Save the current session to a handoff file so a fresh session can pick up where this one left off.
allowed-tools: Bash, Read, Write
---
We are about to hand this session off to a fresh one to reset the context window.
Write a single self-contained handoff file capturing everything the next session
needs - assume it cannot see this conversation at all.
1. Create the folder ".claude/handoffs/" in the current project if it's missing.
2. Write the handoff to ".claude/handoffs/handoff-<timestamp>.md", using the
shell for a sortable timestamp (e.g. date +%Y%m%d-%H%M%S).
3. Include, in plain language:
- Goal: what we are trying to accomplish overall.
- Done so far: key decisions made and work completed.
- Current state: exactly where things stand right now.
- Files touched: the important files and what changed in each.
- Next steps: the concrete next actions, in order.
- Open questions / gotchas: anything unresolved or easy to get wrong.
4. Be specific - real file paths, command names, and decisions, not vague
summaries. When done, print the file path and remind me to start a fresh
session (run /clear, or open a new Claude Code) and run /pickup there.
STEP 3 - The /pickup command (resume in a fresh session).
Write ~/.claude/commands/pickup.md EXACTLY as below:
---
description: Resume from the most recent handoff file written by /handoff.
allowed-tools: Bash, Read
---
Find the most recent handoff file and resume from it.
1. Locate the newest file in ".claude/handoffs/" (e.g.
ls -t .claude/handoffs/*.md | head -1). If the folder or files don't exist,
tell me there's no handoff to resume and stop.
2. Read that file in full.
3. Give me a 3-4 line summary of where we left off and what's next.
4. Then continue the work from the "Next steps" section - pick up exactly where
the previous session stopped.
When all three steps are done: confirm the status-line test output, tell me
anything you skipped (already installed) or that needs my input (e.g. node not on
PATH), and remind me to fully restart Claude Code so the status line and the new
/handoff and /pickup commands load.Claude Code gets dumber the longer you chat with it. The reason is simple: every message you send fills up its context window - the working memory it holds the whole conversation in. Once that's about half full, it starts losing the thread: forgetting decisions you made earlier, missing files it already read, and making mistakes it wouldn't have made on a fresh start.
The fix is the 50% rule. When your session reaches about half its context, tell Claude Code to save the session into a file - a handoff file that captures what you're doing, what's been decided, and what's left. Then start a brand new session that reads that file and continues from there. You keep all the progress, but Claude is working from a clean, near-empty context again - so it gets noticeably smarter and makes far fewer mistakes.
The trick to doing this at the right moment is a live context meter: a small readout that shows your session's context percentage as you work, so you can see the moment you're crossing ~50% and it's time to hand off. The prompt below installs the whole loop - the live meter, plus the commands to save the current session to a handoff file and resume it in a fresh one - so the workflow is one paste away instead of something you have to remember to wire up.