Context Engineering with Claude Code


Context Engineering with Claude Code

Effectively managing context is crucial to getting good results from AI coding agents. After a few months of using Claude Code on both new projects and a large-scale project, here are some context engineering practices that have consistently worked well for me. While I’ve primarily used Claude Code, I believe these practices apply to any agentic coding tool.

Context Engineering

My approach to context engineering boils down to two key practices:

  • Providing enough relevant information for an agent to accomplish a task correctly and successfully
  • Maximizing free space in the context window to give the model enough room to reason effectively

Providing the model with relevant and complete information helps it focus on important details and reduces guesswork and unnecessary searching. In general, the more information you provide, the better. However, it’s equally important to be selective about which information you include and ensure you’re not missing critical details or adding unnecessary noise.

Free space in the context window is imperative for the model’s ability to think and reason. The more free space, the better responses you’ll get. Any information in the window that isn’t directly related to the problem you’re solving wastes the model’s thinking power.

Research, Plan, Implement

My workflow enforces good context management by splitting work into three distinct phases:

  1. Research: Gather context needed for planning. Output: research or context files documenting where to find code, how it works, its purpose, etc.
  2. Plan: Create a plan from requirements and context files. Review and refine. Output: a well-defined plan an agent can use to generate code.
  3. Implement: Take the plan file and generate code.

I clear the context window between each phase and usually clear it several times during implementation as the agent works through each task. This ensures the model has plenty of context window space to focus on the current task.

These phases force me to think carefully about which information is important for the next session. I review the output of each phase to ensure only the best and most accurate information carries forward. The more time I spend during research and planning phases, the less polish and cleanup I need to do with generated code.

Context Files

I rely heavily on context files—external documents that supply the agent with information outside its training data. They’re purpose-built to provide just the right context for a particular task.

A context file may contain:

  • Code references: Specific file paths and line numbers for key components
  • System relationships: How modules connect, depend on each other, and share data
  • Current state: What’s been completed, what’s broken, and what needs to happen next
  • Domain knowledge: Business logic, architectural decisions, and project-specific patterns

I’ll ask Claude to research a module, set of modules, or topic, and it will learn everything relevant to my prompt and save a context file that I can reference later during planning or implementation.

These files are especially powerful when working in large codebases or with information the model doesn’t already know. They make it possible to bring the right details into the window at the right time, without filling it with noise.

Delegate Context with Sub-Agents

I use sub-agents to handle research and analysis so my primary agent’s context window stays focused on the main task.

Claude Code excels at navigating codebases and uncovering relationships between files, functions, and components. But finding that information can quickly fill the context window with tool usage and intermediate steps. Most of that detail becomes unnecessary once the answer is known. By offloading this work to sub-agents, I capture useful findings while keeping my main agent’s window clear.

In practice, I spin up sub-agents for tasks like analyzing code, locating specific files, or searching the web. Each sub-agent uses its context window for heavy lifting and passes back only distilled results. This works especially well during the research phase, where a primary agent might coordinate several specialized sub-agents simultaneously (such as two focused on web research and three scanning the codebase) to gather everything needed in parallel.

Keep Sub-Agents Focused and Minimal

Sub-agents are powerful, but each one consumes context window space with its description. Use them strategically rather than abundantly.

Rather than creating role-based teams of agents, I use sub-agents primarily to delegate context-heavy tasks that would otherwise clutter my main agent’s window.

Monitor Context Window Usage

I check my context window usage frequently and try to keep it around 50% free.

When nearing the limit, I wrap up by asking Claude to summarize next steps that I can use to start a new session. I prefer this over /compact because I can see exactly what information loads into the next context window and make edits to trim it down if needed. I also end up with files I can reference if I need to start a new Claude Code process.

If I hit the limit during plan mode, I ask Claude to summarize next steps in the plan document, then run /clear followed by /plan the-plan.md, which restarts plan mode with the same plan file and continues from where we left off.

I ensure phases are planned to be short enough for completion within the context window, so I don’t hit the limit during implementation.

ABC: Always Be Clearing

I clear context frequently and always between research, plan, and implement phases. I get what I need from the model, write the information to a file, and move on with a fresh context window. Never work until you run out of context window space (auto-compact in Claude Code).

Avoid Compact

I avoid /compact because I prefer choosing what context is relevant for moving forward with a task in a fresh context window. It’s unlikely that every interaction with the agent contains useful information to carry forward.

I’ve gotten much better results since I stopped compacting and instead intentionally pause to summarize progress before picking up again in a fresh context window. This was particularly noticeable when debugging.

Previously, I would work on a bug until filling the context window, compact, and continue. Now, I ask the agent to update findings and next steps in a context file and start the next debugging session fresh with only key pieces of information.

Minimal MCP

MCP is generally considered advantageous for context engineering because it gives agents tools to pull in additional context. However, remember that every tool description from every MCP server loads into the context window in every session. You’re unlikely to use every MCP in every session.

I haven’t found MCP particularly useful for my work. When my agent is focused on solving a problem, it doesn’t need JIRA or GitHub MCP tools polluting its context window.

Frequently Audit CLAUDE.md

Remember that both your project and personal CLAUDE.md files load into every session’s context window.

I use CLAUDE.md for simple instructions relevant to all tasks. Although not every piece of information is relevant to all tasks, I minimize the file’s content to keep it focused on explicit instructions that constrain behavior.

For example, my CLAUDE.md contains no rules or guidelines for writing tests. When I’m in the planning phase, I include a context file about test writing and bake that information into my plan.

Conclusion

After a few months of working this way, I’ve realized that the real skill is managing the context window. Clearing often, writing things down, and being selective about what I carry forward has dramatically improved my productivity with Claude Code.

These practices started as a way to survive in a massive codebase, but I’ve found them equally useful on new projects. No matter the codebase size, the more intentional I am about context, the better the results.