Repository navigation
Studio: refuse MLX prompts longer than the served context - #16821
Conversation
An MLX load only refused an over-length prompt when KV quantization set a per-request budget. Plain loads admitted any prompt: a rotating cache silently dropped its start during prefill, and an unbounded cache ran past the loaded context. Count the prompt against the served context on every single-request and batch-row path and raise ContextBudgetExceeded, matching llama.cpp loads.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9205842865
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Review and CI status for
|
| Workflow | Result |
|---|---|
| Backend CI | ✅ run |
| Core | ✅ run |
| Lint CI | ✅ run |
| MLX CI on Mac M1 | ✅ run |
| Mac Unsloth UI + API + Update + Inference CI | ✅ run |
| Startup profile | ✅ run |
| Studio VAE seam guard | ✅ run |
| Unsloth API CI | ✅ run |
| Unsloth GGUF CI | ✅ run |
| Unsloth UI CI | ✅ run |
| Wheel CI | ✅ run |
| Windows Unsloth API CI | ✅ run |
| Windows Unsloth GGUF CI | ✅ run |
| Windows Unsloth UI CI | ✅ run |
| Workflow trigger lint | ✅ run |
| studiobench CI | ✅ run |
| Kaggle T4 Unsloth GPU CI (×2) | skipped |
What changed during review: nothing; the head is unchanged. The one Codex finding (a possible one-token overcount on image-capable checkpoints served through mlx-lm) was answered and resolved without a code change: it only affects a prompt left with room for a single output token, and fixing it would change prompt counting shared with existing budget sizing, which is outside this PR.
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Review + reproduction evidencePinned review: I reproduced an MLX load pinned to a 512-token context receiving a longer prompt on the merge base
Conclusion: real bug, fixed, pending CI on the final head. |

Problem
An MLX load in Unsloth Studio did not refuse a prompt longer than its context window. On a load pinned to ~41k tokens, a 152k-token prompt came back as an empty 200 when not streamed. Streamed, the same request spent about 20 minutes in prefill while the cache dropped the earliest part of the conversation. llama.cpp loads refuse the same request up front with a context-size error (HTTP 400).
Root cause
MLXInferenceBackend._check_context_budgetruns only when_kv_context_budgetis set, and_resolve_kv_policysets that only for loads with a quantized KV cache. Every other load admitted any prompt:max_kv_sizegives a rotating cache, which silently evicts the start of the prompt during prefill;With no free room,
generation_budget_for_windowfell back to the default budget, so generation still went ahead.Change
A new
_prompt_within_windowcounts the prompt and raisesContextBudgetExceededwhen it leaves no room in the served context (prompt + 1 > window), using the same threshold and error as the existing budget check. It runs whether or notmax_tokensis set, on:_generation_limit(text, raw completion, vision, audio), and_plan_text_row.The refusal reuses the existing
ContextBudgetExceededplumbing (worker payload → orchestrator → route), so clients get the same typed context error a quantized-KV MLX load or a llama.cpp load already returns._kv_context_budgetis intentionally not reused for this: setting it also disables batching and makes every vision request prepare its media to count tokens.Tradeoffs
max_tokensthat runs past the window during decode is unchanged; this PR only covers the prompt.Validation
New tests in
studio/backend/tests/test_mlx_inference_backend.py, each parametrized over an unset and an explicitmax_tokens: one covers the text and vision single-request paths, including a cached-prefix prompt that must be counted in full; the other covers batch-row planning. Each production edit was mutated on its own and the matching test failed (6/6).python -m pytest studio/backend/tests/test_mlx_inference_backend.py studio/backend/tests/test_mlx_load_warm_and_narrow.py -q # 584 passedReal models, loaded with a 512-token context and sent a ~2000-token prompt:
mlx-community/Qwen3-4B-4bitmax_tokens, batch)mlx-community/Qwen3.5-0.8B-4bit(mlx-vlm)A short prompt still generates normally on both.