Files
DeepTutor/tests/services/llm/test_output_limits.py
wojiecihuo 651a616b02 fix(visualize): raise the budget to 32k, cap it to the model, and retry without thinking (#3019)
* fix(book): retry an interactive page with thinking off when it runs out of room

deepseek-flash thinks by default and can spend the whole 16000-token
visualize budget on reasoning, so a book interactive block came back with
no code. When the page is empty or cut off before </html>, ask once more
with thinking turned off (thinking_off_effort_for) before failing. If that
still runs out of room, the error names the budget sent to the model and
the settings that change it.

The shipped default stays at 16000: the budget is shared by every visualize
caller, and 32768 breaks models with a lower output cap (#3018).

Refs #3011

* fix(llm): raise the visualize budget to 32k and cap each request to the model

A thinking model can spend 16k tokens before it writes any code, so the
visualize default is now 32768. Sent unchanged that would be rejected by
models whose output stops lower (gpt-4o and gpt-4o-mini at 16384) and could
overflow a small local context window, so every request now carries
min(budget, the model's known output limit, half the configured context
window). The cap lives in get_token_limit_kwargs, so it applies to every
caller; BaseAgent and the agentic loop pass the configured window.
2026-11-27 22:26:59 +08:00

64 lines
2.4 KiB
Python

"""Output-token budgets are capped to what the model can actually return.
A budget above a model's output limit is rejected with HTTP 400 (gpt-4o and
gpt-4o-mini stop at 16384), and on a local server the prompt plus the budget
must fit inside the context window. The configured budget is a ceiling: the
request carries min(budget, model limit, half the configured window).
"""
from __future__ import annotations
import pytest
from deeptutor.runtime.agentic.client import build_completion_kwargs
from deeptutor.services.llm.config import get_token_limit_kwargs
from deeptutor.services.llm.output_limits import cap_output_tokens, known_output_limit
@pytest.mark.parametrize(
("model", "limit"),
[
("gpt-4o", 16384),
("GPT-4o-mini", 16384),
("openai/gpt-4o", 16384),
("gpt-4o-2024-08-06", 16384),
("gpt-4o-2024-05-13", 4096),
("qwen-max", 8192),
("deepseek-flash", None),
("my-local-model", None),
],
)
def test_known_output_limit(model: str, limit: int | None) -> None:
assert known_output_limit(model) == limit
def test_a_budget_above_the_model_limit_is_lowered_to_it() -> None:
assert cap_output_tokens(32768, model="gpt-4o") == 16384
def test_a_budget_within_the_model_limit_is_kept() -> None:
assert cap_output_tokens(8000, model="gpt-4o") == 8000
assert cap_output_tokens(32768, model="deepseek-flash") == 32768
def test_a_configured_context_window_leaves_room_for_the_prompt() -> None:
# A 32k local server cannot take a 32k answer on top of the prompt.
assert cap_output_tokens(32768, model="my-local-model", context_window=32768) == 16384
assert cap_output_tokens(8000, model="my-local-model", context_window=32768) == 8000
assert cap_output_tokens(32768, model="gpt-4o", context_window=128000) == 16384
def test_token_limit_kwargs_carry_the_capped_value() -> None:
assert get_token_limit_kwargs("gpt-4o", 32768) == {"max_completion_tokens": 16384}
assert get_token_limit_kwargs("deepseek-flash", 32768) == {"max_tokens": 32768}
assert get_token_limit_kwargs("my-local-model", 32768, context_window=32768) == {
"max_tokens": 16384
}
def test_completion_kwargs_carry_the_capped_value() -> None:
kwargs = build_completion_kwargs(
temperature=0.2, model="my-local-model", max_tokens=32768, context_window=32768
)
assert kwargs["max_tokens"] == 16384