Skip to content

Commit ac98014

Browse files
committed
fix(AnthropicChatService): adjust BudgetTokens to not exceed MaxTokens
1 parent b54c63d commit ac98014

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

src/ClaudeCodeProxy.Core/AI/AnthropicChatService.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public async Task<ClaudeChatCompletionDto> ChatCompletionsAsync(AnthropicInput i
2828
options.Address = "https://api.anthropic.com/";
2929
}
3030

31+
if (input.Thinking is not null && input.Thinking.BudgetTokens > 0 && input.MaxTokens != null)
32+
{
33+
if (input.Thinking.BudgetTokens > input.MaxTokens)
34+
{
35+
input.Thinking.BudgetTokens = input.MaxTokens.Value - 1;
36+
}
37+
}
38+
3139
var client = HttpClientFactory.GetHttpClient(options.Address, config);
3240

3341
var url = new Uri(options.Address);
@@ -58,10 +66,10 @@ await client.PostJsonAsync(options.Address.TrimEnd('/') + "/v1/messages?beta=tru
5866
RetryAfterSeconds = (int)retryAfter,
5967
Timestamp = DateTime.Now
6068
};
61-
62-
logger.LogWarning("Claude账户达到限流,需要等待 {RetryAfter} 秒。错误信息:{Error}",
69+
70+
logger.LogWarning("Claude账户达到限流,需要等待 {RetryAfter} 秒。错误信息:{Error}",
6371
retryAfter, error);
64-
72+
6573
throw new RateLimitException("Claude账户达到限流", rateLimitInfo);
6674
}
6775

@@ -89,6 +97,14 @@ await response.Content.ReadFromJsonAsync<ClaudeChatCompletionDto>(ThorJsonSerial
8997
options.Address = "https://api.anthropic.com/";
9098
}
9199

100+
if (input.Thinking is not null && input.Thinking.BudgetTokens > 0 && input.MaxTokens != null)
101+
{
102+
if (input.Thinking.BudgetTokens > input.MaxTokens)
103+
{
104+
input.Thinking.BudgetTokens = input.MaxTokens.Value - 1;
105+
}
106+
}
107+
92108
var client = HttpClientFactory.GetHttpClient(options.Address, config);
93109

94110
var url = new Uri(options.Address);
@@ -105,7 +121,8 @@ await response.Content.ReadFromJsonAsync<ClaudeChatCompletionDto>(ThorJsonSerial
105121
if (response.StatusCode >= HttpStatusCode.BadRequest)
106122
{
107123
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
108-
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", options.Address.TrimEnd('/') + "/v1/messages?beta=true",
124+
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}",
125+
options.Address.TrimEnd('/') + "/v1/messages?beta=true",
109126
response.StatusCode, error);
110127

111128
throw new Exception("OpenAI对话异常" + error);

0 commit comments

Comments
 (0)