1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenAI Reasoning Model で max_tokens を指定したら回答が空になる罠

1
Posted at

OpenAI Reasoning Model で max_tokens を指定したら回答が空になる罠

環境

  • Python: 3.11
  • openai SDK: 1.x
  • 対象モデル: GPT-5-nano 等の Reasoning Model

問題・やりたいこと

OpenAI Chat Completions API で gpt-5-nano を使ったら、output_tokens=512 トークン消費しているのに回答本文が空文字列で返ってくる。トークンはどこに消えたのか。

output_tokens=512, answer_length=0

結論(コード先行)

短い回答生成タスクで Reasoning Model を使うと思考トークンに全消費されて回答が空になる。タスクの性質に合わせて通常モデル (gpt-4.1-nano 等) を選ぶか、Reasoning Model を使うなら max_completion_tokens を十分大きく取る。

# NG: Reasoning Model に max_tokens=512 で短い回答を期待した
response = await client.chat.completions.create(
    model="gpt-5-nano",
    max_tokens=512,             # ← 思考トークンに全消費される
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "入浴介助の手順を教えて"},
    ],
)
# → output_tokens=512, answer="" になる
# OK その1: 短い回答タスクなら通常モデルを使う
response = await client.chat.completions.create(
    model="gpt-4.1-nano",       # 非 Reasoning Model
    max_tokens=512,
    temperature=0.3,            # 通常モデルでは使える
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "入浴介助の手順を教えて"},
    ],
)
# OK その2: どうしても Reasoning Model を使うなら max_completion_tokens を大きく
response = await client.chat.completions.create(
    model="gpt-5-nano",
    max_completion_tokens=2048, # 思考+回答の合計。最低でも 1000 以上を確保
    reasoning_effort="low",     # 思考トークンを節約する
    messages=[...],
)

解説 — なぜ回答が空になるか

Reasoning Model は回答を生成する前に「思考」プロセスを内部で実行する。出力トークンは以下の順に消費される。

  1. Reasoning tokens(思考トークン) — モデル内部の推論チェーン
  2. Completion tokens(回答トークン) — 実際にユーザーへ返す文字列

max_tokens=512 を指定すると、その 512 トークンがすべて Reasoning に消費され、回答用に残るトークンが 0 になる。結果として API は output_tokens=512 を計上しつつ、空文字列を返す。

さらに罠なのは以下の点。

  • max_tokens は Reasoning Model では非推奨で、正しくは max_completion_tokens を使う
  • temperature パラメータは Reasoning Model では非対応 — 指定するとエラーになる
  • reasoning_effort="low" でも、512 トークン程度では思考だけで使い切ってしまうケースが多い

注意点・ハマりどころ

  • 最新モデル ≠ 最適モデル。Reasoning Model は複雑な推論タスク向けで、「知識を引いて短く答える」用途にはオーバースペック
  • 短文 Q&A・分類・要約のような軽量タスクは通常モデル (gpt-4.1-nano gpt-4o-mini 等) のほうが速くて安い
  • Reasoning Model に切り替えるときは API パラメータの差分(temperature 非対応・max_completion_tokens 必須)を確認する
  • output_tokens > 0 なのに content が空のときは、まず Reasoning Model 仕様を疑う

計測結果(参考)

実プロジェクトで Claude Sonnet → gpt-4.1-nano に切り替えたところ、LLM 単体のレイテンシが 7.0 秒 → 3.1 秒 に短縮された。Reasoning Model にこだわらず、タスクに合った通常モデルを選ぶだけで体感速度が大きく変わる。

参考


全体像はnoteに → https://note.com/yamashita_aidev/n/n66f54389f195

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?