Skip to content

Ollama: "the input length exceeds the context length" when embedding

AItested on Ubuntu 24.04, Ollama 0.40.19, nomic-embed-text, bge-m3

A long document is embedded, the response is 200 – and search still only finds content from the beginning. Ollama cut the text after 2048 tokens without an error. Only with truncate false does the cut become an error message.

Symptom

curl -s -w '\nHTTP %{http_code}\n' http://127.0.0.1:11434/api/embed -d @lang-anfrage-streng.json -o lang-streng.json

HTTP 400
cat lang-streng.json
{"error":"the input length exceeds the context length"}

The same request with truncate false is rejected; with bge-m3 it was HTTP 413 with the same message.

Diagnosis

Without truncate false, check how many tokens Ollama actually processed.

jq '{prompt_eval_count, total_duration}' lang.json
{
  "prompt_eval_count": 2048,
  "total_duration": 8332268529
}

Causes

2048 tokensBy default Ollama processes 2048 tokens

The text had 12,600 characters. Ollama embedded exactly 2048 tokens and dropped the rest – also with bge-m3, which according to ollama show handles 8192 tokens.

Silent cutWithout truncate false there is no error

The response is 200. If the key information were in the second half, it would simply not be in the vector.

Fix

num_ctx alone was not enough; only together with num_batch did it return 200 – after 60 seconds and with 1.6 GB of memory. The real fix is short cards.
jq '. + {truncate: false, options: {num_ctx: 8192, num_batch: 8192}}' lang-bge.json > lang-bge-8kb.json
curl -s -w '\nHTTP %{http_code}\n' http://127.0.0.1:11434/api/embed -d @lang-bge-8kb.json -o lang-bge-8kb-out.json

HTTP 200
jq '{prompt_eval_count, total_duration, load_duration}' lang-bge-8kb-out.json
{
  "prompt_eval_count": 3722,
  "total_duration": 60344118441,
  "load_duration": 4709881121
}

Don't: Widen the context window instead of splitting documents into short cards.

▶
Break it yourself and find it – free
RAG infrastructure on Kubernetes hands-on, day 2: measuring embeddings – two models, dimensions, hits and texts that are too long.
Go to day 2 →

FAQ

Why does search only find content from the beginning of long documents?

By default Ollama silently cuts texts to 2048 tokens. The vector only describes the beginning; prompt_eval_count is then exactly 2048.

Does raising num_ctx help?

In the course num_ctx 8192 alone was not enough, it kept returning 413. Only together with num_batch 8192 did it return 200 – after 60 seconds instead of a few.

Related error patterns

More