AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

[Bug] Claude Code integration broken: ValidateGenerateRequest blocks max_tokens 128000 with HTTP 400 instead of clamping

Open Madarchik09 opened this issue 1 month ago • 1 comments

Bug Description

In recent versions (v0.2.6 / v0.2.7), requests from the official Claude Code CLI via /v1/messages fail immediately with HTTP 400: API Error: 400 AI Studio 请求参数无效: max output tokens 128000 超过模型上限 65536

Root Cause

Claude Code CLI defaults to max_tokens: 128000 when sending requests to the Anthropic Messages API. In recent updates, ValidateGenerateRequest -> validateGenerateEntry -> encodeGenerationConfig enforces a strict check:

if includeMaxOutput && maxOutput > defaults.MaxOutputTokens {
    return nil, fmt.Errorf("max output tokens %d 超过模型上限 %d", maxOutput, defaults.MaxOutputTokens)
}

Since Google Gemini models have an output limit of 65536 (defaults.MaxOutputTokens = 65536), all Claude Code requests are rejected before reaching upstream.

Suggested Fix

To maintain seamless compatibility with Claude Code and other Anthropic-compatible clients, please consider clamping maxOutput to defaults.MaxOutputTokens instead of returning an error:

if includeMaxOutput && maxOutput > defaults.MaxOutputTokens {
    maxOutput = defaults.MaxOutputTokens
}

Alternatively, clamp request.MaxTokens inside toGenerateRequest in internal/api/anthropic.go.

Madarchik09 avatar Oct 08 '26 09:10 Madarchik09

Fixed: max_tokens above the model limit is now clamped instead of rejected. shipped in v0.2.8.

Mag1cFall avatar Oct 08 '26 18:10 Mag1cFall