Skip to content

fix(server): read Ollama's reported context window - #1217

Merged
danyaberezun merged 3 commits into
JetBrains:mainfrom
markjoin123:cursor/ollama-context-length-fda6
Nov 11, 2026
Merged

danyaberezun merged 3 commits into
JetBrains:mainfrom
markjoin123:cursor/ollama-context-length-fda6

Conversation

@markjoin123

@markjoin123 markxu (markjoin123) commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A custom Ollama endpoint saved every model with a 128K context window. /v1/models has no context fields, and discovery only read a Modelfile num_ctx line from /api/show. Models that don't set one report the window on model_info["<architecture>.context_length"], so nothing was stored and pi's 128K default applied. Compaction and overflow detection then treated a much larger model as 128K.

Approach

When /api/show has no num_ctx, Ollama cloud (ollama.com) reports the window it serves on model_info["<architecture>.context_length"], so discovery now uses that value there. The architecture may be empty (nemotron-3-ultra reports .context_length). A nested key such as *.vision.context_length is not the text window.

Local and self-hosted endpoints are different: the runtime window comes from OLLAMA_CONTEXT_LENGTH, which can be smaller than the model's metadata. On any host other than ollama.com the context stays unknown unless a Modelfile num_ctx or the /v1/models listing set one, so pi keeps its default and compaction behaves as before.

A Modelfile num_ctx still wins, and a context already reported by /v1/models still wins over both. /api/version still has to succeed before /api/show is trusted; the version string isn't used to tell cloud from local, because a source build also reports 0.0.0.

Changes

  • Ollama enrichment reads the model_info context length when num_ctx is absent, only for ollama.com.
  • The agent spec records the cloud/local distinction and precedence.
  • Tests cover cloud context_length alone, a local endpoint staying unknown, num_ctx overriding it, an empty architecture, a listing length winning, and a failed /api/version.

Screenshots

Not applicable, no frontend changes.

Related issues

Fixes #1211

Checklist

  • Fast gates pass: bun run lint, bun run typecheck, bun run test (packages/server: 2293 pass, 6 skip, 0 fail)
  • E2E suite passes for app-affecting changes (bun run e2e, or bun run e2e:full when touching agent behavior)
  • Before/after screenshots are included for frontend changes, or marked not applicable
  • Relevant SPEC.md / top-level specs updated to reflect any boundary, contract, or behavior change
  • I have read the Contributing guide and agree to the Code of Conduct

Custom endpoint discovery only parsed a Modelfile num_ctx line from
/api/show. Models that omit it, including every Ollama cloud model,
report the window as model_info["<architecture>.context_length"] and
were saved with pi's 128K default.

num_ctx still wins when a Modelfile sets it, because ThinkRail does not
send a context size and that parameter is the window the server runs.
/api/version still has to succeed before /api/show is trusted; Ollama
cloud returns a version, so that check was not dropping the window.

@jetbrains-air jetbrains-air Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes — please address the blocking inline finding.

A missing Modelfile num_ctx is not the runtime window. Local Ollama
loads OLLAMA_CONTEXT_LENGTH and silently truncates past it, so saving
model_info context_length turns off compaction and skips overflow
recovery.

Use that metadata length only for Ollama cloud (host ollama.com), which
serves the full reported window. Version 0.0.0 is not the signal:
source-built local servers report it too.

@jetbrains-air jetbrains-air Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved — ready to merge.

@jetbrains-air jetbrains-air Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved — ready to merge.

@danyaberezun
danyaberezun added this pull request to the merge queue Nov 11, 2026
Merged via the queue into JetBrains:main with commit 2d0309e Nov 11, 2026
28 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

providers: custom Ollama endpoint gives every model a 128K context window (reads num_ctx, ignores model_info.context_length)

2 participants