Letting Agents Download More RAM
Over the last couple of years, the way that I work with code has gone through a phase shift. At first, it was more intelligent auto-complete from Visual Studio Code. Later, I used tools like Codium to generate tests and boilerplate. Then, I was introduced to AI agents. They were the first generative AI tool that I could see radically changing how I work.
Initially, agents needed a lot of hand-holding. As models, harnesses, and our experience with them improved, many engineers (myself included) started to trust agents to work with less supervision. Less supervision means more free time. What do we do with that free time?
The things I do with the freed-up time include making coffee, reading documentation, taking a siesta, or writing. But the number one thing I do with that time is start more agents. As time went on, I have had more and more agents running at the same time. At one point, I had agents working on more than ten tickets at once, all on my laptop. However, each agent ran expensive development processes such as integration tests, compilation, Kubernetes, npm install, etc. All of these processes would take up RAM. Despite my work laptop having over 128 GiB of RAM, I ran out of it.
I told my agent just to get it done
“It’s fine,” I thought; I’ll just go let it run
It cloned the repo, started a type check
Fetched the deps (it took just one sec)
The hours passed, my meetings ended
Let’s check my agent unattended
Of course, it’s still just getting started
The process died; I’m broken-hearted
Dismayed, I simply write “continue”
And pray there’s context left within you— anonymous Cake team member
Off the laptop
This was the push I needed to move my agents somewhere bigger. I work on Cake Agents, which runs coding agents on Kubernetes in your own cloud. So, I moved my own tickets onto it. It was a chance to dogfood, and a data center has more RAM than I do.
The RAM is there. Getting it to an agent at the moment the agent needs it is the harder part.
Kubernetes wants a number
Kubernetes asks every container to declare its memory up front, as a request and a limit. But there's no good declaration for an agent. When idle, agents need almost nothing. Mid-task, an agent might need 1 GiB, or 8 GiB, or an order of magnitude more, and that changes the moment it goes from reading a file to running the test suite.
Anyone running coding agents on Kubernetes ends up choosing between three bad outcomes:
- Requests lower than real usage. The scheduler packs nodes by requests, so the node is overcommitted. When several agents burst together the node runs short of memory. The kubelet starts evicting pods, or the node goes NotReady and takes every session on it down.
- Requests high. The request is what reserves memory. Reserve 16 GiB for every agent and you pay for 16 GiB per agent while most of them wait on a model response. If you haven't heard, we are in a RAM crisis.
- Limits low. The kernel OOM-kills the agent's container partway through a build.
Any static pair of numbers lands on one of these or, on a blend of them.
Let the agent pick the number
One thing that agents are good at, especially when compared to other kinds of workloads, is adapting. An agent knows it is about to compile something. It can recognize when the commands it runs are terminated by resource contention and work out what happened. This gave us the idea to let the agent choose how much memory it needs. We settled on telling agents how much memory they have, giving them a tool to change that amount, and finally we put a safety cap on that tool to prevent abuse.
The old joke is that you can't download more RAM. But with these techniques, an agent in a pod more or less can.
Until recently, Kubernetes required restarting the pod every time requests or limits changed because a running pod's resources were immutable. In-place pod resize changed that. It has been on by default since Kubernetes 1.33 and went stable in 1.35. When you patch a running container's resources, the kubelet is able to reserve memory on the node and adjust the underlying cgroup without restarting anything.
How it works
Each Cake Agents session runs in its own pod. Each pod starts with its memory request and limit both at 1 GiB by default, which covers a typical session, and the two stay equal for the life of the pod. The agent gets two MCP tools:
session_memory_statusreports the current memory, the cap, and whether a resize is in flight.memory_limitsets a new amount, in GiB. Request and limit move together.
When the agent is about to run something heavy, or a command has just been killed, it calls memory_limit. The control plane clamps the value to the operator's cap (16 GiB by default) and patches the pod's resize subresource. It is the same call as:
kubectl patch pod session-abc123-0 --subresource resize --patch \ '{"spec":{"containers":[{"name":"data-plane","resources":{"requests":{"memory":"8Gi"},"limits":{"memory":"8Gi"}}}]}}'
The kubelet reserves the memory on the node and applies the new cgroup limit, usually within a few seconds, and the agent retries its command. The agent process, its shell, and its working tree are untouched.
When the job finishes, the agent calls memory_limit again with a lower number. If the container is still using more than the new limit, the kubelet doesn't kill anything. It marks the resize Deferred and retries until usage drops. Then the reservation goes back to the node for the next agent.
Three details matter if you are building this yourself.
- Request and limit stay equal. This is in order to prevent overcommitment, which causes instability in the node and/or other workloads. The price of this is that a raise has to fit on the node. If the node is full, the kubelet marks the resize
Deferredand retries as memory frees up. In a future release of Cake Agents, we will make it so that if increasing the requests isn't currently possible, we transfer the agent to a different node. - Deployments and StatefulSets don't resize in place. In Cake Agents, each agent has a corresponding StatefulSet. If you change the pod's template in the StatefulSet or Deployment, then Kubernetes will always recreate the pod (kubernetes/kubernetes#136697). Our workaround was to patch the pod directly.
- Older clusters still work, with a restart. When the resize subresource is missing, or the node can't fit the resize and reports it as
Infeasible, we fall back to updating the template and letting the pod restart.
Why not VPA?
Vertical Pod Autoscaler is the one of the first tools we considered when solving this problem. Two things kept us from using it.
The first thing is that it is not part of core Kubernetes. VPA requires the installation of a dedicated operator per cluster. Cake Agents is meant to be self-contained with as few dependencies on other operators as possible.
VPA is tuned for server workloads. VPA's recommender builds its numbers from days of usage history, which suits a server whose load drifts over days or weeks. Agents' needs change from one tool call to the next. The one thing VPA reacts to quickly is an OOM kill. But, we wanted agents to be able to anticipate OOMs and adjust usage before an OOM happens
Horizontal autoscaling (HPA, KEDA) doesn't apply here. Each coding agent is doing something different :).
About Cake Agents
Cake Agents is hosted in your own cloud and integrates with your git provider, project management tool, and model providers. Try it at agents.cake.ai/.
About Author
Rosetta Roberts
Rosetta Roberts is a Staff Software Engineer at Cake, specializing in AI operations, cloud infrastructure, and the deployment of AI/ML applications. Her technical experience spans Kubernetes, Amazon EKS, healthcare data pipelines, and full-stack software development. Prior to joining Cake, Rosetta held engineering roles at Impact Crew, Triplebyte, and Tone, and worked on critical infrastructure analysis at Idaho National Laboratory. She holds a bachelor's degree in Computer Science and Applied Mathematics from Idaho State University.
More articles from Rosetta Roberts