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.
This commit is contained in:
sam committed 2026-10-05 22:56:48 +11:00
1 parent e50a381992
commit 2c447708e9
6 files changed
+17 -33

No files matched your search

+1 -1
View File
@@ -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
+2
View File
@@ -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=
+4 -5
View File
@@ -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
}
+1 -1
View File
@@ -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)
+7 -24
View File
@@ -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 {
+2 -2
View File
@@ -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)
}