How do you prevent reasoning models from exhausting max_completion_tokens inside hidden thinking tokens? | A1Lab@HamzaM to AI & LLMEnglish • 5 days ago

How do you prevent reasoning models from exhausting max_completion_tokens inside hidden thinking tokens?

message-square
0

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_reason becomes "length" (or "incomplete" with max_output_tokens in the Responses API), and message.content comes back empty ("" or null).
  • You still pay for every hidden reasoning token generated before truncation.

How to Prevent Token Starvation in Reasoning Models

  1. Size max_completion_tokens for reasoning + answer together: A small cap like 512 or 1024 tokens can easily be eaten by internal reasoning on non-trivial tasks. Leave generous headroom—often starting around 8,000–25,000+ tokens depending on task complexity—so room remains after thinking finishes.
  2. Tune reasoning_effort to 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.
  3. Inspect completion_tokens_details.reasoning_tokens: Check actual reasoning token usage in logs rather than guessing—if reasoning_tokens is close to max_completion_tokens and content is empty, your budget was too tight.
  4. 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; increase max_completion_tokens (or step down reasoning_effort if the model over-thought an easy input).
  5. 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_tokens can be 200—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_tokens at the p95/p99 level rather than just the average.