adjustMaxTokensForThinking function
pi's adjustMaxTokensForThinking: pair the wire max_tokens with a
thinking budget that fits inside it.
The wire max_tokens is the caller's answer budget PLUS the thinking
budget, clamped to the model output ceiling — thinking fits INSIDE the
ceiling, never on top of it. baseMaxTokens == null sends the model cap
as-is (only the budget is computed); level == null budgets 0, making
the pair the plain max_tokens identity.
Implementation
({int maxTokens, int thinkingBudget}) adjustMaxTokensForThinking({
int? baseMaxTokens,
required int modelMaxTokens,
String? level,
Map<String, int>? customBudgets,
}) {
var budget = thinkingBudgetForLevel(level, customBudgets);
var maxTokens = baseMaxTokens == null
? modelMaxTokens
: min(baseMaxTokens + budget, modelMaxTokens);
if (maxTokens <= budget) {
budget = clampThinkingBudgetToAnswerRoom(budget, maxTokens);
}
return (maxTokens: maxTokens, thinkingBudget: budget);
}