Why Reasoning Models Exhaust max_completion_tokens While Thinking
On reasoning models, max_completion_tokens (or max_output_tokens in the Responses API) caps all generated tokens for the turn—both hidden reasoning tokens and the visible final response.
When a prompt is ambiguous, multi-constraint, or causes the model to loop through alternative approaches, reasoning tokens can consume 100% of max_completion_tokens. When that happens:
- The model stops as soon as the token cap is reached.
finish_reasonbecomes"length"(or"incomplete"withmax_output_tokensin the Responses API), andmessage.contentcomes back empty (""ornull).- You still pay for every hidden reasoning token generated before truncation.
How to Prevent Token Starvation in Reasoning Models
- Size
max_completion_tokensfor reasoning + answer together: A small cap like512or1024tokens can easily be eaten by internal reasoning on non-trivial tasks. Leave generous headroom—often starting around8,000–25,000+tokens depending on task complexity—so room remains after thinking finishes. - Tune
reasoning_effortto match task complexity: Use"low"(or"minimal"/"none"where supported) for straightforward extraction, routing, or formatting, and reserve"medium"or"high"for hard multi-step reasoning. - Inspect
completion_tokens_details.reasoning_tokens: Check actual reasoning token usage in logs rather than guessing—ifreasoning_tokensis close tomax_completion_tokensandcontentis empty, your budget was too tight. - Detect
finish_reason === "length"and retry with a higher cap (or lower effort): Do not treat an empty output caused by token exhaustion as a malformed response to re-prompt blindly; increasemax_completion_tokens(or step downreasoning_effortif the model over-thought an easy input). - Keep prompts crisp and unambiguous: Vague edge cases or conflicting rules trigger extra self-checking loops and inflate hidden reasoning tokens.
Node.js Example: Budget Guard & Retry on Truncated Reasoning
import OpenAI from "openai";
const openai = new OpenAI();
export async function generateWithReasoningBudget({
prompt,
reasoningEffort = "low",
initialMaxCompletionTokens = 8192,
retryMaxCompletionTokens = 24576,
}) {
let maxTokens = initialMaxCompletionTokens;
let effort = reasoningEffort;
for (let attempt = 1; attempt <= 2; attempt++) {
const response = await openai.chat.completions.create({
model: "o3-mini",
reasoning_effort: effort,
max_completion_tokens: maxTokens,
messages: [{ role: "user", content: prompt }],
});
const choice = response.choices[0];
const content = choice?.message?.content ?? "";
const details = response.usage?.completion_tokens_details;
const reasoningTokens = details?.reasoning_tokens ?? 0;
// If truncated due to token cap and no visible answer was produced, retry once with more room
if (choice?.finish_reason === "length" && !content.trim() && attempt === 1) {
console.warn(
`Reasoning exhausted budget (reasoning_tokens=${reasoningTokens}, cap=${maxTokens}). Retrying with higher cap.`
);
maxTokens = retryMaxCompletionTokens;
if (effort === "high") {
effort = "medium";
}
continue;
}
return {
content,
finishReason: choice?.finish_reason ?? "unknown",
reasoningTokens,
completionTokens: response.usage?.completion_tokens ?? 0,
};
}
throw new Error("Exceeded completion token budget across retry attempts");
}
const result = await generateWithReasoningBudget({
prompt: "Design a state machine for idempotent webhook retries with dead-letter handling.",
reasoningEffort: "medium",
});
console.log(result);
Helpful Tips for Reasoning Workloads
- Separate cap sizing from desired output length: Wanting a 150-word answer does not mean
max_completion_tokenscan be200—the model may spend thousands of hidden tokens before emitting those 150 words. - Watch multi-turn context growth: In long agent or tool loops, earlier turns and accumulated context increase per-turn token pressure, so track
reasoning_tokensat the p95/p99 level rather than just the average.
You must log in or register to comment.
