Memory Manners


Happy Friday!

Recently I’ve been encountering a frustrating scenario where my coding agents get a bit too enthusiastic and write some integration test logic that ends up in an infinite loop, consuming all of my system resources, crashing my desktop, and ultimately freezing the machine. Ahh, technology!

I looked to my toolkit to see what the simplest way to prevent this would be, without having to overly complicate my setup. I knew systemd has a bunch of isolation utilities, so I figured it would be a good place to start. It turns out it had just the tool I needed.

Giving it a budget

You probably primarily interact with systemd (if you interact with systemd) through systemctl to start and stop your services. But it has a bunch of other utilities, amongst them is systemd-run. This lets you run an ordinary command with some of the same controls that you’d use with services.

For example:

systemd-run --user --scope \
    -p MemoryMax=12G \ 
    -p OOMPolicy=continue \
    -- claude 

This runs Claude Code in a temporary group with a 12G memory ceiling. Importantly, it isn’t allocating the 12G up front, it is just the max. If the workload can’t stay under the ceiling, the kernel can kill the proccesses to free up the memory.

systemd runs the command in a temporary group, and any processes it spins up, like the tests, also belong to the group and they all share the same resource budget.

Of course, the kernel might kill the offending test or it might kill the agent. In either case it helps safeguard against the machine blowing up which is a win in my book.

Convenience script

As with most handy commands, I’ve wrapped it into a little helper script called agent-run, so I can simply run:

agent-run claude

or

agent-run omp

I only have it limiting memory, but systemd supports more like CPU limits, max number of processes and threads, and even I/O should you desire it.

I’ve put the simple script up as a Gist if you want it.

Of course, this isn’t specific to AI agents. This same approach works for any memory-hungry commands you’d like to give a budget.

I wish I had though to do this sooner before all of the restarts…

– Logan / Terminal Velocity

Terminal Velocity

Automation should feel like cheating. Join me for fast, practical tips that turn your terminal into a superpower.

Read more from Terminal Velocity

Happy Friday! I don’t know about you, but I’m always on the lookout for more services to try out in my home lab. I regularly check out https://github.com/awesome-selfhosted/awesome-selfhosted and https://www.reddit.com/r/selfhosted/ to see what looks interesting. This past week I was wanting to host something similar to Airtable and found that there were a few decent looking options including Baserow, Grist, and NocoDB. Being a lazy man, the natural question came up: How can I try each of...

I’m not sure how long these alliterative titles are going to work. Happy Friday! If you are a regular user of git, you’ll know that the default git diff experience leaves much to be desired. It gets the job done, and it works everywhere, so I can’t really fault it. But we can do better. delta is a plug-and-play tool that instantly makes your git diffs easier to work with. It belongs to the category of tools like bat and exa that take some integral part of your workflows and just adds nice...

Happy Friday! If you’ve seen my Shell Wizardry video, you’ll know I love fzf. But in my excitement to try to use it in as many clever places as possible, I failed to notice the even more useful piece which is the shell integration. Simply add one line to the end of your shell config file and the magic begins (start a new shell to see the effects). # bash eval "$(fzf --bash)" # zsh source <(fzf --zsh) # fish fzf --fish | source Inserting files/directories into commands (ctrl+t) Whenever you’re...