nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Detect the context window the inference server is actually serving instead of trusting catalogue values

Open akramcodez opened this issue 3 weeks ago • 1 comments

Description

Nanocoder never asks the inference server what context window it is actually serving. The ceiling used for the context percentage and for auto-compaction comes from resolveModelContextLimit in source/models/models-dev-client.ts: the models.dev catalogue, a hardcoded OLLAMA_MODEL_CONTEXT_LIMITS table, or the --context-max flag. There is no probing of num_ctx, Ollama /api/show or /api/ps, llama.cpp /props, LM Studio, or vLLM max_model_len.

For local servers the advertised model maximum and the value the server actually loaded are often very different. Ollama in particular serves a much smaller num_ctx by default than the model supports and silently truncates the front of the prompt when it overflows. Nanocoder believes it has room, never compacts, and the first thing the server drops is the system prompt and tool definitions, so the agent quietly stops calling tools correctly.

Use Case

A user runs a local model through Ollama, llama.cpp or vLLM. The model card says 128k, the server loaded 4k or 8k. The status line shows low context usage, compaction never triggers, and the conversation degrades with no error and no hint as to why.

Proposed Solution

Query the effective context length from the server once per provider/model and prefer it over catalogue values:

  • Ollama: POST /api/show (model parameters, num_ctx) and GET /api/ps (context_length of the loaded model).
  • llama.cpp server: GET /props (default_generation_settings.n_ctx).
  • vLLM: max_model_len from GET /v1/models.
  • LM Studio: loaded context length from its models endpoint.

Order of precedence: explicit --context-max or config value, then the server-reported value, then models.dev, then the built-in table. When the server-reported value is lower than the catalogue value, show it once (for example in /doctor and /context) so the mismatch is visible, and optionally warn that the server is truncating.

Alternatives Considered

  • Keep relying on a manual contextLimit setting. This works for users who already know the problem exists, but the failure is silent, so most users never find it.
  • Hardcode more per-model values. This cannot follow a user's own num_ctx, Modelfile or server flags.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

Related but different: earlier requests for a manually configured context size, and the open calibration idea (#1579), which measures behaviour rather than reading the server's configuration. A grep for num_ctx, api/show and api/ps finds no existing usage.

Implementation Notes (optional)

getModelContextLimit is already called asynchronously from the client constructor (updateContextSize in source/ai-sdk-client/ai-sdk-client.ts), so a server probe can slot into the same place with a short timeout and a silent fallback. Results can be cached per base URL and model for the session.

akramcodez avatar Nov 12 '26 14:11 akramcodez

I'd like to take this, starting with Ollama since it is the clearest case. resolveModelContextLimit in source/models/models-dev-client.ts currently falls back to the catalogue and the hardcoded OLLAMA_MODEL_CONTEXT_LIMITS table, so I'll add a probe that asks the server once per provider and model (Ollama /api/show for num_ctx, llama.cpp /props, vLLM max_model_len) and prefers that value, with --context-max still taking precedence and any probe failure falling back to today's behaviour. I'll add specs with mocked server responses for each shape plus the failure fallback. I'll post the probe order here before coding if you want to confirm it, as the contributing guide suggests for issues without a spec.

ahmadkhancodes avatar Nov 13 '26 06:11 ahmadkhancodes