Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix LLM Using Only 1/6 of Context Window

· 7 min read · Home Tech

In this article
  1. Background: Why Context Windows Matter
  2. How Context Length Is Set in Inference Software
  3. Diagnosing the Real Limit
  4. Step-by-Step Fixes
  5. Common Problems and Their Causes
  6. Comparison: Different Models and Their Contexts
  7. Costs and Resource Considerations
  8. Safety and Accuracy Notes
  9. Frequently Asked Questions

If your local large language model (LLM) is only using about one-sixth of its context window, the most likely cause is that the model's configured context length (max_seq_len) is set far below the hardware's actual capability, or the inference software is applying a default limit that doesn't match the model's training. You can usually fix this by explicitly setting the context length in your inference server or client to match the model's maximum supported value, and by ensuring your hardware has enough memory. Below, we explain why this happens, how to diagnose it, and step-by-step fixes for common setups.

Background: Why Context Windows Matter

The context window is the number of tokens (roughly words or word pieces) a model can consider at once when generating text. A model with a 32K context window can process up to 32,000 tokens of input and output combined. If your software only feeds it 5,000 tokens, you're effectively using one-sixth of the window. This limitation often appears when:

  • The model card specifies a maximum context length (e.g., 32K) but the inference framework defaults to a lower value (e.g., 4K or 8K).
  • The model's architecture uses a fixed positional encoding that was trained on a shorter context, and the software clamps to that.
  • Your GPU or CPU memory is insufficient for the full context, and the software auto-reduces the limit to avoid out-of-memory errors.
  • You're using a chat template or API that truncates the input before it reaches the model.

Understanding the difference between the model's trained context and the inference context is key. Many modern models (like Llama 3, Mistral, or Qwen) support long contexts through techniques like RoPE (Rotary Position Embedding) scaling, but the software must enable that scaling.

How Context Length Is Set in Inference Software

Most local LLM tools have a setting for context length, often called n_ctx, max_seq_len, or context_size. This value tells the model how many tokens to allocate memory for. If it's set too low, the model will truncate anything beyond that. Here are common places to check:

  • llama.cpp and its server: use the -c or --ctx-size flag (e.g., -c 32768).
  • Ollama: set num_ctx in the Modelfile or via the API (e.g., /set parameter num_ctx 32768).
  • LM Studio: in the model settings, look for “Context Length” and set it to the desired value.
  • Text Generation WebUI (oobabooga): in the Parameters tab, set n_ctx.
  • vLLM: pass --max-model-len (e.g., --max-model-len 32768).

If you’re using a Python library like Transformers, you may need to pass max_length or max_new_tokens in the generation call, but that’s for the output length, not the input context. The input context is usually controlled by the tokenizer’s max_model_input_sizes or by the model config’s max_position_embeddings.

Diagnosing the Real Limit

Before changing settings, find out what your model actually supports. Check the model card on Hugging Face or the documentation. For example, Llama 3.1 8B supports 128K context, but the base Llama 3 8B supports only 8K. Mistral 7B supports 32K. Qwen 2.5 7B supports 32K, but some versions support 128K with YaRN scaling. If you’re using a fine-tuned model, the fine-tune may have reduced the context.

To see what your current software is using, you can often log the token count of the input and output. If the model truncates, you’ll see a warning or the input length will be capped. For instance, in llama.cpp, you can add --verbose to see the prompt token count.

Another diagnostic: try a long prompt and ask the model to repeat a phrase from the beginning. If it fails, the context is being cut. You can also check the memory usage; a larger context uses more memory, so if your GPU is nearly full, the software may have lowered the limit automatically.

Step-by-Step Fixes

Here’s a practical order to try:

  1. Set the context length explicitly to the model’s maximum in your inference tool. For example, in Ollama, create a Modelfile with PARAMETER num_ctx 32768 and run ollama create mymodel -f Modelfile.
  2. Check memory. If the full context causes out-of-memory, reduce it to the largest value that fits. Use nvidia-smi on Linux or Task Manager on Windows to see VRAM usage. A 7B model in 4-bit quantization with 32K context may need around 8-10 GB VRAM, but this varies.
  3. Enable context scaling if the model supports it. For RoPE models, you can often set a scaling factor (e.g., rope_scaling in Transformers) or use a tool that does it automatically (like llama.cpp’s --rope-scaling yarn).
  4. Update your software – older versions may have bugs or lack support for long contexts. Newer versions of llama.cpp, Ollama, and LM Studio have improved long-context handling.
  5. Check the tokenizer. Some tokenizers have a max length attribute. In Transformers, you can print tokenizer.model_max_length – if it’s lower than expected, you can set it to a higher value.
  6. If using an API or server, ensure the client isn’t truncating the input. Some frontends have a “max tokens” slider that applies to input as well.

