sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] Ollama-served models are absent from _CONTEXT_WINDOW_LIMITS, so context_window_limit silently falls back to 200k (6x over for 32k models)

Open yukihiro-murai opened this issue 2 months ago • 3 comments

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

1.57.0

Language Runtime Version

3.13

Operating System

macOS 26.0 (Apple Silicon)

Installation Method

pip

Steps to Reproduce

_CONTEXT_WINDOW_LIMITS in strands/models/_defaults.py carries no Ollama-served models at all — every local id (qwen*, llama*, gemma*, mistral*, phi*) is absent from the 103-key table, so get_context_window_limit() returns None.

from strands.models._defaults import (
    get_context_window_limit, DEFAULT_CONTEXT_WINDOW_LIMIT, _CONTEXT_WINDOW_LIMITS)

print(DEFAULT_CONTEXT_WINDOW_LIMIT)                                    # 200000
print(len(_CONTEXT_WINDOW_LIMITS))                                     # 103
print(sum(1 for k in _CONTEXT_WINDOW_LIMITS if "qwen" in k.lower()))    # 0
print(sum(1 for k in _CONTEXT_WINDOW_LIMITS if "ollama" in k.lower()))  # 0

for m in ["qwen2.5:7b", "gemma4:e2b-it-qat", "kimi-k2.6:cloud",
          "llama3.3:70b", "mistral:7b"]:
    print(m, get_context_window_limit(m))
# qwen2.5:7b None
# gemma4:e2b-it-qat None
# kimi-k2.6:cloud None
# llama3.3:70b None
# mistral:7b None

Every consumer then falls back to DEFAULT_CONTEXT_WINDOW_LIMIT = 200_000, which for the common 32,768-window local models is a 6x overestimate. The real windows are available as first-party data from the Ollama daemon itself:

curl -s http://localhost:11434/api/tags | python3 -c "
import json,sys
for m in json.load(sys.stdin)['models']:
    print(m['name'], m.get('details',{}).get('context_length'))"
# qwen2.5:7b 32768
# gemma4:e2b-it-qat 131072
# kimi-k2.6:cloud 262144

Observed with strands-harness 0.1.2:

context_window_limit=<200000> | context_window_limit not set on model, using default
    for utilization estimate | set context_window_limit in your model config for accurate results
model real window get_context_window_limit() error
qwen2.5:7b 32,768 None → 200,000 6.1x too large
gemma4:e2b-it-qat 131,072 None → 200,000 1.5x too large
kimi-k2.6:cloud 262,144 None → 200,000 0.76x (too small)

Expected Behavior

get_context_window_limit("qwen2.5:7b") returns 32768, so utilization estimates and any threshold derived from them are computed against the real window. Failing that, the warning should say the lookup missed the table rather than implying the user forgot to set a config value.

Actual Behavior

Utilization is always computed against 200,000 whenever a user has not set context_window_limit explicitly. For a 32,768-window model this reads as "still plenty of room" at a point where the real window is more than half consumed:

  • Model.estimate_utilization divides by 200k. For an actual 25k-token prompt on a 32,768-window model it reports 0.125 where the true figure is 0.763.
  • Anything keyed off that ratio fires late. In strands-harness, context_manager='auto' (the default) replaces tool results over _AUTO_TRUNCATE_THRESHOLD = 1_500 tokens with a _TRUNCATE_PREVIEW_TOKENS = 750 head/tail preview — the decision to do so is driven by the same estimate, so it no longer corresponds to the actual context load.
  • Compaction is attempted only after the real window has already been exceeded.

Nothing surfaces as an error; the only signal is one logger.warning that points at a missing config value.

This appears to be the same class of defect as #4220 (table miss → silent fallback to the 200k default), here caused by the provider's models being absent from the table rather than by a prefix-strip count.

Workaround

Set it explicitly — OllamaModel accepts **model_config:

from strands.models.ollama import OllamaModel
model = OllamaModel(host="http://127.0.0.1:11434", model_id="qwen2.5:7b",
                    context_window_limit=32_768)

Proposal

Any one of:

  1. Add the common Ollama models (qwen / llama / gemma / mistral / phi families) to _CONTEXT_WINDOW_LIMITS.
  2. Have the Ollama provider resolve the window at runtime from /api/tags → details.context_length, which is first-party data as shown above.
  3. At minimum, when the value falls back to the default, state in the warning that the model was missing from the lookup table, since today's wording reads as a user configuration mistake.

yukihiro-murai avatar Sep 25 '26 23:09 yukihiro-murai

One thing worth flagging before a fix: _defaults.py leaves Ollama out of _CONTEXT_WINDOW_LIMITS on purpose. Its docstring says local and custom model ids "depend on deployment config, not a static table." The number that actually bounds an Ollama request is num_ctx, not the model family's maximum: qwen2.5 supports 32k, but Ollama only allocates whatever num_ctx is set to.

So instead of adding Ollama ids to the table, I'd propose OllamaModel.context_window_limit resolve in this order: an explicit context_window_limit in the config, then options["num_ctx"], then None, so the existing fallback warning still fires when neither is set. I'd also have that warning name num_ctx / context_window_limit for local providers.

Would that direction work? Happy to open the PR.

sidxh avatar Sep 26 '26 20:09 sidxh

Hi, I'd like to work on this if it's available.

I reproduced the table miss locally: get_context_window_limit("qwen2.5:7b") returns None.

Root cause

The root cause is one step before the table: OllamaModel.get_config() returns self.config directly and never calls resolve_config_metadata(), unlike the OpenAI, Anthropic, Bedrock, Gemini, and Mistral providers. So adding Ollama model IDs to _CONTEXT_WINDOW_LIMITS alone wouldn't be picked up.

Proposed fix

  1. In OllamaModel.get_config(), resolve context_window_limit in this order:
    • an explicitly set context_window_limit
    • options["num_ctx"], if the user set it (the window Ollama actually runs with, since options is forwarded to the Ollama request). This won't cover windows set server-side via OLLAMA_CONTEXT_LENGTH or a Modelfile, which the client can't see.
    • the built-in table via resolve_config_metadata()
  2. Update the fallback warning in Model.estimate_utilization to include the model_id and say it wasn't found in the built-in table, so it no longer reads as a user config mistake.

Both changes would include unit tests and need no network calls. Runtime lookup from the Ollama API could be a separate follow-up, since it would add a network call to a synchronous getter.

Questions

  • Does using num_ctx as a fallback fit the direction you'd like?
  • Should I also add common Ollama model IDs to _CONTEXT_WINDOW_LIMITS, or leave the table as is?

Happy to open a PR once you confirm the approach.

JukaleManmath avatar Sep 26 '26 23:09 JukaleManmath

Just saw @sidxh's comment and #4627, which cover the same approach, so I'll step back here rather than open a duplicate PR. Also, disregard my question about adding Ollama IDs to the table; I missed that the _defaults.py docstring excludes local models intentionally.

Happy to help review #4627 if useful.

JukaleManmath avatar Sep 26 '26 23:09 JukaleManmath