Finding why a local Ollama RAG bot ignored its instructions: silent num_ctx truncation
- Hardware
- llama3.1:8b on Ollama, single 24 GB GPU
- Key gotcha
- A model's maximum context (128K for Llama 3.1) is not the context Ollama gives you
What they built
jidonglab runs a local RAG bot: llama3.1:8b served by Ollama on a single 24 GB GPU, a Python client calling /api/chat, and a hybrid retriever feeding the top 8 chunks into a prompt with rules to cite chunk ids and say "not in the docs" when unsure.
The bot answered easy questions fine but on anything needing real context it skipped the citation format and answered from vibes, with HTTP 200 on every call. After two evenings blaming the retriever, the author found the cause in the Ollama server log: prompts were being cut to the default context window without any error to the client.
Setup
- llama3.1:8b on Ollama, single 24 GB GPU
- Python client hitting the native /api/chat endpoint
- Hybrid retriever returning the top 8 chunks of about 350 tokens each
- Prompt template: system rules (~260 tokens), context chunks best-first (~2,800 tokens), user question (~40 tokens); about 3,100 tokens on average
How it works
- Ollama sizes the KV cache from num_ctx when it loads a model; a longer prompt is trimmed to fit and only a server-side warning ("truncating input prompt") is logged
- On the author's build the default was 2048 tokens; trimming dropped tokens from the front, so the system rules and the top-ranked chunks disappeared while the question survived
- Truncation shows in the response field prompt_eval_count, which sat at 2048 on every cut request
- The fix was a Modelfile variant (`FROM llama3.1:8b` / `PARAMETER num_ctx 8192`) plus a client check that raises an error when prompt_eval_count reaches the limit
Results & numbers
- 500-question run: 0 of 188 prompts under 2,000 tokens were cut, but all 141 between 2,000 and 3,000 and all 171 over 3,000 hit the 2048 cap (312 total)
- Going from num_ctx 2048 to 8192: grounded-answer accuracy 61% to 84%, answers with valid chunk citations 38% to 91%, truncated prompts 312 to 0
- Median latency per answer rose from 1.9 s to 3.4 s, because the model now actually reads the whole prompt
Gotchas
- A model's maximum context (128K for Llama 3.1) is not the context Ollama gives you
- The OpenAI-compatible /v1 endpoint ignored num_ctx in the request body; use a Modelfile or OLLAMA_CONTEXT_LENGTH there. A second service using the OpenAI client kept running at 2048 after the first was fixed
- Changing num_ctx between requests can force a model reload that costs seconds, so pick one value
- Cached prompt prefixes can make prompt_eval_count smaller on repeat calls; compare on cold calls
- Newer Ollama releases raised the default, so check what your version uses (`ollama ps` shows the loaded context size)
“It wasn't the model. The model never received the instructions.”— jidonglab
Do it yourself
- Print prompt_eval_count from each /api/chat response and compare it to your own token estimate
- Create a model variant with a Modelfile: `FROM llama3.1:8b` and `PARAMETER num_ctx 8192`, then `ollama create llama3.1-8k -f Modelfile`
- Or set a server-wide default with `OLLAMA_CONTEXT_LENGTH=8192 ollama serve`
- Point every client, including OpenAI-SDK ones, at the new variant
- Move the question and the top chunk near the end of the prompt
- Add a guard that raises an error when prompt_eval_count reaches num_ctx minus a few tokens
if resp["prompt_eval_count"] >= NUM_CTX - 16:
raise RuntimeError(f"Prompt likely truncated at {NUM_CTX} tokens")Similar setups
Breaking re-ranker ties with Gemma's own token probabilities on a Mac mini
A developer testing a local retrieval setup on an M4 Mac mini fixed tied re-ranker scores from gemma3:12b by reading the model's digit probabilities from Ollama, getting the right article first on 11 of 12 questions.
MapNet: permission-aware local RAG over your folders on a CPU laptop with Ollama
Gal built MapNet, an MIT research prototype that keeps a live map of folders, files and sections so a small Ollama model only reads the few passages it needs, and published where it loses to embedding everything.
Local AI: a one-click Windows app that picks a model for your PC and sets up Ollama for friends
Tired of setting up local LLMs for friends, a developer built a free MIT-licensed Windows app that detects your GPU and RAM, installs Ollama without admin rights, pulls a fitting model and opens a private chat.
Cutting a local Qwen coding agent's wait on a 16GB Mac with a memory hook
A developer running a coding agent on qwen3.5:9b through Ollama replaced its always-loaded memory index with a hook that attaches only relevant memory lines, and cut median answer time from 90.4s to 56.4s.