Skip to content

fix(providers): read context limit from local server's meta.n_ctx - #9656

Closed
nuthalapativarun wants to merge 1 commit into
aaif-goose:mainfrom
nuthalapativarun:fix/9529-ollama-context-limit
Closed

fix(providers): read context limit from local server's meta.n_ctx#9656
nuthalapativarun wants to merge 1 commit into
aaif-goose:mainfrom
nuthalapativarun:fix/9529-ollama-context-limit

Conversation

@nuthalapativarun

Copy link
Copy Markdown
Contributor

Summary

When pointing the OpenAI-compatible provider at a local llama.cpp or Ollama server, the model isn't in the canonical model registry, so context_limit stays unset and falls back to DEFAULT_CONTEXT_LIMIT (128K). This causes check_if_compaction_needed to compute usage against 128K while the server hard-errors at its actual (often much smaller) context window — auto-compaction never fires in time and sessions crash with a context length error.

llama.cpp and Ollama expose the real, currently-allocated context window via a non-standard meta.n_ctx field on /v1/models, which isn't part of the OpenAI spec and so was previously ignored entirely.

This adds OpenAiProvider::fetch_context_limit_from_api, which queries the models endpoint and reads meta.n_ctx for the configured model. It's only consulted when no context limit is otherwise configured (env var, config.yaml, or canonical registry), so behavior for known cloud models is unchanged.

Testing

Added fetch_context_limit_from_api_reads_meta_n_ctx and fetch_context_limit_from_api_returns_none_without_meta using wiremock, following the existing test patterns in providers::openai::tests. Ran cargo test -p goose --lib providers::openai:: — all 36 tests pass. cargo clippy -p goose --lib --tests -- -D warnings reports no issues for the changed file.

Related Issues

Relates to #9529

llama.cpp and Ollama expose the actual runtime context window via a
non-standard meta.n_ctx field on /v1/models, which isn't covered by
the canonical model registry. Without it, local/custom models fall
back to DEFAULT_CONTEXT_LIMIT (128K), so auto-compaction triggers too
late and sessions hard-error against the server's real limit.

When no context limit is otherwise configured, query the models
endpoint and use meta.n_ctx if the server provides it.

Closes aaif-goose#9529

Signed-off-by: Varun Nuthalapati <nuthalapativarun@gmail.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eca5c27362

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +649 to +651
entry
.get("meta")?
.get("n_ctx")?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Read llama.cpp runtime context from /props

For llama.cpp servers started with -c/--ctx-size, /v1/models exposes model metadata such as meta.n_ctx_train, while the actual runtime context window is reported as default_generation_settings.n_ctx from /props. In that common local-server setup this meta.n_ctx lookup returns None, so the model keeps falling back to Goose's default 128k context and can still send prompts that exceed a smaller configured context (for example 4096 or 8192).

Useful? React with 👍 / 👎.

@nuthalapativarun

Copy link
Copy Markdown
Contributor Author

Gentle ping — any chance this could get a review? Happy to address any feedback.

@DOsinga

DOsinga commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Thank you so much for this — it's a clean, well-tested fix for a real problem (and nicely targeted at the right provider path). The trouble is that several contributors converged on this exact same meta.n_ctx fix at once, so we had to pick one to land to avoid duplicate work. We're going with #9530, which was opened by the author of the originating issue (#9529) and has already been through review.

This isn't a reflection on your code — it's genuinely good, and we really appreciate you following the existing test patterns with wiremock. Please don't let this discourage you; we'd love to see more contributions from you. Closing as a duplicate of #9530.

@DOsinga DOsinga closed this Jun 16, 2026
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.

2 participants