I built a small Python agent that uses a local model (Gemma 4 on Ollama) to explain what is filling my Mac's disk. The most useful thing I learned had nothing to do with how clever the model is. A small local model can do useful work on private files, as long as code decides what it can reach and the OS blocks everything else.

I have spent about a decade as a sysadmin, and I am learning Python and agents by building small, useful tools, with Claude as a pair programmer. "What is eating my disk?" is a good task to automate: it is a real problem, the answer is checkable, and it touches private files. That last part forces the question every agent builder should ask early: how do I know it won't do something I didn't want?

The prompt asks. The code limits. The OS enforces.

Everything here runs locally. No API keys, no cloud, and no pip install: just Python's standard library and Ollama. That makes it easy to move to another machine, which is what I do in part 2.

The setup: one file, three tools

Disk Detective is one Python file of about 280 lines. It gives Gemma 4 three tools, and none of them can change anything on disk.

Tool What it does How
disk_overview Total, used and free space shutil.disk_usage("/")
largest_children Size of each sub-folder, biggest first runs du -d 1 -k
largest_files Biggest single files under a folder os.walk + os.lstat

My machine: an Apple Silicon Mac with 24 GB of RAM, macOS 15, Python 3.14, and gemma4 (5 GB) in Ollama.

python3 disk_detective.py # whole home folder python3 disk_detective.py ~/Desktop # one folder 

The model decides where to dig. A typical run on my home folder took 5 tool calls: overview, then ~, then ~/Library, then ~/Library/Caches, then ~/Downloads. It finished in about two minutes and wrote a Markdown report.

The loop, and the one line I had to ask about

The whole "agent" is a loop of about 25 lines. The model never runs code. It only writes text that asks for a tool, and the loop decides whether to act on it.

for step in range(1, max_steps + 1): msg = chat(model, messages, TOOL_SCHEMAS) messages.append(msg) calls = msg.get("tool_calls") or [] if not calls: break # no tool call = "I'm done" for call in calls: name = call["function"]["name"] args = call["function"].get("arguments") or {} if name not in TOOLS: # allow-list result = f"Unknown tool {name}." else: try: result = TOOLS[name](**args) except Exception as e: # errors go back to the model result = f"ERROR: {e}" messages.append({"role": "tool", "tool_name": name, "content": result}) else: log("Step budget used.") 

The line that stopped me was the first one: why range(1, max_steps + 1)? I asked Claude. range excludes its end, so with max_steps = 10 this counts 1 to 10. It is a ceiling, not a target: Gemma stopped after 5 of 10 steps on its own. And the else on the for loop only runs if the loop never hit break, which means the budget ran out.

The model was good at picking folders and less good at judging them. It labelled ~/.vscode as SAFE to clear, even though it holds my installed extensions. That is why none of the tools can delete anything.

The real lesson: proving it can only read

My system prompt says "You can only LOOK; you cannot delete anything." That is a request, not a guarantee. So I checked at three levels, weakest to strongest.

1. Read the code: the model can only reach three functions

The only place model output becomes an action is TOOLS[name](**args). Nothing passes model text to eval, exec or a shell. So "is the agent read-only?" becomes "are these three functions read-only?":

  • shutil.disk_usage asks the OS for space figures.
  • du only reads sizes, and it is called with a list of arguments and no shell=True. If the model sent a path like "; rm -rf ~", it would reach du as an odd folder name, not a command.
  • os.walk lists folders and os.lstat reads file metadata. Neither opens a file.

Every path the model supplies also goes through safe_path(). It calls .resolve() before checking that the path is inside my home folder, so ~/../../etc and symlinks pointing outside are refused. Check first and resolve second, and both get through.

There is exactly one write in the whole file: saving the report, to a filename I choose with --out. The model never decides it.

2. Search the code: let grep do the audit

grep -nE "open\(|write|remove|unlink|rmtree|rename|mkdir|chmod|shell=True|eval\(|exec\(|subprocess|os\.system" disk_detective.py 

This should print exactly three hits: the subprocess import, the du call, and write_text for the report. A fourth hit means something changed. It takes a second to rerun after every edit.

3. Make the OS enforce it: macOS sandbox-exec

Reading code proves what the code says. A sandbox makes it impossible to do anything else, whatever bug I add later. I have used Macs for years and had no idea this was built in. macOS ships sandbox-exec, which runs a program under a Seatbelt profile. Seatbelt is the same mechanism coding agents such as OpenAI's Codex CLI use to sandbox commands on macOS.

My whole profile, readonly.sb, is four lines:

(version 1) (allow default) (deny file-write* (subpath (param "HOME"))) (allow file-write* (literal (param "OUT"))) 

It blocks every write in my home folder except the one report file. Running the agent inside it:

OUT="$PWD/report.md" sandbox-exec -D HOME="$HOME" -D OUT="$OUT" -f readonly.sb python3 disk_detective.py --out "$OUT" 

I tested it both ways. touch ~/Desktop/should-fail.txt inside the sandbox failed with "Operation not permitted", and the file was never created. The agent itself ran normally inside the sandbox and wrote its report. Ollama runs as its own process outside the sandbox, so the model is unaffected.

Apple marks sandbox-exec as deprecated, but it still works on current macOS (I have since tested it on macOS 26 too). On Linux, the equivalent is a read-only bind mount or bwrap.

Layer What it does Can you rely on it?
Prompt Asks the model to only look No: it's a request
Tools Only read-only functions exist Yes, if you audit them (grep)
OS sandbox Blocks writes whatever the code does Yes, it's the backstop

Coming in part 2

Once Disk Detective worked, I stripped it down to a reusable template. In part 2, I take that template to a second Mac with a different model and point it at the internet: an agent that reads IT news feeds. Reading text written by strangers is a different threat, and the sandbox has to do a different job.

If you build agents, try the grep check and the four-line sandbox profile on your own tools. Seeing "Operation not permitted" on a write you know your code would never attempt is reassuring in a way no system prompt is. And if you've locked down an agent a different way on macOS or Linux, I'd like to see your profile in the comments.

Built on a Mac with Ollama and Gemma 4; I worked through it with Claude as a pair programmer.