Dev.to AI 🤖 Ai 👁 0 📖 5 min read

Ollama returns an empty response after 60 seconds? The model spent the context thinking

We had a small script that turns meeting minutes into a JSON task list. It called qwen3.5:9b through Ollama's OpenAI-compatible endpoint (/v1/chat/completions), the same endpoint most apps and SDKs use. Every call hung f

We had a small script that turns meeting minutes into a JSON task list. It called qwen3.5:9b through Ollama's OpenAI-compatible endpoint (/v1/chat/completions), the same endpoint most apps and SDKs use. Every call hung for about a minute and then came back with nothing. Our code then failed with "the response is not valid JSON", which sent us looking at the parser first.

Ollama hadn't errored and the model hadn't crashed. It had reasoned until it ran out of room, and there was no context left for the answer.

Setup: Windows 11, 16 GB RTX 5060 Ti, Ollama 0.40.14.

The tell: usage adds up to the context

The response body had an empty content, but the usage counters were not empty at all:

completion_tokens: 3661     total_tokens: 4096

Thousands of generated tokens and an empty string. total_tokens is exactly 4,096, which is the context Ollama chose for that GPU. The server log says the same thing:

prompt eval time =   255.53 ms /   435 tokens
       eval time = 59558.53 ms /  3661 tokens
      total time = 59814.06 ms /  4096 tokens
stop processing: n_tokens = 4095, truncated = 1

Read it from top to bottom: 435 tokens of prompt (minutes plus instructions), then 3,661 tokens generated over almost 60 seconds, all of it hidden reasoning, until the window was full. truncated = 1 means generation stopped at the limit, not because the model finished.

So the pattern to look for:

  • Latency far above normal (here ~60 s).
  • content empty or blank.
  • prompt_tokens + completion_tokens equals the context size.
  • truncated = 1 at the end of the request in the log.

If you use the native API (/api/chat) with thinking on, the reasoning comes back in a separate thinking field. A full thinking and an empty content is the same diagnosis.

Why a 4k context makes this so easy to hit

Models like qwen3.5 think by default: they write a reasoning trace before the answer, and those tokens occupy context exactly like your prompt does. On a GPU with less than 24 GiB of VRAM, Ollama's default context is 4,096 (the log prints it at startup in the vram-based default context line). A 435-token prompt leaves about 3,600 tokens for thinking plus answering, and the model used all of it thinking.

How much a model reasons is hard to predict. With a much larger model on a different local server we measured 54,512 tokens of hidden reasoning for a single answer. This isn't a rare bug. Reasoning models do this unless something limits them.

Step 1: check whether your model reasons

$m = @{ model = 'qwen3.5:9b' } | ConvertTo-Json
(Invoke-RestMethod http://127.0.0.1:11434/api/show -Method Post -Body $m `
  -ContentType 'application/json').capabilities

If thinking is in the list, the model reasons. If it isn't, the switches below do nothing and your empty reply has another cause (more on that at the end).

Step 2: turn reasoning off, per API

The switch depends on how you call Ollama:

Interface Switch
/v1/chat/completions (OpenAI-compatible) "reasoning_effort": "none"
/api/chat, /api/generate (native) "think": false
CLI ollama run qwen3.5:9b --think=false
Inside an ollama run session /set nothink

We tested the first two on 0.40.14. The CLI options come from Ollama's thinking docs and announcement. gpt-oss takes levels (low, medium, high) rather than on/off, per the same docs.

Don't use --hidethinking for this. According to Ollama's announcement it only hides the trace on screen. The model still reasons and still spends context.

Here's the request that fixed our script, in PowerShell. On Windows, curl in PowerShell is a different command and handles JSON quoting badly, so we use Invoke-RestMethod:

$body = @{
  model = 'qwen3.5:9b'; temperature = 0
  reasoning_effort = 'none'
  response_format = @{ type = 'json_object' }
  messages = @(
    @{ role = 'system'; content = 'Return only JSON: {"actions":[{"action":"","owner":""}]}' },
    @{ role = 'user'; content = 'Marta will review the inventory. Lucia is preparing the draft.' }
  )
} | ConvertTo-Json -Depth 5
$r = Invoke-RestMethod http://127.0.0.1:11434/v1/chat/completions -Method Post `
  -Body ([Text.Encoding]::UTF8.GetBytes($body)) -ContentType 'application/json'
$r.choices[0].message.content
$r.usage

The same minutes that used to time out at 60 s came back in 3.6 seconds with valid JSON.

For the native API, the equivalent is:

$body = @{ model = 'qwen3.5:9b'; stream = $false; think = $false
  messages = @(@{ role = 'user'; content = 'Summarize in one sentence: the meeting is postponed to Monday.' }) } | ConvertTo-Json -Depth 5
Invoke-RestMethod -Uri http://127.0.0.1:11434/api/chat -Method Post -ContentType 'application/json' -Body $body

Step 3 (alternative): keep reasoning, give it room

For multi-step problems you may want the reasoning. In that case the prompt, the trace and the answer all have to fit. The OpenAI-compatible endpoint won't take num_ctx per request, so either raise it server-wide or bake it into a model:

FROM qwen3.5:9b
PARAMETER num_ctx 16384
ollama create qwen9b-16k -f .\Modelfile

On the native API you can pass "options": { "num_ctx": 16384 } per request, or set OLLAMA_CONTEXT_LENGTH before starting ollama serve.

Throughput barely suffers. At 32,768 of context we measured 86, 55 and 80 tokens/s for qwen3.5:4b, qwen3.5:9b and gemma4:12b. The real cost is VRAM, and wall-clock time, because every answer takes as long as the model decides to think. We didn't measure how much qwen3.5:9b reasons with that extra room, so put a timeout on the call.

Step 4: stop failing silently

The worst part of this bug was that our program saved a blank row and moved on. Make empty content fail loudly:

content = d["choices"][0]["message"].get("content") or ""
if not content.strip():
    raise ValueError("the model returned an empty response (did it run out of context while thinking?)")

While you're there: "almost JSON"

The second failure in the same script was JSON that looked right but didn't parse. When we only asked for JSON in the prompt, qwen3.5:9b occasionally wrote a number with a leading zero ("turno": 01 in our Spanish test), which isn't valid JSON. Requesting the format through the API instead of in prose fixed it:

  • /v1: response_format: { "type": "json_object" }
  • native: "format": "json" (or a full JSON schema)

With the format forced, three models across five cases and three repetitions each gave 45 valid responses out of 45. It also helped accuracy: qwen3.5:9b went from 16 to 23 of 27 criteria on one test, and from 14 to 21 of 24 on the minutes-to-tasks one.

Valid JSON isn't correct JSON, though. Even with valid output, qwen3.5:9b left owner and due-date fields blank instead of writing "PENDING" as instructed, and our validator raised 4 warnings. gemma4:12b passed the same minutes with no warnings, in 11.2 s. Keep validating fields in code.

If it's not reasoning

  • The prompt doesn't fit. An agent like OpenCode sent about 23k tokens into a 4,096 context, and Ollama truncated it without an error. See OpenCode with Ollama not working.
  • The model doesn't support think. Check /api/show. If there's no thinking capability, the switch changes nothing.
  • The model never loaded. A model larger than your VRAM can end in a 500. See Ollama error 500 "unable to load model".

Full measurements and configs: https://beautythings.net/en/guides/ollama-empty-response-thinking/?s=devto

I wrote the whole setup up as a short book: https://beautythings.net/go/ollama-en?s=devto

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.