Local LLMs · Systems

Your local LLM shouldn’t freeze your laptop

I set out to build an assistant, killed my laptop instead, and ended up writing a daemon that shrinks the model rather than freezing the machine.

Every number below measured on 16 GB RAM · RTX 5060 Laptop 8 GB · Qwen2.5 and Llama 3.1

The afternoon it froze

I was building something else entirely — a local assistant that remembered things between conversations. It was going well. A 7B model was loaded and answering, I had a browser open on one side and an editor on the other, and for about an hour it felt like the future.

Then I opened one more tab.

The cursor stuttered. Then it stopped. The machine went somewhere I couldn’t follow it for a full minute — no window switching, no task manager, nothing — and when it came back, Ollama had been killed and the editor had gone with it. An afternoon of work, ended by a browser tab.

I sat there long enough to get properly annoyed, and somewhere in that annoyance the actual problem arrived, which was not the one I thought I had:

I didn’t lose that work because the model was too big. I lost it because the model had no way to become smaller.

It had exactly one size. It held that size while the machine died around it.

If you run models locally, you have already made this trade

You know the ritual. You pick the largest model your machine can hold, you learn which other applications you’re not allowed to open while it runs, and sooner or later something goes wrong anyway. So you drop to a smaller model permanently — and now you spend every hour with worse answers, in order to prevent something that happens once a fortnight.

Both options are bad, and every local tool I could find offered only those two, because they all make the decision at the same moment: once, at load time, forever. Ollama and LM Studio pick a quantization when the model loads. llama-swap will swap models, but on the client’s request — never because the machine is in trouble. Nothing was watching the one thing that actually changes.

So I stopped building the assistant and started measuring what was going wrong. That is where it got strange.

Then a measurement made no sense

I loaded Llama 3.1 8B on an 8 GB card three times, changing only the context length, and recorded free VRAM after each load.

Measured — free VRAM after loading Llama 3.1 8B
ContextFree VRAM
default2,516 MiB
32,7681,018 MiB
40,9601,702 MiB

Look at the last row. I asked for more context — more KV cache, more memory — and the card handed me back 684 MiB.

That is not how memory works. I re-ran it three times assuming I had made a mistake.

The answer was in the number I wasn’t watching. System RAM had fallen to 1,509 MB. The KV cache at 40k tokens no longer fit on the card, so the runtime had quietly moved layers onto the CPU — which frees VRAM and eats RAM instead. My GPU looked healthier precisely because my machine was in more danger.

The shortage doesn’t stay put. It moves house, and nothing announces it.

Watching VRAM alone would have told me things were improving, right up until the freeze. That single measurement is why RAMP watches RAM, VRAM and disk together rather than picking one.

4k context 40k context GPU (8 GB) weights KV cache system RAM (free) GPU (8 GB) some weights more free! system RAM spilled layers Ask for more context, and the shortage moves house rather than announcing itself.
The runtime is already doing elastic memory management — badly, silently, and without telling anyone which resource is actually short.

The arithmetic behind it

None of this is mysterious once you count the KV cache, which most capacity planning quietly ignores. Every generated token stores a key and a value vector in every layer, and that store grows linearly with context length:

kv_bytes_per_token = 2 · n_layers · n_kv_heads · head_dim · bytes_per_element

For Llama 3.1 8B — 32 layers, 8 KV heads, head dimension 128, fp16:

2 · 32 · 8 · 128 · 2 = 131,072 bytes ≈ 128 KiB / token 128 KiB · 40,960 tokens = 5.0 GiB ← KV cache alone 5.0 GiB + 4.9 GiB Q4 weights = 9.9 GiB > 8.0 GiB card

The cache alone outweighs the model. The request simply did not fit, and the runtime resolved the overflow the only way it could. The surprise was never the spill — it was that the spill was silent, and that the resource I was monitoring moved in the reassuring direction while it happened.

So the model has to be able to move

If the shortage can migrate between resources, then a fixed model is the wrong shape of solution. What you want is a model that gets out of the way when the machine needs the memory, and comes back when it doesn’t.

RAMP arranges the models you already have into a ladder — largest and best at the top, smallest and fastest at the bottom — and moves between rungs based on what the machine is actually doing. It speaks the OpenAI API, so whatever you already point at Ollama can point at RAMP instead, and never find out that the model underneath it changed.

The decision rule

Two details do most of the work. The first is that a tier is only admissible if it fits in both resources at once — the lesson from the table above, written down as a rule:

fits(t) ⇔ ram(t) + M ≤ R and vram(t) + Mv ≤ V

The second is subtler, and I got it wrong at first. The budget for a swap is not the memory free right now — it is the memory free right now plus whatever the current model gives back when it unloads:

R′ = R + ram(current)

Compare against currently-free memory alone and upgrades become arithmetically impossible: the machine never has room for a bigger model while still holding the smaller one. The daemon would ratchet downward and never recover.

Then, because the controller’s own actions move the signal it reads, it needs damping — separate thresholds for dropping and climbing, an averaged signal rather than an instantaneous one, and a rate limit on upgrades:

drop when ram_free < T_down — fast, ~6 s climb when ram_free > T_up — slow, ~90 s sustained where T_up > T_down — the dead band

