📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Solving Claude Code's (Short-Term) Memory Problem

Brian Casel15:28

Transcription

If you've ever watched Cloud Code forget that function that you've just built or lose track of your project structure midway through a session, then you know exactly what I'm talking about. It starts to forget important details because in a long-running session, Cloud Code will automatically compact the conversation, and then suddenly it's suggesting changes that would break everything that you've just built.

The real problem is that we're filling up the context window way faster than we should be. Every verbose instruction, every unnecessary file that's read, every back-and-forth revision, all of that adds up, and before you know it, Cloud Code is working with a fraction of the context, or the short-term memory, if you will, that it needs in order to be effective.

So, here's what I discovered. You can actually accomplish a lot more with Cloud Code by doing less. I've got a few simple adjustments that you can start making today to not only 10x or 50x the size of the context window that you're working with, but these tips will also help you streamline your entire workflow. You'll build features faster, you'll debug more efficiently, and you'll never lose momentum due to these context limits.

This is how we start to think about context engineering. It's about making this a win-win: more productive sessions and a smoother development experience when you're building with AI. So, in this video, I'll show you some of these new power moves when you're working with Cloud Code, including making the use of the new sub-agents feature, which is a total game-changer, and I'll show you some updates that I just made to Agent OS. That's the free, open-source system I built for spec-driven development with Cloud Code. It supports the new sub-agents feature, and I found a few ways for Agent OS to help you use your context window more efficiently.

As we go along here, I'll reveal a few pitfalls that I ran into in my projects, and maybe you have too, which could be secretly eating up your context window unnecessarily. Once you see how simple these fixes are, you'll be speeding past other developers. Because that's what I do here on the channel and in my free weekly email, The Builder Briefing. I help professionals like you and me upgrade our workflows when it comes to building with AI. You can get the next issue of my Builder Briefing by going to buildermethods.com. And subscribers will be the first to access my Cloud Code course coming this year.

Now, in case you're wondering what all this talk about context windows is all about, let me give you a quick refresher. Think of Cloud Code's context window as its short-term memory. Every prompt you write, every file it reads, every response it generates takes up a little bit more space in that context window. Now, this is not to be confused with Claude MD files, which I tend to think of as the long-term memory for Cloud Code, since it contains information that Claude can refer back to anytime. The context window, on the other hand, I think of as the short-term memory for Cloud Code, since that's the memory of things that we're working on right now and in the past few minutes.

The context window for Cloud Code is around 200,000 tokens. And that might seem like a lot, but you'd be surprised at how quickly it could add up. Reading a typical file in your codebase could eat up 500 or a thousand tokens, multiplied by all the files in your codebase. And then again, if it's searching and rereading files, or reading logs, or fetching documentation off the web and reading all of that, or analyzing an image, or reading a highly detailed spec, all of that fills up the context window fast.

And once that context window fills up, Cloud Code starts forgetting things. It's not that Claude stops working altogether. It tries to automatically compact the conversation, but in that process, it loses important details. So, that function that you just wrote, it forgot about that. Or that important instruction that you gave it earlier, that's gone. Now, it can find those details again, but it's like you're paying twice because the searching and rereading of the content eats up more tokens in your context window.

Now, I'm going to get into how to be a lot more efficient when it comes to managing your context window. But what do we do when we've already filled it up or we're coming close? You may know about the `/clear` command, which completely resets the context. It's a clean slate. Use `/clear` when you're done and you're ready to move on to the next task, and the previous context doesn't even matter. You know, using the `/clear` command is just like spinning up a fresh Cloud Code instance, whether you're in a new terminal or if you're spinning up multiple Cloud Codes that run at the same time. And by the way, I have another video on this channel where I show you the best workflow for multitasking with Cloud Code.

And then you have the `/compact` command, which Cloud Code will actually use on its own automatically when you've filled up your context window. And what that will do is it'll summarize everything from your current context and then start a fresh context, passing along that summary so that you can somewhat continue where you left off. Now, I say "somewhat" because, you know, a small summary of a 200,000 token conversation certainly will not capture every important detail that you want to carry forward.

