Fixing Mid-Stream Cutoffs and Long-Run Freezes When Antigravity Talks to Ollama
When Antigravity connects to Ollama just fine but responses keep dying mid-stream or long refactors hang forever, the fix usually isn't at the connection layer. A focused triage guide for cutoff-class symptoms, with measured numbers, a durable hardening recipe, and a verification loop.
Your Ollama and Antigravity are talking. The handshake works, short prompts complete fine. But the moment you ask for a real refactor — the kind where you want Gemma 4 to chew on five files and output a proper diff — the stream dies halfway through. Or the UI just sits there, spinner spinning, until "Stream closed" appears with no error that actually tells you why.
I hit this wall more than once when I first wired up local Gemma 4 for my own work. What finally unblocked me was realizing that most "Ollama connection troubleshooting" guides focus on a different layer entirely. This article is the guide I wish I'd had: specifically for the case where the connection is fine but generation gets interrupted mid-flight. If you can't establish a connection at all, start with the local LLM connection error triage guide first.
As an indie developer, I keep Ollama in daily rotation because there are stretches of solo work where I want to run long, messy trial-and-error without watching a cloud API meter tick. That's exactly why mid-stream cutoffs stung: they quietly eroded the one advantage local inference was supposed to give me. So this guide goes all the way to the permanent fix, not just the first workaround.
"Can't connect" and "cut off mid-stream" are different problems
Local LLM trouble with Antigravity falls into three layers. Mixing them up leads to hours of touching the wrong knob.
Transport layer (TCP, ports, firewall): does curl http://localhost:11434/api/tags answer?
Model-load layer (VRAM, RAM): does ollama run gemma-4:8b produce tokens standalone?
Streaming layer (keeping long runs alive): does a 30-second-plus prompt from Antigravity survive to completion?
Most existing guides cover layers one and two. This one is purely about layer three. If your symptom is "the first tokens arrive but then stop" or "short prompts work, long prompts die," you're in the right place.
To make triage fast, here's the symptom-to-log map up front. Keep it in mind and the individual causes below get much easier to pin down.
Observed symptom
Log clue
Suspect cause
Slow resume or death after minutes of silence
Ollama: model stopped: timeout
Cause 1: keep-alive
Long input, response degrades partway
Ollama: context length exceeded
Cause 2: num_ctx
Dies before the first token appears
Antigravity: stream aborted
Cause 3: client timeout
Dies during a long mid-output pause
Ollama logs a clean completion
Cause 3 or 4: dropped path
Cause 1: Early unload from keep-alive timeout
The first thing that catches people is Ollama's default OLLAMA_KEEP_ALIVE of five minutes. That's the window Ollama holds a model in memory after its last use. When your Antigravity agent pauses to think for more than five minutes mid-task, the model gets evicted in the background. When the agent finally comes back with its next message, Ollama has to reload the whole thing — which alone takes long enough that many clients time out.
For long-running work, extend this explicitly. On macOS with the Ollama.app, the Advanced settings let you set environment variables through a UI, or you can use launchctl:
# macOS: pass environment variables to the Ollama service# Ollama.app users can set these in Settings → Advanced → Environment Variables# CLI approach:launchctl setenv OLLAMA_KEEP_ALIVE "30m"launchctl setenv OLLAMA_MAX_LOADED_MODELS "2"# Restart Ollama to applyosascript -e 'quit app "Ollama"'open -a Ollama# Expected: the model stays resident for 30 minutes, so Antigravity's# thinking pauses no longer trigger an unload.
On Linux under systemd, use systemctl edit ollama to add Environment=OLLAMA_KEEP_ALIVE=30m. On Windows, set the same variable through system environment variables and restart the service.
You might be tempted to use -1 (keep forever). Avoid it on a shared dev machine — something else will eventually need the VRAM and you'll be fighting the OOM killer instead of a timeout. Thirty minutes to two hours is a sensible range for most personal machines.
Why is a reload so costly? On my Apple M2 (24GB), a cold load of gemma-4:8b (Q4) took 11–14 seconds to first token on average. If the agent's thinking runs long enough to trigger an unload and then resumes, those seconds land directly on top of your wait — and combined with the client timeout in Cause 3, that's often exactly where the disconnect happens. Extending keep-alive is the first move that cuts off this secondary damage.
✦
Thank you for reading this far.
Continue Reading
What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.
WHAT YOU'LL LEARN
✦Split the problem into transport, model-load, and streaming layers so your first move targets the right knob
✦Kill the four real causes (keep-alive, num_ctx, client timeout, network path) with a hardening recipe backed by measured numbers
✦A verification loop that mechanically confirms the fix held instead of trusting a lucky short prompt
Secure payment via Stripe · Cancel anytime
✦
Unlock This Article
Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.
Cause 2: The default num_ctx is smaller than you think
The second common cause of mid-stream cutoffs is Ollama's default context length. Regardless of what the model card advertises, Ollama often defaults to just 2048 tokens. If Antigravity feeds in several source files for a refactor, you can blow through that in the prompt alone, and the response comes back truncated or goes sideways partway through.
Override it cleanly with a Modelfile:
# Create a custom Modelfile with a larger contextcat << 'MODELFILE' > Modelfile.gemma4-longFROM gemma-4:8bPARAMETER num_ctx 16384PARAMETER num_predict 4096PARAMETER temperature 0.3MODELFILE# Register it under a new nameollama create gemma-4-long -f Modelfile.gemma4-long# Verifyollama show gemma-4-long --modelfile | grep num_ctx# Expected output: PARAMETER num_ctx 16384
Point Antigravity at gemma-4-long instead of the vanilla model name. VRAM usage scales with num_ctx, so rather than maxing it blindly, pick a value that fits your machine's headroom. Here's a rough guide from running the same gemma-4:8b (Q4) on an M2 24GB, changing only num_ctx. The absolute numbers shift with hardware and quantization, but the "heavier as it climbs" gradient holds.
num_ctx
Resident VRAM/RAM
Felt speed
Good for
2048 (default)
~6.0 GB
Fast (~24 tok/s)
Single short completions
8192
~7.2 GB
Brisk (~21 tok/s)
One or two file edits
16384
~8.8 GB
Practical (~18 tok/s)
Multi-file refactors
32768
~11.5 GB
Heavy (~13 tok/s)
Large context, watch usage
In my own setup, everyday edits run at 16384, and I keep a separate 32768 model only for when I genuinely need to read a whole repo. num_predict caps tokens per response — set it generously (4K+) if you expect long code generations. Squeeze it too tight and you cap the output even when context is plentiful, which again looks like a "mid-stream cutoff."
Cause 3: Antigravity's client timeout is too tight
With Ollama fixed, if you still see mid-stream deaths, the next suspect is Antigravity's own request timeout. Gemma 4 at 27B on CPU-only can take upwards of 30 seconds just to produce the first token, and many HTTP clients give up before then.
In Antigravity's model settings, if you're using a custom provider endpoint, look for a "Request Timeout" field alongside the base URL. Pushing it to 300 seconds eliminates the "slow first token" class of failures. If your build doesn't expose it in the UI, edit the settings file directly (on macOS, ~/Library/Application Support/Antigravity/settings.json):
streamReadTimeoutMs is the one that usually matters most for mid-stream cutoffs — it's the maximum time the client will wait between tokens. If your model pauses for more than the default (often 30 seconds) during a tough generation, you get killed even though Ollama was happily still working. Two minutes is a safe floor for a loaded machine.
These two timeouts play different roles. requestTimeoutMs is your patience for the first token; streamReadTimeoutMs is your patience between tokens. Raise only the former and leave the latter at default, and you land in a half-fixed state where the run starts but dies mid-generation. Tune them as a pair.
Cause 4: Proxies, VPNs, and firewalls dropping idle streams
One underappreciated source of mid-stream deaths is the network path itself. Corporate VPNs and some security suites forcibly close TCP connections that go silent for too long. Because the connection establishes fine, you don't perceive it as "can't connect" — but after the first burst of output, a long pause triggers a middlebox to drop the socket.
The fastest way to confirm this is simple: turn off your VPN and rerun the same prompt. If it completes, you've found it. Fix it in your VPN client by raising the idle timeout or enabling TCP keep-alive. On a corporate network, your IT team may need to whitelist long-lived HTTP/1.1 connections to the Ollama host.
At home, the usual culprits are outbound firewalls like Little Snitch or LuLu. These can allow the initial connection but then trip on a different rule during a long stream. If you're running one, switch the ollama process to "allow forever" rather than "allow once."
When none of that works: collect logs that actually help
If the problem survives all four fixes above, the next step is log correlation. The fastest path is watching Ollama's server log and Antigravity's main log side by side at the same moment.
# Stream Ollama's server log on macOStail -f ~/.ollama/logs/server.log# On Linux with systemdjournalctl -u ollama -f# Antigravity's log on macOStail -f ~/Library/Logs/Antigravity/main.log# What to look for:# - Ollama log shows "model stopped: timeout" → keep-alive problem# - Ollama log shows "context length exceeded" → num_ctx problem# - Antigravity log shows "stream aborted" → client-side timeout or network
If Ollama's log shows a clean completion but Antigravity reports a cutoff, you're almost certainly looking at a client-side timeout or a dropped socket on the way back. If Ollama itself logs an error partway through generation, the fix is on the model side (context, VRAM, or model parameters). That simple dichotomy will save you from swapping settings blindly.
Hardening recipe: settings that survive a reboot
Once you've found the cause, you want to guarantee you can get back to the same stable state after a reboot, or after touching a different model. Volatile settings mean the same symptom returns days later. I pin three things so I can restore a clean environment in minutes.
First, consolidate the Modelfile into a single version-controlled file. Rather than leaning on environment variables for behavior, bake num_ctx and num_predict into the model definition itself, so naming the model is enough to get the right behavior.
# One hardened Modelfile, kept under version controlcat << 'MODELFILE' > Modelfile.gemma4-agentFROM gemma-4:8bPARAMETER num_ctx 16384PARAMETER num_predict 4096PARAMETER temperature 0.3# Favor output consistency for agent usePARAMETER repeat_penalty 1.1MODELFILEollama create gemma-4-agent -f Modelfile.gemma4-agent
Second, put keep-alive and the loaded-model cap on the service side, not in a shell profile. If they only live in your login shell, an Ollama launched from the GUI never sees them. macOS launchctl setenv, Linux systemctl edit ollama, Windows system environment variables — the point in every case is "where the service reads."
Third, keep Antigravity's settings.json under version control with your dotfiles. Leave one provider block with requestTimeoutMs and streamReadTimeoutMs spelled out, and rebuilding an environment becomes a paste-back. With this trio — Modelfile, service env vars, and settings.json — mid-stream cutoffs effectively stopped recurring for me.
The verification loop: mechanically confirm it's fixed
Stopping at "probably fixed" risks celebrating a lucky short prompt. Every time I change a setting, I deliberately re-run a heavy prompt to reproduce the original conditions — long context and long generation — so I'm actually exercising the layer that was failing.
# 1. Force long context + long output; confirm on Ollama alone first.time ollama run gemma-4-agent "Implement a TypeScript CLI from scratch for the \following requirements, add JSDoc to every function, and finish with usage \examples and tests: (paste ~300 lines of requirements here)"# Expected: runs to completion without stalling. Use time to see the split# between first-token wait and generation.# 2. If Ollama completes, send the same prompt from Antigravity. If only one# side cuts off, you've isolated the client/network path (Cause 3/4).
If Ollama alone completes but Antigravity cuts off, the model settings are innocent and the client or network is guilty. If Ollama itself breaks partway, suspect num_ctx or VRAM. This two-stage test ends the finger-pointing. Raise your bar for "fixed" to "the same heavy prompt completes three times in a row" and you won't get fooled by a coincidental success.
Once you've confirmed the fix, it's worth baking num_ctx and your keep-alive defaults into a Modelfile so the behavior survives machine reboots. The Antigravity × Ollama × LM Studio local LLM integration guide walks through the full setup from scratch with those settings included. For a broader view of local LLM troubleshooting — including the connection-layer problems this article deliberately skipped — see the local LLM setup and connection troubleshooting guide.
The one thing I'd try first, before anything else in this article: run ollama show <model-name> --modelfile on whatever model you're currently using and look at the num_ctx line. If it's 2048, half your "random" cutoff symptoms are already explained. A local setup you can push hard through long trial-and-error is a real advantage for solo development — and it only earns its keep once you can run it without bracing for the stream to die.
Share
Thank You for Reading
Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.