ES

How to manage context when coding with AI

Leo G.7 minLeer en español

An office desk buried under papers, discs, and stacked folders, with a whiteboard full of notes behind it.
An office desk buried under papers, discs, and stacked folders, with a whiteboard full of notes behind it.

At the start of the session you told the agent “this project uses pnpm, not npm.” It said got it. Two hours and a handful of fixes later, it runs npm install as if you’d never said a word.

It didn’t get distracted or ignore you: its desk filled up. This article explains what that desk is, which is called context, and four habits to keep it from overflowing. There’s nothing to install or configure: they apply from your next conversation and work with any tool.

What context is

Context is everything the language model has in front of it when it answers. It isn’t just your last message. It’s the whole conversation: what you asked, what it replied, every file it opened, and every output of every command it ran.

The easiest way to think about it is as a desk. The model has no separate memory, no drawer where it keeps things for later: what isn’t on the desk doesn’t exist. And the desk has a fixed size.

This applies to the model and to the coding agent, the program around it (Claude Code, Cursor, Copilot) that lets it read files and run commands. The agent is the one putting things on the desk. The model only sees what’s there.

Why it fills up without you noticing

You see the conversation as a chat: a few short messages. But there’s much more on the desk than what you see on screen.

An example. You ask it to fix a failing test. To do that, the agent:

  • opens the test file (200 lines),
  • opens the file under test (600 lines),
  • runs the whole test suite, which prints another 300 lines,
  • and since something doesn’t add up, opens two more files to see where a value comes from.

Your request was one line. What ended up on the desk is over a thousand. And all of it is still there when you ask for the next thing.

A full desk doesn’t break anything all at once. What happens is subtler: what you put there first loses weight. Your note about pnpm is still there, but now it’s competing with thousands of lines of code and test output. At some point, it loses.

Four habits that are enough

Each habit ends with the concrete steps in Claude Code. Other agents name the commands differently, but the idea is the same.

1. One task, one conversation

This is the habit that makes the biggest difference. When you finish something and are about to start something else, open a new conversation.

If you fixed a login bug and now you’re going to write a sales report, nothing left on the desk helps you: the login files, the failed attempts, the test output. But it’s all still there, taking up room and getting in the way. A new conversation starts with a clean desk.

The temptation is to stay in the same one “because it already knows the project.” What it knows about the project is in the code, and it can read it again. What you’re dragging along is mostly noise.

Step by step:

  1. You finish the login fix and check that the tests pass.
  2. You commit.
  3. You type /clear. The conversation is emptied; the project and the CLAUDE.md are still there.
  4. You ask for the new task: “Build the monthly sales report.”
  5. If you later need to go back to the login conversation, /resume lists the previous ones so you can pick it up.

2. Give it just enough, not everything

When you ask for something, tell it where to look. Compare these two requests:

  • “Figure out why the discount calculation fails.”
  • “The test calculates_volume_discount fails with this error: expected 90, received 100. The function is in src/pricing.ts.”

With the first, the agent will explore: open folders, read unrelated files, run things to get its bearings. All of that ends up on the desk. With the second, it goes straight there. It takes less time, costs less, and leaves more room for what matters.

The same goes the other way: don’t paste the whole project “just in case.” Three right files work better than twenty that happen to include those three.

Step by step:

  1. You run the failing test yourself and copy the error.
  2. In the request, you name the file with @: typing @src/pricing.ts makes Claude Code add it to the request without searching for it.
  3. You paste the error as is, without summarizing it: The test calculates_volume_discount fails: expected 90, received 100. Look at @src/pricing.ts
  4. If it still starts opening unrelated files, you press Esc to stop it and tell it where to look.

3. Write down what’s always true, once

Some things are true in every conversation: that you use pnpm, that tests run with a certain command, that the migrations folder is off limits. If you tell it in the chat, it gets lost as the desk fills up, and you have to repeat it in the next conversation.

The fix is an instructions file at the root of the project, which the agent reads at the start of every conversation. Depending on the tool it’s called AGENTS.md or CLAUDE.md, and a few lines are enough:

- We use pnpm, not npm.
- Tests run with `pnpm test`.
- Don't modify anything inside `db/migrations/`.

That way those rules are on the desk from the start, in every conversation, without you having to remember. The article on getting your repository ready has more on what’s worth putting there.

Step by step:

  1. The first time you work on the project with the agent, you run /init. It reads the repository and writes a CLAUDE.md with what it found.
  2. You open it and delete whatever can be inferred by reading the code, like the folder list. Every line is loaded into every conversation.
  3. You add what can’t be inferred: “we use pnpm,” “don’t touch db/migrations/.”
  4. You commit it, so the whole team has it.
  5. Whenever you catch yourself repeating something in the chat, you add it to the file.

4. If you’ve corrected it twice, start over

You ask for something, it gets it wrong, you correct it. It gets it wrong again, you correct it again. By now the desk holds three wrong versions and two corrections, and the model has to guess which part of all that is what counts.

Instead of a third correction, open a new conversation with a better request, one that already includes what you learned:

Add validation to the sign-up form. The email must be validated with the function that already exists in src/validation.ts, not a new one. Error messages go under each field, not in a banner at the top.

Those two clarifications are what cost you two rounds of corrections before. Written in from the start, the first answer usually comes out right.

Step by step:

  1. At the second correction that doesn’t work, you press Esc and stop correcting.
  2. If it touched files you didn’t want changed, you press Esc twice (or type /rewind), pick the message where the problem started, and restore both the code and the conversation.
  3. You write down in one line what it misunderstood.
  4. You type /clear.
  5. You make the request again with that clarification included, like the example above.

If your tool lets you see the desk

Many agents show how much context you’ve used. In Claude Code, for example, the /context command breaks it down by part, and there are commands to clear or summarize the conversation without leaving. They’re covered in the article on Claude Code commands.

You don’t need to watch it all the time. It’s like your phone’s battery indicator: it tells you when it’s time to stop before things start failing.

A clean desk works better

Almost every “the agent forgot” or “it started doing random things” problem is an overflowing desk. The fix isn’t a pricier model or a longer prompt, it’s having less on top: one task per conversation, just enough for each request, fixed rules in a file, and starting over instead of correcting a third time.

Comments