Measured latency: localhost vs 127.0.0.1 on Windows
With the server listening on 127.0.0.1 only, we sent five consecutive requests to /api/version from each client, first using http://localhost:11434 and then http://127.0.0.1:11434.
- Python urllib: 2,049, 2,048, 2,044, 2,031 and 2,031 ms with localhost vs 15, 0, 0, 0 and 0 ms with 127.0.0.1.
- Python requests: 2,053, 2,032, 2,041, 2,030 and 2,031 ms with localhost vs 2, 1, 1, 2 and 0 ms with 127.0.0.1.
- PowerShell Invoke-RestMethod: 2,326 ms on the first call (then 0 ms in the same session) vs 103, 1, 0, 0 and 0 ms with 127.0.0.1.
- Windows curl.exe: 213, 207, 207, 210 and 209 ms with localhost vs 0.9 to 1.0 ms with 127.0.0.1.
- Node.js fetch: 39, 2, 1, 11 and 17 ms with localhost vs 31, 4, 4, 15 and 21 ms with 127.0.0.1.
- Before (slow): 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')"
- After (fast): 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')"
- OpenAI Python SDK setup: client = OpenAI(base_url="http://127.0.0.1:11434/v1", api_key="ollama")
- Check who actually holds port 11434: Get-NetTCPConnection -LocalPort 11434 -State Listen | ForEach-Object { Get-Process -Id $_.OwningProcess | Select-Object Id, ProcessName, Path }
With 127.0.0.1, the Python check prints a few thousandths of a second instead of ~2.05 s. A batch script classifying 100 rows saves over three minutes of pure TCP wait.
Three other traps when calling Ollama from Python or PowerShell
Once 127.0.0.1 is set, three quieter mistakes break scripts on Windows without an obvious message.
- The /v1 endpoint cannot set context size per request. As Ollama’s OpenAI compatibility page notes, the OpenAI API has no field for num_ctx, so requests fall back to the 4,096-token default below 24 GiB VRAM and silently truncate long prompts. Raise OLLAMA_CONTEXT_LENGTH on the server or build a zero-overhead alias with a Modelfile (see Ollama context length on Windows).
- Missing api_key or /v1 in the OpenAI Python library (3.8.0): leaving out api_key raises OpenAIError: Missing credentials before reaching Ollama (pass api_key="ollama"); leaving out /v1 returns 404 page not found, while a wrong model tag returns model not found.
- Windows PowerShell 5.1 strips inner double quotes when calling curl.exe (causing invalid character m looking for beginning of object key string) and Out-File -Encoding utf8 adds a BOM (causing invalid character looking for beginning of value). Prefer Invoke-RestMethod with Content-Type application/json; charset=utf-8.
When settings appear to be ignored: two Ollama copies on port 11434
If you unpacked the portable zip after installing the desktop app, whichever copy started first owns port 11434. A second ollama serve exits in 0.1 seconds with bind: Only one usage of each socket address is normally permitted, and your script keeps talking to the first server with its old environment variables and models folder.
Use the Get-NetTCPConnection command above to inspect the exact Path of the process listening on 11434. Also check that HTTP_PROXY is not set in your client environment, which Ollama’s FAQ warns can interrupt local connections. If your model loads on CPU instead of GPU once connected, see Ollama not using the GPU on Windows and OpenCode with Ollama not working.
Frequently asked questions
Why is http://localhost:11434 slow on Windows but fast on Linux?Windows resolves localhost to IPv6 (::1) first, while Ollama binds to IPv4 (127.0.0.1) by default. Python waits ~2 seconds on the refused IPv6 socket before trying 127.0.0.1.
Does changing localhost to 127.0.0.1 require restarting Ollama?No. Ollama already listens on 127.0.0.1:11434 by default; you only change the URL in your client code or app settings.
How do I pass num_ctx when using the OpenAI Python library with Ollama?You cannot pass num_ctx through /v1/chat/completions. Set OLLAMA_CONTEXT_LENGTH before starting Ollama or create a Modelfile with PARAMETER num_ctx 16384.
Why does Ollama say Only one usage of each socket address is normally permitted?Another Ollama instance (usually the tray app) is already listening on port 11434. Quit it from the system tray before starting ollama serve.