Fosowl/agenticSeek · critical · Exception
Ollama connection refused at {host}. Is the server running?
Error message
Ollama connection refused at {host}. Is the server running? What it means
This error is raised by ollama_fn (sources/llm_provider.py:198) when the HTTP request to the Ollama server fails with a 'connection refused'-style error that is not a plain httpx.ConnectError (that case is handled separately at line 188). The library detects the word 'refused' in the underlying exception string and re-raises it with the target host, meaning the TCP connection reached the machine but nothing is listening on the Ollama port. This is a server-side availability problem, not a client code bug.
Source
Thrown at sources/llm_provider.py:198
model=self.model,
messages=history,
stream=True,
)
for chunk in stream:
if verbose:
print(chunk["message"]["content"], end="", flush=True)
thought += chunk["message"]["content"]
except httpx.ConnectError as e:
raise Exception(
f"\nOllama connection failed at {host}. Check if the server is running."
) from e
except Exception as e:
if hasattr(e, 'status_code') and e.status_code == 404:
animate_thinking(f"Downloading {self.model}...")
client.pull(self.model)
return self.ollama_fn(history, verbose)
if "refused" in str(e).lower():
raise Exception(
f"Ollama connection refused at {host}. Is the server running?"
) from e
raise e
return thought
def huggingface_fn(self, history, verbose=False):
"""
Use huggingface to generate text.
"""
from huggingface_hub import InferenceClient
client = InferenceClient(
api_key=self.get_api_key("huggingface")
)
completion = client.chat.completions.create(
model=self.model,
messages=history,
max_tokens=1024,View on GitHub (pinned to ae57a23577)
Solutions
- Start the Ollama server: run `ollama serve` (or start the Ollama desktop app) and verify it responds, e.g. `curl http://localhost:11434/api/tags`.
- Check the host/port in config.ini: the address must point where Ollama listens (default port 11434); set OLLAMA_HOST if you use a non-default port.
- If the app runs in Docker, use the docker-appropriate address (host.docker.internal or the host IP) instead of localhost, matching the library's internal_url handling.
- If connecting to a remote Ollama server, confirm the server is reachable from your machine and the port is open (firewall/security group rules).
- Restart the Ollama service if it crashed; check its logs for bind errors (port already in use, etc.).
Example fix
// before (config.ini) server_address = localhost:11433 // after server_address = localhost:11434 ; or start ollama with OLLAMA_HOST=0.0.0.0:11433
Defensive patterns
Strategy: validation
Validate before calling
import socket
def assert_ollama_reachable(host="localhost", port=11434, timeout=2):
try:
with socket.create_connection((host, port), timeout=timeout):
return True
except OSError as e:
raise RuntimeError(
f"Ollama not reachable at {host}:{port} ({e}). Run `ollama serve` first."
) from e
assert_ollama_reachable() # call before constructing the provider / ollama_fn Type guard
def is_connection_refused(err: BaseException) -> bool:
"""Narrow arbitrary provider exceptions down to the connection-refused case."""
return isinstance(err, Exception) and "refused" in str(err).lower() Try / catch
import time
def safe_ollama_fn(provider, history, retries=3, delay=1.0):
for attempt in range(retries):
try:
return provider.ollama_fn(history)
except Exception as e:
if "refused" in str(e).lower() and attempt < retries - 1:
time.sleep(delay * (attempt + 1)) # give the server time to start
continue
raise RuntimeError(
"Ollama server is not running. Start it with `ollama serve`."
) from e Prevention
- Run a preflight TCP check (socket.create_connection) against the Ollama host/port before making LLM calls.
- Start Ollama as a systemd/launchd service so it survives reboots instead of relying on a manual `ollama serve`.
- Keep the port consistent (default 11434) between OLLAMA_HOST and config.ini's server_address.
- In Docker, use host.docker.internal or the host IP, never localhost, to reach an Ollama server on the host.
- Health-check http://<host>:11434/api/tags at app startup and fail fast with a clear setup message.
When it happens
Trigger: client.chat(model=..., messages=..., stream=True) is called against host (e.g. http://localhost:11434) and the underlying exception message contains 'refused' — i.e. no process is bound to that port. Note it is only reachable for exceptions that are not httpx.ConnectError but still carry 'refused' (some Ollama client/httpx versions wrap connect failures differently), and not for 404 responses (model not pulled), which are auto-retried via client.pull.
Common situations: Ollama daemon not started (forgot `ollama serve`); wrong host/port in config.ini (e.g. port 11434 vs a custom OLLAMA_HOST); running inside Docker where 'localhost' points at the container instead of the host (the library has an internal_url for docker, but a misconfigured server_address still refuses); server crashed or stopped mid-session; firewall/proxy rejecting the port on a remote server.
Understand the failure class
Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.
- Connection failures: ECONNREFUSED, ECONNRESET, and friends — why connections get refused, reset, or dropped.
Related errors
- Ollama connection failed. is the server running ?
- Ollama connection failed at {host}. Check if the server is
- Cannot connect to LM Studio at {route_start} - check if serv
- Model not set
- Prompt file not found at path: {file_path}
AI-assisted analysis of Fosowl/agenticSeek@ae57a23577 (2026-08-30).
Data as JSON: /api/errors/99ce4ccdc924add0.
Report an issue: GitHub.