Make Ollama context window configurable via num_ctx#29
Merged
Conversation
Ollama defaults to a small context window (2048 tokens), which silently truncates long prompts and image inputs. Expose it as a configurable num_ctx passed in the request options for both the generate and chat endpoints. The value can be set per-instance (OllamaModel(num_ctx=...)) or via the OLLAMA_NUM_CTX environment variable, defaulting to 8192. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ollama defaults to a small context window (2048 tokens), which silently truncates long prompts and image inputs — producing degraded or incomplete labels with no error.
This makes the context window configurable and sends it to Ollama via the request
options.num_ctxfor both the/api/generate(vision) and/api/chat(text) endpoints.Changes
config.py: addOLLAMA_NUM_CTX(env varOLLAMA_NUM_CTX, default8192).OllamaModel.__init__: accept anum_ctxargument, falling back to the config default."options": {"num_ctx": self.num_ctx}.Usage
Notes
Larger context windows increase memory use; raise gradually for large vision models to avoid slow loads / OOM. Independent of #28.
🤖 Generated with Claude Code