fix(providers): read context limit from local server's meta.n_ctx - #9656
fix(providers): read context limit from local server's meta.n_ctx#9656nuthalapativarun wants to merge 1 commit into
Conversation
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>
There was a problem hiding this comment.
💡 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".
| entry | ||
| .get("meta")? | ||
| .get("n_ctx")? |
There was a problem hiding this comment.
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 👍 / 👎.
|
Gentle ping — any chance this could get a review? Happy to address any feedback. |
|
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 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. |
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_limitstays unset and falls back toDEFAULT_CONTEXT_LIMIT(128K). This causescheck_if_compaction_neededto 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_ctxfield 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 readsmeta.n_ctxfor 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_ctxandfetch_context_limit_from_api_returns_none_without_metausingwiremock, following the existing test patterns inproviders::openai::tests. Rancargo test -p goose --lib providers::openai::— all 36 tests pass.cargo clippy -p goose --lib --tests -- -D warningsreports no issues for the changed file.Related Issues
Relates to #9529