fix(model_context): query Ollama /api/show for actual serving context#753
fix(model_context): query Ollama /api/show for actual serving context#753MAK-9 wants to merge 4 commits into
Conversation
Odysseus resolves context window size via /slots (llama.cpp) then
/v1/models. Ollama exposes neither with a context_length field, so the
code fell through to the hardcoded KNOWN_CONTEXT_WINDOWS map — returning
131072 for qwen3 even when Ollama was serving the model at 40960 tokens
(the value in the GGUF metadata). This caused context compaction to fire
far too late, letting Ollama silently truncate conversations.
Add a POST /api/show probe between the /slots and /v1/models attempts.
Ollama returns model_info with a {arch}.context_length field that matches
what it actually uses as the default num_ctx. The probe is local-only and
fails silently on non-Ollama servers (e.g. llama.cpp, vLLM).
Add TestQueryContextLength covering: /api/show hit, 404 fallthrough,
connection error fallthrough, remote endpoint skip, known-model fallback,
and unknown-model DEFAULT_CONTEXT fallback.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
The Ollama Please split this down to just
Once narrowed, this should be much easier to review. |
|
The Ollama |
_build_ollama_payload sends options.temperature and options.num_predict to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048 when the option is omitted, so prompts going to any Ollama backend are silently truncated there regardless of the model's actual capability. Thread the discovered context length through the three call sites (llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it is known and larger than 2048. The builder filters out the DEFAULT_CONTEXT fallback (128000) so we don't lie to Ollama about models whose window we couldn't actually discover. Sister fix to PR odysseus-dev#753 — that PR teaches the compactor the right budget, this one tells Ollama to actually use that budget on the way in.
_build_ollama_payload sends options.temperature and options.num_predict to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048 when the option is omitted, so prompts going to any Ollama backend are silently truncated there regardless of the model's actual capability. Thread the discovered context length through the three call sites (llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it is known and positive. The builder filters out the DEFAULT_CONTEXT fallback (128000) so we don't lie to Ollama about models whose window we couldn't actually discover. The issue's literal 'when > 2048' heuristic is dropped: a model with a real context smaller than 2048 would OOM if Ollama used its default, so we pass the real value regardless of size. Matches how src/context_compactor.py uses the same helper. Sister fix to PR odysseus-dev#753 — that PR teaches the compactor the right budget, this one tells Ollama to actually use that budget on the way in.
_build_ollama_payload sends options.temperature and options.num_predict to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048 when the option is omitted, so prompts going to any Ollama backend are silently truncated there regardless of the model's actual capability. Thread the discovered context length through the three call sites (llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it is known and positive. The builder filters out the DEFAULT_CONTEXT fallback (128000) so we don't lie to Ollama about models whose window we couldn't actually discover. The issue's literal 'when > 2048' heuristic is dropped: a model with a real context smaller than 2048 would OOM if Ollama used its default, so we pass the real value regardless of size. Matches how src/context_compactor.py uses the same helper. Sister fix to PR #753 — that PR teaches the compactor the right budget, this one tells Ollama to actually use that budget on the way in.
|
The Ollama |
_build_ollama_payload sends options.temperature and options.num_predict to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048 when the option is omitted, so prompts going to any Ollama backend are silently truncated there regardless of the model's actual capability. Thread the discovered context length through the three call sites (llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it is known and positive. The builder filters out the DEFAULT_CONTEXT fallback (128000) so we don't lie to Ollama about models whose window we couldn't actually discover. The issue's literal 'when > 2048' heuristic is dropped: a model with a real context smaller than 2048 would OOM if Ollama used its default, so we pass the real value regardless of size. Matches how src/context_compactor.py uses the same helper. Sister fix to PR odysseus-dev#753 — that PR teaches the compactor the right budget, this one tells Ollama to actually use that budget on the way in.
|
The following required sections are missing or incomplete. Please update the PR description to address them:
This comment is deleted automatically once all sections are complete. |
_build_ollama_payload sends options.temperature and options.num_predict to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048 when the option is omitted, so prompts going to any Ollama backend are silently truncated there regardless of the model's actual capability. Thread the discovered context length through the three call sites (llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it is known and positive. The builder filters out the DEFAULT_CONTEXT fallback (128000) so we don't lie to Ollama about models whose window we couldn't actually discover. The issue's literal 'when > 2048' heuristic is dropped: a model with a real context smaller than 2048 would OOM if Ollama used its default, so we pass the real value regardless of size. Matches how src/context_compactor.py uses the same helper. Sister fix to PR odysseus-dev#753 — that PR teaches the compactor the right budget, this one tells Ollama to actually use that budget on the way in.
_build_ollama_payload sends options.temperature and options.num_predict to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048 when the option is omitted, so prompts going to any Ollama backend are silently truncated there regardless of the model's actual capability. Thread the discovered context length through the three call sites (llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it is known and positive. The builder filters out the DEFAULT_CONTEXT fallback (128000) so we don't lie to Ollama about models whose window we couldn't actually discover. The issue's literal 'when > 2048' heuristic is dropped: a model with a real context smaller than 2048 would OOM if Ollama used its default, so we pass the real value regardless of size. Matches how src/context_compactor.py uses the same helper. Sister fix to PR odysseus-dev#753 — that PR teaches the compactor the right budget, this one tells Ollama to actually use that budget on the way in.
_build_ollama_payload sends options.temperature and options.num_predict to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048 when the option is omitted, so prompts going to any Ollama backend are silently truncated there regardless of the model's actual capability. Thread the discovered context length through the three call sites (llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it is known and positive. The builder filters out the DEFAULT_CONTEXT fallback (128000) so we don't lie to Ollama about models whose window we couldn't actually discover. The issue's literal 'when > 2048' heuristic is dropped: a model with a real context smaller than 2048 would OOM if Ollama used its default, so we pass the real value regardless of size. Matches how src/context_compactor.py uses the same helper. Sister fix to PR odysseus-dev#753 — that PR teaches the compactor the right budget, this one tells Ollama to actually use that budget on the way in.
|
Thanks for working on this. I’m closing this version because it remains conflicting and still bundles unrelated Deep Research changes after several requests to split it. The provider-metadata side is now covered by the capability work in #2737, through merged #2739 and the continuing work in #5576. However, If runtime prompt budgeting still needs a fix, it should return as a current, focused PR limited to |
Summary
POST /api/showprobe to_query_context_length()for local endpoints/slotsattempt and before/v1/modelsmodel_info.{arch}.context_length— the value Ollama uses as its defaultnum_ctxTestQueryContextLengthclass with 6 tests covering all probe pathsWhy
Ollama doesn't expose context length via
/slots(llama.cpp-only) or/v1/models(nocontext_lengthfield). The code fell through toKNOWN_CONTEXT_WINDOWS, returning131072forqwen3models even when Ollama was actually serving them at40960tokens (the GGUF metadata value). Context compaction fired far too late and Ollama silently truncated long conversations./api/showis Ollama's native introspection endpoint and returns the correct value.Test plan
python -m pytest tests/test_model_context.py -v— 28 tests passget_context_length("http://localhost:11434/v1/chat/completions", "qwen3:14b")returns40960(was131072)/api/showreturns 404 → code still resolves via/v1/modelsor known fallback🤖 Generated with Claude Code