Skip to content

Commit 31dfbeb

Browse files
nodeeeeeeclaude
andcommitted
Cap max_tokens per-model so gpt-4o translation doesn't 400
Translation passes a max_tokens budget of len(text) * 3, which for a 9k-char section comes out to ~27k tokens — above gpt-4o's 16384 max completion cap. Adds _MODEL_MAX_COMPLETION and _cap_tokens() to clamp the request, fixing the "max_tokens is too large" 400 error that crashed note_generation on the first Chinese section. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d65f3c1 commit 31dfbeb

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

note_generation.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,39 @@ def _translate(text: str, lang: str) -> str:
538538
return _call(TRANSLATE_MODEL, system, prompt, len(text) * 3)
539539

540540

541+
_MODEL_MAX_COMPLETION = {
542+
# Conservative per-model output-token caps for OpenAI models. The API
543+
# rejects requests where max_tokens exceeds these, so we clamp here.
544+
"gpt-4o": 16384,
545+
"gpt-4o-2024-08-06": 16384,
546+
"gpt-4o-2024-11-20": 16384,
547+
"gpt-4o-mini": 16384,
548+
"gpt-4.1": 32768,
549+
"gpt-4.1-mini": 32768,
550+
"gpt-4.1-nano": 32768,
551+
"gpt-5.1": 128000,
552+
"gpt-5.2": 128000,
553+
"o3": 100000,
554+
"o4-mini": 100000,
555+
}
556+
557+
558+
def _cap_tokens(model: str, max_tokens: int) -> int:
559+
"""Clamp max_tokens to the model's max completion-token limit."""
560+
cap = _MODEL_MAX_COMPLETION.get(model)
561+
if cap and max_tokens > cap:
562+
return cap
563+
return max_tokens
564+
565+
541566
def _call(model: str, system: str, user: str, max_tokens: int,
542567
_truncated: list | None = None) -> str:
543568
"""Call any supported LLM (OpenAI, Gemini, Anthropic, or Claude CLI).
544569
545570
If *_truncated* is a list, appends True/False to indicate whether the
546571
response was cut short by the token limit.
547572
"""
573+
max_tokens = _cap_tokens(model, max_tokens)
548574
# ── Claude CLI mode: call `claude -p` as subprocess ──────────────────
549575
if _provider(model) == "claude-cli":
550576
import subprocess as _sp

0 commit comments

Comments
 (0)