The asymmetry is deliberate and it is the whole safety argument: a late downgrade freezes your machine, while a late upgrade costs you nothing but a slightly worse answer for another minute. When in doubt, be slow to take memory and quick to give it back.

time free memory climb back above here step down below here no action in between 7B -> 3B -> 1.5B back to 7B
Two thresholds, not one. Dropping is quick; climbing back needs sustained headroom. A single line would make it flap up and down forever.

What it costs

Measured
ThingCost
A swap, warm1.9 s
A swap, cold (first load)16.5 s
RAMP’s own memory65 MB
RAMP’s own CPU, per hour idle~1 s

Two seconds is the number that makes the whole approach viable, and I did not expect it. I had assumed swapping was far too expensive to do casually — that assumption is probably what stops anyone building this. It is wrong because the operating system keeps recently-read weights in its page cache, so the second load of a model is nothing like the first.

A pleasing consequence falls out of that: when memory pressure is severe enough to evict the page cache, it is the upgrade that becomes slow, never the downgrade. The cost asymmetry lines up with the urgency asymmetry. The expensive direction is the one you are never in a hurry to take.

And since “the memory watchdog is the leak” is the obvious retort, RAMP reports its own footprint through ramp status. You can check that 65 MB rather than take my word for it.

Then it started fighting me

The suite stands at 166 tests. It caught none of the three defects below. Every one surfaced only under a running system, and each is a category worth naming — the last one especially, because it is the funniest and the most embarrassing.

1. It undid my own commands

I pinned a tier by hand. Two seconds later it reverted itself.

Mid-swap, the old backend is stopped before the current-tier variable is updated, so for a brief window the daemon appears crashed to itself. Crash recovery fired, helpfully restored the tier the swap was busy replacing, and clobbered the thing I had just asked for. The fix is to treat a transition in progress as a state about which no decision may be made; the general lesson is that any system observing itself has to be able to tell “broken” from “mid-change.”

Worth recording: my first regression test for this passed against the broken code, because it didn’t wait long enough for the spurious recovery to land. A regression test you have not watched fail is not evidence of anything.

2. It cleaned up only when politely asked

RAMP can take the port your model server already uses and move that server one port over, so every client you own routes through it with no reconfiguration. Restoration ran on clean exit — and only on clean exit. I killed the daemon rather than stopping it, which skipped the handler entirely, and found port 11434 answering to nothing at all. I had broken Ollama for every other tool on the machine.

The arrangement is now journalled to disk the moment it is engaged, so it survives a hard kill and can be repaired on the next run.

3. It refused to use my best model

This is the instructive one, because it produced no error, no crash and no failing test. It just quietly did the opposite of its entire purpose.

A GPU-resident model was charged for its weights twice — once against VRAM and again against system RAM — double-counting memory that exists in exactly one place. A 5 GB model was billed 6.7 GB of RAM it never touched. The top of the ladder therefore demanded 8.8 GB free on a 16 GB machine and was, in practice, unreachable.

So the daemon I had written to stop my machine’s capacity going to waste sat there declining to use the best model I owned, and reported itself perfectly healthy the whole time. I only caught it because I went looking for why the good model never seemed to load.

Over-conservative estimates are not the safe direction. They are just a different way of being wrong — and a quieter one.

Three defects, three categories, zero caught by a green test suite on my own machine. Continuous integration later caught a fourth that I had already declared absent.

What’s proven, and what isn’t

RAMP is verified end to end against Ollama: detection, relocation, the full pressure cycle, crash recovery and restoration. The llama.cpp and LM Studio paths run through the same policy and lifecycle code and are covered by unit tests including every refusal branch, but have not had an integration run against those binaries. The design anticipates that gap rather than papering over it — where RAMP cannot positively identify a runtime, or cannot reconstruct the command needed to restart it, it declines and changes nothing. Stopping a server you cannot start again is the one outcome worth engineering against absolutely.

The open measurement is swap frequency over a multi-hour workload. Per-swap cost is characterised; the rate is a function of your machine and your work, so a figure from one laptop would be worth less than the instrumentation. ramp status reports swaps per hour, cumulative time spent swapping and per-tier occupancy, which makes the claim checkable instead of asserted. Reports from other hardware are the most useful thing anyone can send me right now — particularly from llama.cpp and LM Studio users.

Running it

pip install ramp-llm ramp doctor # can this machine run it? ramp # start it

There is no configuration file. RAMP finds your models, measures your RAM and GPU and builds the ladder itself. If you want to watch it work without downloading anything, ramp demo runs the whole daemon against a mock backend, and ramp stress fills memory so you can watch the tier drop in real time.

ramp-llm on PyPI · source on GitHub — install notes, the full command reference and the design docs all live in the repository.

I still haven’t finished the assistant I was originally building. But my machine hasn’t frozen since, and I no longer pick my model based on the worst thing that might happen this afternoon — which was the actual thing I wanted all along.

RAMP is MIT-licensed and published on PyPI as ramp-llm. Source, issues and pull requests: github.com/shivapreetham/resource-aware-model-proxy. Measurements from other hardware are the most useful contribution right now.