If every request from your Python script to a local Ollama server takes two extra seconds on Windows, even a tiny health check, your GPU is fine. The culprit is the word localhost.
While testing client libraries against Ollama on Windows 11, we noticed Python scripts felt sluggish on batches of small prompts even when generation speed was over 80 tokens/s. Measuring bare /api/version calls with no model loaded exposed a 2-second penalty on every single call.
What we measured: localhost vs 127.0.0.1
With the server listening on its default 127.0.0.1 address, we sent five consecutive requests to /api/version from five common clients on Windows 11 (Python requests 2.34.2, Node.js 24.19, Windows curl.exe 8.22):
| Client | http://localhost:11434 (ms per call) | http://127.0.0.1:11434 (ms per call) |
|---|---|---|
Python urllib | 2,049 / 2,048 / 2,044 / 2,031 / 2,031 | 15 / 0 / 0 / 0 / 0 |
Python requests | 2,053 / 2,032 / 2,041 / 2,030 / 2,031 | 2 / 1 / 1 / 2 / 0 |
PowerShell Invoke-RestMethod | 2,326 / 0 / 0 / 0 / 0 | 103 / 1 / 0 / 0 / 0 |
Windows curl.exe | 213 / 207 / 207 / 210 / 209 | 0.9 / 0.8 / 1.0 / 0.8 / 1.0 |
Node.js fetch | 39 / 2 / 1 / 11 / 17 | 31 / 4 / 4 / 15 / 21 |
In Python, every request to localhost paid ~2.04 seconds before touching Ollama. A script that classifies 100 rows in a CSV wastes more than three minutes waiting on dead sockets.
Why it happens
On Windows, localhost resolves to two addresses in order: ::1 (IPv6) first, and 127.0.0.1 (IPv4) second. By default, Ollama binds only to 127.0.0.1:11434.
Running curl.exe -v shows the exact sequence: the client tries [::1]:11434, waits for Windows to refuse the TCP SYN on IPv6, and only then falls back to 127.0.0.1. Python's socket connection loop pays the full ~2-second Windows refusal timeout on every new connection.
The one-line check and fix
You can reproduce it in ten seconds without loading a model:
python -c "import time,urllib.request as u;t=time.time();u.urlopen('http://localhost:11434/api/version');print(round(time.time()-t,3),'s')" python -c "import time,urllib.request as u;t=time.time();u.urlopen('http://127.0.0.1:11434/api/version');print(round(time.time()-t,3),'s')" Replace localhost with 127.0.0.1 in your scripts, .env files and editor extensions. You do not need to restart Ollama; 127.0.0.1:11434 is already where it listens.
Three other Windows client traps we hit in the same test run
Once 127.0.0.1 is set, three quieter issues still trip up Python and PowerShell scripts talking to Ollama:
1. OpenAI's Python SDK (/v1) silently ignores context size.
When you connect with OpenAI(base_url="http://127.0.0.1:11434/v1", api_key="ollama"), there is no per-request field for num_ctx (passing it via extra_body is ignored by /v1/chat/completions). Below 24 GiB of VRAM, Ollama falls back to 4,096 tokens and truncates long prompts from the front without an error. Set OLLAMA_CONTEXT_LENGTH on the server or create a zero-disk-overhead alias with a Modelfile (PARAMETER num_ctx 16384).
2. Windows PowerShell 5.1 breaks Linux curl examples and UTF-8.
Pasting curl.exe ... -d '{"model":"qwen3.5:4b","prompt":"hi"}' into Windows PowerShell 5.1 strips the inner double quotes and returns invalid character 'm' looking for beginning of object key string. Saving JSON with Out-File -Encoding utf8 writes a UTF-8 BOM and returns invalid character 'ï' looking for beginning of value. Use Invoke-RestMethod with -ContentType 'application/json; charset=utf-8' instead.
3. Two Ollama servers fighting over port 11434.
If you installed the desktop tray app and later ran ollama serve from a terminal with new environment variables, the tray app already holds port 11434. Your terminal server exits in 0.1 s with Only one usage of each socket address is normally permitted, and your script silently talks to the old tray process. Check which binary actually owns the port:
Get-NetTCPConnection -LocalPort 11434 -State Listen | ForEach-Object { Get-Process -Id $_.OwningProcess | Select-Object Id, ProcessName, Path } Full guide and test commands: https://beautythings.net/en/guides/ollama-localhost-slow-python-windows/?s=devto
All 21 chapters of Windows Ollama troubleshooting and the 11 PowerShell diagnostic scripts are in the book: https://beautythings.net/go/ollama-en?s=devto