So, did you know that you can proactively run the `/compact` command and give it specific instructions on exactly which pieces of information and bits of content that you want it to include in that summary to pass along as you extend out the context window? And Cloud Code conveniently shows you when your context window is running low. So, that could be a good indicator for when it might be time for you to run that custom `/compact` command and pass along your instructions to keep it going. And so instead of just typing `/compact` or letting Cloud Code run that automatically, you might try something like `/compact keep the database schema and the authentication logic`. And now Cloud Code knows exactly what to preserve when it compresses everything else.

All right, now we've got to talk about the new sub-agents feature in Cloud Code because it's a complete game-changer, both when it comes to your workflow and for massively extending your context window. Sub-agents let you create dedicated coding agents that are highly specialized in a particular area, like running tests or creating files or committing to Git. And so when you call on a sub-agent, or when you ask Cloud Code to delegate to any of your sub-agents, it passes along instructions, and then your sub-agent performs a task in its own fresh context window. Meaning, while it's performing that task, it's not eating up context in the main agent's window.

All right, let me show you an example of putting sub-agents to work when I'm using Agent OS in Cloud Code. So, as a simple example, I'm going to start creating a tic-tac-toe game. So, I'm going to fire up Claude and I'm going to run the `agent os plan product` command. And this is going to go through our product planning process. First, it'll gather some information about the product that I want to build. And usually, I give it a lot more detail than this, but this is just a simple example. Uh, we're going to build a simple tic-tac-toe game. Just creating a simple HTML vanilla JavaScript thing here.

Okay, so it just called on the first sub-agent. That's the context fetcher. And what that one does is it goes into my Agent OS documentation and it pulls out my important preferences when it comes to building uh products. It then called on my file creator sub-agent to create uh some files and folders inside my project. And so these are things that would normally eat up tokens in my main context, but as you can literally see here, you know, 3,500 tokens were taken by that sub-agent. This one is writing several files and folders right now. Um, all using its own tokens in its own context window.

Okay, so now Agent OS is going on to document the mission for this new product, following our process for that. Again, we're calling on the file creator sub-agent. You can see the previous one used, you know, over 7,000 tokens on its own. And this one is now writing a detailed mission for the product. And as it works, we are not eating up the main agent's context window. And so you can literally start to see the savings in context space. You know, 8,000 tokens, 7,500 tokens, 3,500 tokens, and on we go.

Now, we're building out the tech stack for this uh very simple game product. And all of this is being built into the product folder in my project. My other video on the channel does a full deep dive into how Agent OS works and the philosophy behind it and how to use it. So you should check that out. Now, one of the nice things about how Agent OS is designed is we're going to build out all these product planning documents like the mission statement and the, you know, the target users, the problem, the solution. We build out a whole uh roadmap. We have our whole uh tech stack documented here. And then this is a new update to Agent OS. We're now creating a mission light. So we have some of these like light versions which are easier and more context-efficient to refer back to later when we're creating feature specs and then executing the tasks for those feature specs. All this stuff is useful for us humans on on the team, as well as feeding it as training material to Agent OS when it's writing out specs.

Okay, so we've built out all the product planning stuff for this project. The next step, of course, would be to move on to writing feature specs and then actually building out those specs. And those can even be in their own context windows. But let's just see what happened here. Every time we see one of these like highlighted things here, that means that it used a sub-agent to do that task. And you can literally see the savings in uh in in context here. Again, like, you know, almost 8,000 tokens, 9K tokens, 7,500 tokens. So each of these were done in their own context window.

And so Agent OS now actually ships with its own Cloud Code sub-agents. As of today, we have four that are built in. And I'm adding a few more as we go along here. Um, so I'm hopping over to my home folder on my system here. We've got my core uh Agent OS instructions and standards. Again, I go deep into that in the Agent OS video on my channel. But in the Claude folder, we install the agents folder with these uh four sub-agents. So you can take a look here. We've got the our context fetcher. And by the way, I'm following Anthropic's instructions to to say use proactively to to do whatever it is that you do. In this case, it retrieves and extracts relevant information. So it's like reading lots of files and finding bits of information. I want that to be done by a sub-agent so that I'm not eating up that context in the main agent's conversation.

