Original Reddit post

Most people hear about context engineering but don’t really know how to actually do it properly. I made this full guide to demystify the concept from first principles. Motivations and Preface Why this happens at all LLMs are stateless. Every call starts with an empty context window. There is no memory between turns. Agents fake state. Your conversation is an array sitting in a file, and the harness re-pastes the entire thing into the model’s context window every single time you hit enter. Which means you can’t teach an agent anything. You can only paste things into its context. The context window is the model’s entire observable universe. If something isn’t in the window, or implied by the window, it does not exist as far as the model is concerned. Two consequences fall out of that. Autoregressive sensitivity. The model predicts one token at a time, and every token it emits becomes input for the next one. A tiny variation in context changes one predicted token, that token changes the next, and the divergence compounds across the whole response. Small change at the start, huge delta by the end. Finite attention. Every token in the window competes for attention budget. Go from 100 tokens to 200 and on average every original token gets half as much. A relevant token holding a third of the attention weight can drop to 0.3% once there’s enough junk around it. The part people get wrong: context rot has no drop-off point. There’s no magic number where it kicks in. Attention gets stretched from the first token you add. The literature shows accuracy dropping up to 80% as context grows. Let a dirty session run and you’re working at a fraction of the performance you’re paying for. Lost in the Middle is the classic paper here. As context grows the middle is what gets ignored first, because the model learns in expected value that the important stuff lives at the beginning and end. So it skims the middle like a speed reader. Bury something critical in the middle of a giant dump and the model will miss it. How rot actually shows up Your rules get ignored. You put “no em dashes” in your CLAUDE.md , and 40 turns later the em dashes are back. The instruction is still sitting right there in the window. It just stopped getting attention. This is also why piling on more rules backfires: the model has to allocate attention to all of them on every single turn. The model gets dumber. Same model, same question, worse answer. What you’re talking to at 150k feels like a different thing from what you started the session with. It gets lazy. It hands work back to you and defers decisions it should be making. Explicit instructions just get skipped. Something dangerous gets stuck. A bad instruction, a contradiction, a wrong file it read, a poisoned skill description. Once it’s in the window it cannot leave. It gets re-read every single turn, forever. And these compound. A degraded model makes a worse decision, that decision goes back into context, and now it’s reading its own bad work next turn. That’s why a bug fix that fails twice tends to go round and round until the codebase is a mess. Context pollution is worse than rot Rot is about quantity. Pollution is about quality. Context that’s wrong but reads as convincing eats attention more aggressively, because it looks important. Almost always this happens on its own, with no adversary involved: a hallucination Claude wrote earlier, a stale spec, a wrong plan assumption, an unresolved debugging loop. Context confusion is the common flavor. Too many semantically similar tools or agents (ui-agent, frontend-agent, nextjs-agent) and the model calls the wrong one at the wrong time. A client of mine left the deep research skill model-invocable, so its description sat in context permanently. During one of his autonomous runs it talked the model into running deep research on a task that had no need for it. Burned half a week of usage. Step zero: measure and trim your baseline Type /context all in Claude Code and look at what you’re paying for before you’ve done anything. Mine sits around 16k. Here’s how:

  • Disabled three default tools I never use. That alone was ~7k tokens. Disabled artifacts too.
  • No CLAUDE.md. There’s almost never context I want injected on every single turn. Repo-specific ones are occasionally useful and I still rarely bother.
  • Skills set to disable-model-invocation. Skills work by injecting frontmatter into context so the model knows when to call them. That frontmatter is expensive and it’s there whether you use the skill or not. Disable model invocation and it stops being injected. The skill still works when I invoke it myself.
  • Deep research specifically: disable model invocation on it. Use /deep-research when you actually want it.
  • Turn off auto-compact in /config. You’re not going to need it, and if compaction fires mid-task the model loses track of what it was doing and leaves you with half-baked code. Boris Cherney said recently that you can theoretically delete all your system prompts and tools and Opus 5 still performs decently. I haven’t tested it. But it’s the direction things are going, and it’s why I stay on Claude Code: you can strip it down to close to a bare LLM if you want. The SCRUB framework (My Simple Acronym for Active Context Engineering) These five are your entire action space for context engineering. Everything else is a mixture of them. S: Subagents. A disposable worker with its own context window and the same intelligence as your main agent. Spawn one to read and process a pile of text and hand back only the conclusion. The subagent soaks up the exploration and the dead ends. Your orchestrator stays lean and makes the decisions. Treat them as cheap, spawn one like you’d call a function. Literally just say “spawn a subagent to look into X.” Run them in parallel and you cover enormous ground without touching your window. C: Cut (/rewind). My favorite command in Claude Code. Since state is just an external file, there’s no reason you can’t delete the last few rows of it. You’re time traveling in active memory. Two ways I use it constantly:
  • Restore only the conversation and leave the code alone. The code stays on disk as an artifact, the conversation gets trimmed, and you tell the model “I made these changes myself.” Spend five turns fixing a bug, then rewind to before you hit the bug and inform it of the result. You get all that context back for free. In a demo session I pulled back 75k tokens this way.
  • When something simple has failed two or three times, you’re swimming upstream against a damaged trajectory. Pattern interrupt: rewind to before you started fixing and re-prompt with better framing. Claude Code can restore code and conversation because it has internal version control. Codex can’t. Deleting history is a superpower. The model’s entire reality is that file, and the file is malleable, so you get to decide what survives into the next prediction. You can branch trajectories, explore, and keep only the path that deserves to live. The model never knows it took a bad path. Elite users hit rewind 50 to a few hundred times a day. Most people have never touched it. R: Reduce (/compact). Compaction forks your agent, hands the duplicate your state file, asks it to summarize itself, and injects the summary back. I use it at breakpoints where I need the gist and the nitty-gritty can go. It’s also a speed play when I don’t want to think hard about fidelity. I always fire it manually. U: Upload (handoff). Offload the parts of the window you want to keep, to a file or just your clipboard, then clear and paste it back into a clean slate wrapped in XML tags. I use <context> for the pasted material and <user_prompt> for what I want next. Copying conversation output straight to the clipboard and pasting it back is brute force and it works extremely well. The other version is hardening context into a real artifact. Codebase docs and a build list, or a handoff file you re-inject after clearing. Same principle, you’re moving state out of the window and into something persistent. One session I did this on went from 222k tokens to 26.9k, and the model still knew everything it had done and what came next. B: Burn (/clear). A full reset, the same as a new session without leaving the one you’re in. Use it at good breakpoints or for genuinely new work. You’d be surprised how often you can get away with it. Pair it with docs or a lean CLAUDE.md you can point at to get back up to speed fast. How I actually use them S and C are my proactive levers. I look for excuses to use subagents and rewind from the first message, way before I’m anywhere near 150k tokens. R, U and B are more drastic and I save them for real breakpoints. I work down the list. If I can get away with a subagent, I do. If I can’t, rewind. Then down the acronym, since it’s roughly ordered from least to most destructive. The reason I framed it as five letters is so you have a mental model of what your moves on the board even are. Context engineering is a balancing act. Strip out too much and the model doesn’t know what’s going on and starts filling gaps with assumptions, which is how you get hallucinations. Leave too much and rot degrades you. You’re aiming for the information density where the model has exactly what it needs and nothing else. Most of the time you won’t get a clean before/after comparison like the one I opened with. Be proactive anyway. The science is there. Full video link (to see all the principles in action): https://youtu.be/F_bpvXlUSwU submitted by /u/agenticlab1

Originally posted by u/agenticlab1 on r/ClaudeCode