fix: keep upstream token budgets billing-safe

This commit is contained in:
wlx committed 2026-09-28 08:39:04 +08:00
1 parent 4ec47cdb23
commit e0c926104e
4 files changed
+131 -44

No files matched your search

@@ -83,22 +83,6 @@ public class OpenAiCompatibleProvider {
return open(providerId,upstreamModel,input,true);
}
/**
* Normalize the small set of provider-specific request constraints that
* are known before the request is reserved and billed. Claude manual
* thinking requires a larger output budget than the common 512-token
* playground default; changing it here keeps the billing reservation and
* the upstream request aligned.
*/
public static void prepareRequestForModel(String upstreamModel, Map<String, Object> body) {
if (upstreamModel == null || body == null || !upstreamModel.toLowerCase(java.util.Locale.ROOT).contains("claude")
|| !upstreamModel.toLowerCase(java.util.Locale.ROOT).contains("thinking")) return;
String field = body.containsKey("max_completion_tokens") ? "max_completion_tokens" : "max_tokens";
if (!body.containsKey(field)) return;
int current = numberValue(body.get(field), 0);
if (current > 0 && current < 2048) body.put(field, 2048);
}
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) {
ProviderConfig provider = resolve(providerId);
if (provider == null || !provider.hasKey()) {
@@ -124,10 +108,12 @@ public class OpenAiCompatibleProvider {
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
String details = readErrorBody(response.body());
if (requiresLargerOutputBudget(details)) {
throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400);
}
// Some OpenAI-compatible relays use the newer spelling for
// reasoning-model budgets, and Claude manual thinking
// rejects small budgets. Retry only these deterministic
// request-shape errors; other failures remain bill-safe.
// reasoning-model budgets. Retry only this deterministic
// GPT request-shape error; other failures remain bill-safe.
if (attempt == 0 && prepareCompatibilityRetry(body, details)) continue;
throw classifyUpstreamError(response.statusCode(), details);
}
@@ -153,23 +139,20 @@ public class OpenAiCompatibleProvider {
/** Return true only for a deterministic request-shape rejection that is safe to retry. */
static boolean prepareCompatibilityRetry(Map<String, Object> body, String details) {
String text = details == null ? "" : details.toLowerCase(java.util.Locale.ROOT);
if (text.matches(".*max_tokens.{0,80}max_completion_tokens.*") && body.containsKey("max_tokens")) {
String model = String.valueOf(body.getOrDefault("model", "")).toLowerCase(java.util.Locale.ROOT);
if (model.startsWith("gpt-") && text.matches(".*max_tokens.{0,80}max_completion_tokens.*") && body.containsKey("max_tokens")) {
Object value = body.remove("max_tokens");
body.put("max_completion_tokens", value);
return true;
}
String model = String.valueOf(body.getOrDefault("model", "")).toLowerCase(java.util.Locale.ROOT);
if (text.matches(".*max_tokens must be greater than 1024.*") && body.containsKey("max_tokens")
&& model.contains("claude") && model.contains("thinking")) {
int current = numberValue(body.get("max_tokens"), 0);
if (current < 2048) {
body.put("max_tokens", 2048);
return true;
}
}
return false;
}
static boolean requiresLargerOutputBudget(String details) {
String text = details == null ? "" : details.toLowerCase(java.util.Locale.ROOT);
return text.matches(".*max_tokens.{0,80}(must be greater than|minimum is|at least).{0,20}1024.*");
}
private static int numberValue(Object value, int fallback) {
if (value instanceof Number number) return number.intValue();
try { return Integer.parseInt(String.valueOf(value)); } catch (RuntimeException ignored) { return fallback; }