Um, file creator is for creating, you know, new files, directories, applying templates, and whatnot. Uh, Git workflow is for, you know, commit messages and creating pull requests. And then test runner is for proactively running all of our tests for specific features or running the entire test suite and then suggesting fixes back to the main agent to execute on those fixes. So, you know, we've got like workflows and stuff built into these agents. Um, you can take a look at them. Agent OS is free and open source for you to uh tweak and use and build on in your own projects.

And by the way, if you want to start to spin up your own agents for Cloud Code, it's super simple to do. All you need to do is run `agent`. And first, it'll show you the agents that you have. Again, we installed a few from Agent OS. You might not have any yet. And so you can just go here and create a new agent. Uh, you can decide if it should be specific to the current project or if you want to install it into that global agents folder on your system, as we did using uh Agent OS. And so you would just go through there and then, you know, generate with Claude. They'll give you a simple process and a simple template.

Let's go ahead and do that. Let's create an agent that's uh an expert at refactoring front-end templates. I probably want to develop something like that in the near future. Um, but let's just see what it comes up with. You know, then you can actually decide which sets of tools are going to be available to the sub-agent. You can uh even give it more granular control. In this case, uh this is just an example. We'll go with all tools. Oh, I actually, I just unselected that. So, I'm going to select that again. Then I'm going to go up to continue. We'll decide which model it uses. Got to go with Opus, right? Then you can give it give it any color that you want. Let's go with orange.

All right, then it gives us a basic template for uh the agent itself. Let's go ahead and accept that. And I'm going to hop over to my home folder, which is where uh it installed it on my system. And we're going to go into Claude under agents. And there is the front-end template refactor agent that it created. So, you know, Cloud Code itself actually generated all of this, you know, example content just just on the very short description that I gave it. Now, you know, you'll probably want to go in here and really tweak it to your exact preferences for what you want this type of agent to do. Um, but that's a look at the process for spinning up your own agents.

Here's the counterintuitive part. We're used to training our human team members with detailed documentation and context. But when it comes to training AI agents, being verbose can actually hurt performance. Every extra word in your instructions not only adds to your context window, but also introduces the opportunity for Cloud Code to get confused or go down the wrong path. So, it's better to be concise and direct, and then instruct Cloud Code to ask clarifying questions when it needs to. Think of it like this: You want Cloud Code to act like a senior developer who knows when to ask clarifying questions when it needs to, not a junior developer who needs every little detail spelled out.

This is why spec-driven development works so well. When you develop clear specs upfront and then you spend the time to review that execution plan before you build, you don't need to give Cloud Code verbose instructions when you get up to that building phase. The agent can just work from and pull from those well-crafted specs without needing to give it extra explanations. Now, in my last video on this channel, I did a deep dive into that spec-driven development approach. So, you won't want to miss that. And this week, I released an update to Agent OS where I cut almost half of the instruction lines, making them simpler and more direct. The result: Agent OS is helping me build even more ambitious projects with Cloud Code because it's that much more efficient at managing the context window.

Now, my last tip when it comes to working efficiently is I don't actually use Cloud Code for everything. I like to use Cloud Code as my workhorse for building entire features, typically from a spec. But I found that what really starts to eat up the context window in Cloud Code are those back-and-forth rounds of revisions. You know, move this function over here, or refactor that code, or go fetch some documentation from the web and fix some code based on what you learned. All that stuff adds up, and then when it comes time to build the next feature with Cloud Code, you're out of space in the context window.

That's why I'm currently using Cloud Code running inside of Cursor. I find Cursor's interface to be better designed for that rapid-fire collaborative tweaking and refining. I can make quick adjustments in Cursor's AI chat without worrying about polluting or busting my well-managed context window in Cloud Code. You know, this isn't about one versus the other. This is about two specialized tools working really well hand-in-hand: Cloud Code for creation, Cursor for collaboration and refinement.

So hopefully you can see how just a few simple adjustments can give you a massive efficiency boost when you're working with Cloud Code. Now, for a deeper dive into what Cloud Code is really all about and the movement that it's creating in our industry, I want you to see my other video where I make the case for Cloud Code. So, right after you hit subscribe on this channel, you can check out that video next, and I'll see you there. Let's keep building.