Common Problems and Their Causes

  • Model outputs gibberish after a certain point – this often means you’re exceeding the trained context and the positional encoding breaks. Reduce the context to the model’s official maximum.
  • Out-of-memory errors – the context length is too high for your hardware. Lower it or use quantization (e.g., GGUF Q4_K_M) to reduce memory.
  • Software ignores the setting – some tools have a hard cap. For example, older llama.cpp versions had a default of 512 tokens. Update and set the flag correctly.
  • Slow generation – longer contexts use more memory and compute. If you don’t need the full window, keep it lower for speed.
  • Truncation without warning – some tools silently truncate. Check the logs or add a prompt length indicator.

Comparison: Different Models and Their Contexts

Here’s a quick reference for popular open models (as of 2024-2025):

  • Llama 3.1 8B: 128K context (with RoPE scaling).
  • Llama 3 8B: 8K context.
  • Mistral 7B: 32K context.
  • Qwen 2.5 7B: 32K context (some versions up to 128K).
  • Phi-3 mini: 4K context (some versions 128K).
  • Gemma 2 9B: 8K context.

Always check the specific model card – fine-tunes may have different limits. Also note that even if the model supports a long context, the quality may degrade at the very end, so you may want to keep a margin.

Costs and Resource Considerations

Using a longer context has real costs:

  • Memory: The KV cache (key-value cache) grows linearly with context length. For a 7B model, each token might use a few MB depending on quantization. A 128K context can require tens of GB of VRAM.
  • Speed: Prefilling a long prompt takes time; generation is slower because the model attends to all previous tokens.
  • Quality: Some models lose accuracy on long contexts, especially if not trained for it. You may need to test with your own documents.

If you can’t fit the full context, consider chunking your input (splitting into smaller parts) or using a retrieval system to select relevant sections.

Safety and Accuracy Notes

When you fix the context window, be aware that the model may now see more of your data. If you’re using sensitive documents, ensure your local setup is secure. Also, longer context does not guarantee better accuracy – the model might still “forget” information in the middle. Always verify critical outputs. For coding or factual tasks, test with a known answer.

Frequently Asked Questions

Why does my model only use 4K tokens when it says 32K?

Most likely the inference software has a default context length that is lower than the model’s maximum. You need to manually set it to 32K (or whatever the model supports) in the tool’s settings. Check the documentation for the exact parameter name.

Will increasing the context length cause out-of-memory errors?

Yes, it can. The KV cache uses memory proportional to context length. If you get OOM, reduce the context length or use a more quantized version of the model. For example, a 7B model in 4-bit might fit 32K on a 12GB GPU, but 128K would not.

How do I know the maximum context length of a model?

Check the model card on Hugging Face or the official documentation. Look for “context length” or “max sequence length”. You can also inspect the model config file (config.json) for max_position_embeddings.

What is RoPE scaling and do I need it?

RoPE (Rotary Position Embedding) scaling is a technique to extend a model’s context beyond its original training length. Some tools like llama.cpp support it via flags like --rope-scaling yarn. You need it only if you want to use a context longer than the model’s original limit. For most models, the original limit is enough.

Why does my model repeat itself or lose track after a long prompt?

This can happen if the context length is set too high for the model’s training – the model didn’t learn to handle that many tokens, so attention degrades. Reduce the context to the official maximum, or use a model specifically trained for long contexts.

Can I fix this in Ollama specifically?

Yes. Create a Modelfile with PARAMETER num_ctx 32768 (replace with your desired value), then run ollama create mymodel -f Modelfile. Alternatively, set it at runtime with /set parameter num_ctx 32768 in the interactive mode, but that only affects the current session.