From 2c447708e90feb1d3bb82f505d660d867e2f10b1 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 5 Oct 2026 22:56:48 +1100 Subject: [PATCH] fix(archiegateway,agentexec,agentworker): rely on ai-sdk reasoning token selection Bump github.com/samcharles93/ai-sdk v0.1.40 -> v0.1.41. ai-sdk now sends max_completion_tokens for reasoning models and omits temperature when the model rejects it, so the max_tokens workarounds are no longer needed: the chat-turn bound is set directly and MCP sampling loses its reasoning branch. Classic models keep their existing bounds. --- go.mod | 2 +- go.sum | 2 ++ internal/agentexec/modelloop/sampling.go | 9 +++--- internal/app/agentworker/mcp_sampling.go | 2 +- internal/app/archiegateway/chat_turn_model.go | 31 +++++-------------- internal/app/archiegateway/mcp_sampling.go | 4 +-- 6 files changed, 17 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index 04b6ba530..0b00af93e 100644 --- a/go.mod +++ b/go.mod @@ -112,7 +112,7 @@ require ( github.com/nats-io/nats.go v1.54.0 github.com/opencontainers/image-spec v1.1.1 github.com/pressly/goose/v3 v3.28.0 - github.com/samcharles93/ai-sdk v0.1.40 + github.com/samcharles93/ai-sdk v0.1.41 github.com/testcontainers/testcontainers-go v0.44.0 github.com/testcontainers/testcontainers-go/modules/postgres v0.44.0 go.lsp.dev/jsonrpc2 v1.0.1 diff --git a/go.sum b/go.sum index fd1feac4f..35ad79ee8 100644 --- a/go.sum +++ b/go.sum @@ -209,6 +209,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/samcharles93/ai-sdk v0.1.40 h1:nvhlUZ+pfTiSDPYO/5ett0MlcfUpg4uvBOlo7qVmngA= github.com/samcharles93/ai-sdk v0.1.40/go.mod h1:u4zrQ9/Q58L5mNEut4bNQ1ks+RCKq71LN5EDz2N/0Zk= +github.com/samcharles93/ai-sdk v0.1.41 h1:fVfl699V6H8hXHS1VYv8jnIi2tDQeqV2zVyj83foNlI= +github.com/samcharles93/ai-sdk v0.1.41/go.mod h1:u4zrQ9/Q58L5mNEut4bNQ1ks+RCKq71LN5EDz2N/0Zk= github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0= github.com/segmentio/asm v1.2.1/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= github.com/segmentio/encoding v0.5.4 h1:OW1VRern8Nw6ITAtwSZ7Idrl3MXCFwXHPgqESYfvNt0= diff --git a/internal/agentexec/modelloop/sampling.go b/internal/agentexec/modelloop/sampling.go index 2b68d24c6..409189c2c 100644 --- a/internal/agentexec/modelloop/sampling.go +++ b/internal/agentexec/modelloop/sampling.go @@ -41,8 +41,10 @@ func SamplingMessages(req protocol.SamplingRequest) ([]chat.Message, error) { } // SamplingMaxTokens returns the request's maxTokens or the default, capped at -// the model's ceiling. Reasoning models get no bound. -func SamplingMaxTokens(requested, modelCeiling int, reasoning bool) int { +// the model's ceiling. The ai-sdk runtime picks max_completion_tokens or +// max_tokens from the model's catalog metadata, so there is no reasoning +// branch here. +func SamplingMaxTokens(requested, modelCeiling int) int { maxTokens := requested if maxTokens <= 0 { maxTokens = DefaultSamplingMaxTokens @@ -50,8 +52,5 @@ func SamplingMaxTokens(requested, modelCeiling int, reasoning bool) int { if modelCeiling > 0 && maxTokens > modelCeiling { maxTokens = modelCeiling } - if reasoning { - return 0 - } return maxTokens } diff --git a/internal/app/agentworker/mcp_sampling.go b/internal/app/agentworker/mcp_sampling.go index 1e0af5d37..c4e8aa89f 100644 --- a/internal/app/agentworker/mcp_sampling.go +++ b/internal/app/agentworker/mcp_sampling.go @@ -39,7 +39,7 @@ func taskSamplingHandler(llm *runtime.Runtime, cfg config.TaskConfig) mcp.Sampli Messages: messages, System: req.SystemPrompt, MaxSteps: 1, - MaxTokens: modelloop.SamplingMaxTokens(req.MaxTokens, limits.MaxOutputTokens, limits.Reasoning), + MaxTokens: modelloop.SamplingMaxTokens(req.MaxTokens, limits.MaxOutputTokens), } if req.Temperature != nil { options.Temperature = float32(*req.Temperature) diff --git a/internal/app/archiegateway/chat_turn_model.go b/internal/app/archiegateway/chat_turn_model.go index 5aa210ef5..cc27526d7 100644 --- a/internal/app/archiegateway/chat_turn_model.go +++ b/internal/app/archiegateway/chat_turn_model.go @@ -67,7 +67,6 @@ func (m *chatTurnModel) Prepare( llm: m.llm(), model: req.Model, options: options, - reasoning: req.Reasoning, toolInfo: toolSummaries(options.Tools), toolTokens: gateway.EstimateTokens(string(toolSchema)), outcomes: m.outcomes, @@ -76,13 +75,9 @@ func (m *chatTurnModel) Prepare( } type preparedChatTurnModel struct { - llm *runtime.Runtime - model string - options core.GenerateOptions - // reasoning is the catalog's class for this model, carried from the - // prepare context so Generate can choose the output-token parameter the - // provider accepts. - reasoning bool + llm *runtime.Runtime + model string + options core.GenerateOptions toolInfo []gateway.ToolSummary toolTokens int outcomes *providerOutcomeRecorder @@ -190,20 +185,6 @@ func (m *preparedChatTurnModel) ToolSchemaTokens() int { return m.toolTokens } -// maxTokensForRequest returns the output-token bound to place on an ai-sdk -// generate request for a model. A reasoning-class model gets no bound at all: -// the OpenAI chat-completions provider serialises a non-zero bound as -// `max_tokens`, which reasoning models reject with HTTP 400 ("Use -// 'max_completion_tokens' instead"), and the pinned ai-sdk release has no -// max_completion_tokens path, so no bound is the only usable form. A classic -// model keeps the caller's bound unchanged. -func maxTokensForRequest(reasoning bool, bound int) int { - if reasoning { - return 0 - } - return bound -} - func (m *preparedChatTurnModel) Generate( ctx context.Context, request gateway.TurnModelRequest, @@ -212,8 +193,10 @@ func (m *preparedChatTurnModel) Generate( options := m.options options.Messages = buildTurnMessages(request) // This is one provider response's output allowance, not a turn- - // continuation budget. Tool loops remain free to continue. - options.MaxTokens = maxTokensForRequest(m.reasoning, request.MaxOutputTokens) + // continuation budget. Tool loops remain free to continue. The ai-sdk + // runtime picks max_completion_tokens or max_tokens from the model's + // catalog metadata. + options.MaxTokens = request.MaxOutputTokens // A runtime swapped to nil -- a live update that left no usable provider // configured -- is a refused turn, not a panic. if m.llm == nil { diff --git a/internal/app/archiegateway/mcp_sampling.go b/internal/app/archiegateway/mcp_sampling.go index 18071b8ab..aad4a708c 100644 --- a/internal/app/archiegateway/mcp_sampling.go +++ b/internal/app/archiegateway/mcp_sampling.go @@ -66,6 +66,6 @@ func (b *server) mcpSamplingHandler() mcp.SamplingHandler { // path (modelloop.SamplingMaxTokens) so the same server sees the same bound on // either path. func samplingMaxTokens(req mcp.SamplingRequest, models *chatModelManager, model string) int { - details, ok := models.ModelDetails(model) - return modelloop.SamplingMaxTokens(req.MaxTokens, details.MaxOutputTokens, ok && details.Reasoning) + details, _ := models.ModelDetails(model) + return modelloop.SamplingMaxTokens(req.MaxTokens, details.MaxOutputTokens) }