We run a Laravel email platform that calls a self-hosted Ollama model. Two problems showed up that look unrelated and share one root: the settings we thought we had set were not the ones in force. This page covers both, with the code we ended up with. It does not include speed or quality benchmarks, because we have not re-measured them for this article.
Problem 1: requests died at 60 seconds
Our HTTP client was the one that ships with the AI package we use (NeuronAI). Its default timeout is 60 seconds. A model on modest hardware regularly needs longer than that for a long reply, so requests failed even though the Ollama server was happy to keep generating, and even though we had a timeout value in our config. The config was simply not being passed to the client.
The fix is to construct the client yourself with an explicit timeout and connect timeout from config:
class OllamaProvider extends Ollama
{
public function __construct(string $url, string $model, array $parameters = [], ?HttpClientInterface $httpClient = null)
{
parent::__construct(
url: $url,
model: $model,
parameters: self::normalizeParameters($parameters, (int) config('services.ollama.num_predict', 2048)),
httpClient: $httpClient ?? new GuzzleHttpClient(
timeout: (float) config('services.ollama.timeout', 180),
connectTimeout: (float) config('services.ollama.connect_timeout', 10),
),
);
}
}
And the config, with two separate bounds:
'ollama' => [
'host' => env('OLLAMA_URL', env('OLLAMA_HOST', 'https://example.com')),
// Total time to wait for a model response.
'timeout' => env('OLLAMA_TIMEOUT', 180),
// Fail fast when the host is unreachable, so a dead host errors in seconds.
'connect_timeout' => env('OLLAMA_CONNECT_TIMEOUT', 10),
'num_predict' => (int) env('OLLAMA_NUM_PREDICT', 2048),
],
Keep the two timeouts separate. The total timeout is long because generation is slow. The connect timeout is short so that an unreachable server fails right away instead of hanging for three minutes. We also keep a legacy variable name as a fallback (OLLAMA_URL, then OLLAMA_HOST) so an older environment does not silently point at a default host.
Problem 2: our token limits did nothing
Several agents set num_predict to cap output length. The caps were dead. The Ollama API only reads model parameters from a nested options object. A top-level num_predict key is silently ignored. So the cap you think you set is not there, and a generation can run until the model decides to stop.
We fixed it in one place, by moving known parameters into options before the request is built, and by adding a default cap if none is set:
foreach (self::OPTION_KEYS as $key) {
if (array_key_exists($key, $parameters)) {
// A value already nested in options wins over the top-level one.
if (! array_key_exists($key, $options)) {
$options[$key] = $parameters[$key];
}
unset($parameters[$key]);
}
}
if (! array_key_exists('num_predict', $options)) {
$options['num_predict'] = $defaultNumPredict;
}
$parameters['options'] = $options;
The key list covers the parameters Ollama reads from options, such as temperature, top_p, top_k, num_predict, num_ctx, seed and stop.
Problem 3: a capped reply looks like a normal reply
When a generation hits num_predict, Ollama returns a partial answer with done_reason set to length. If you pass that on, you get half an email or half a JSON object. We reject it:
if (($response['done_reason'] ?? null) === 'length') {
throw new OllamaOutputTruncatedException(
'Ollama generation hit the num_predict cap; refusing truncated output for model '.$this->model
);
}
We also log the done_reason and token counts for each response, and never the content, so a downstream failure can be matched to what the server actually did.
What the timeout does on the server
With an explicit client timeout, a timed-out request makes the client disconnect, and the server cancels the generation and frees its slot. We checked this on a deployed container with a deliberately short test timeout on a non-streaming chat request, and the server log showed the cancel. The cap from problem 2 is the backstop for the case where the connection dies without a clean disconnect: the orphaned generation still stops at the limit.
Per-agent output caps
We size each agent’s cap from the time budget, not from habit. If the HTTP timeout is 180 seconds and your model produces a known number of tokens per second on your hardware, then a cap far above timeout times throughput can never be reached. The wall-clock timeout owns that bound. Measure throughput on your own machine and write the reasoning next to the setting.
One more thing: env changes and long-running workers
After any change to these settings, clear the config cache and restart the long-lived processes (queue workers, Octane or RoadRunner). They keep the old environment in memory, so a change can look like it did nothing.
Checklist
- Pass the timeout to the HTTP client yourself. Check the package’s default.
- Use a short connect timeout and a long total timeout.
- Put
num_predictand other model parameters insideoptions. - Reject
done_reasonoflengthunless partial output is acceptable. - Log reasons and counts, not content.
- Restart workers after config changes.