Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Reserve reasoning tokens on top of the answer budget for OpenAI reasoning models

Guido Boyke6f430ce32f1a
diff --git a/app/services/lalabase/llm/open_ai_provider.rb b/app/services/lalabase/llm/open_ai_provider.rb
index d12f844..4ba4ec0 100644
--- a/app/services/lalabase/llm/open_ai_provider.rb
+++ b/app/services/lalabase/llm/open_ai_provider.rb
@@ -43,6 +43,13 @@ module Lalabase
# description generator's gpt-5-nano, migrated onto this seam in S2).
REASONING_MODEL_PATTERN = /\A(?:gpt-5|o[1-9])/i
+ # max_tokens is the budget of the ANSWER (Provider#chat contract). A
+ # reasoning model spends its reasoning tokens out of max_completion_tokens
+ # as well, so with the bare limit the reasoning can use it all and leave an
+ # empty or cut answer. The reserve goes on top; a caller that sets
+ # max_completion_tokens in provider_options still wins.
+ REASONING_RESERVE = 4_096
+
# OpenAI's finish_reason is already close to the canonical vocabulary
# (stop / length / tool_calls / content_filter). The only legacy quirk
# is `function_call` (predecessor of tool_calls) — normalise so the
@@ -272,7 +279,7 @@ module Lalabase
if reasoning_model?(model)
# gpt-5 / o-series: max_completion_tokens (not max_tokens), and temperature
# must stay at the model's default — omit it entirely.
- params[:max_completion_tokens] = max_tokens
+ params[:max_completion_tokens] = max_tokens + REASONING_RESERVE
else
params[:max_tokens] = max_tokens
params[:temperature] = temperature
diff --git a/spec/services/lalabase/llm/open_ai_provider_spec.rb b/spec/services/lalabase/llm/open_ai_provider_spec.rb
index 8933a94..e93ff93 100644
--- a/spec/services/lalabase/llm/open_ai_provider_spec.rb
+++ b/spec/services/lalabase/llm/open_ai_provider_spec.rb
@@ -127,15 +127,24 @@ RSpec.describe Lalabase::Llm::OpenAiProvider do
expect(result[:token_usage][:cost_cents]).to eq(0)
end
- it 'maps reasoning models (gpt-5) to max_completion_tokens and drops temperature' do
+ # max_tokens is the budget of the ANSWER. A reasoning model spends its
+ # reasoning tokens out of max_completion_tokens too, so the caller's answer
+ # budget gets REASONING_RESERVE on top.
+ it 'maps reasoning models (gpt-5) to max_completion_tokens plus the reasoning reserve and drops temperature' do
provider.chat(messages: messages, model: 'gpt-5-nano', max_tokens: 1000, temperature: 0.7,
provider_options: { reasoning_effort: 'minimal' })
expect(fake_client).to have_received(:chat).with(
- parameters: hash_including(model: 'gpt-5-nano', max_completion_tokens: 1000, reasoning_effort: 'minimal')
+ parameters: hash_including(model: 'gpt-5-nano', max_completion_tokens: 1000 + described_class::REASONING_RESERVE,
+ reasoning_effort: 'minimal')
)
expect(fake_client).to have_received(:chat).with(parameters: hash_excluding(:max_tokens, :temperature))
end
+ it 'lets provider_options override the reserved max_completion_tokens (raw override wins)' do
+ provider.chat(messages: messages, model: 'gpt-5', max_tokens: 1000, provider_options: { max_completion_tokens: 700 })
+ expect(fake_client).to have_received(:chat).with(parameters: hash_including(max_completion_tokens: 700))
+ end
+
it 'forwards provider_options (e.g. presence_penalty) for non-reasoning models' do
provider.chat(messages: messages, provider_options: { presence_penalty: 0.6 })
expect(fake_client).to have_received(:chat).with(
@@ -161,7 +170,8 @@ RSpec.describe Lalabase::Llm::OpenAiProvider do
provider.chat(messages: messages, model: model, max_tokens: 500, temperature: 0.3)
if kind == :reasoning
- expect(fake_client).to have_received(:chat).with(parameters: hash_including(max_completion_tokens: 500))
+ expect(fake_client).to have_received(:chat)
+ .with(parameters: hash_including(max_completion_tokens: 500 + described_class::REASONING_RESERVE))
expect(fake_client).to have_received(:chat).with(parameters: hash_excluding(:max_tokens, :temperature))
else
expect(fake_client).to have_received(:chat).with(parameters: hash_including(max_tokens: 500, temperature: 0.3))
@@ -222,6 +232,21 @@ RSpec.describe Lalabase::Llm::OpenAiProvider do
expect(result[:token_usage][:output_tokens]).to be_positive
end
+ it 'adds the reasoning reserve when streaming a reasoning model, and leaves a standard model alone' do
+ captured = []
+ allow(fake_client).to receive(:chat) do |args|
+ captured << args[:parameters]
+ nil
+ end
+
+ provider.stream_chat(messages: messages, model: 'gpt-5', max_tokens: 1500) { |_| }
+ provider.stream_chat(messages: messages, model: 'gpt-4o-mini', max_tokens: 1500) { |_| }
+
+ expect(captured.first[:max_completion_tokens]).to eq(1500 + described_class::REASONING_RESERVE)
+ expect(captured.last).to include(max_tokens: 1500)
+ expect(captured.last).not_to have_key(:max_completion_tokens)
+ end
+
it 'requests stream_options.include_usage so OpenAI emits real token counts' do
captured = nil
allow(fake_client).to receive(:chat) do |args|