Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix: convert reasoning_effort to Claude thinking format & proactive token refresh (#627, #631)

diegosouzapw3976c79e12c7
diff --git a/open-sse/translator/request/openai-to-claude.ts b/open-sse/translator/request/openai-to-claude.ts
index 8d8d1a3160..169889d3d4 100644
--- a/open-sse/translator/request/openai-to-claude.ts
+++ b/open-sse/translator/request/openai-to-claude.ts
@@ -261,8 +261,8 @@ export function openaiToClaudeRequest(model, body, stream) {
// Normalize input_schema: Anthropic requires `properties` when type is "object" (#595).
// MCP tools (e.g. pencil, computer_use) may omit properties on object-type schemas.
- const rawSchema: Record<string, unknown> =
- toolData.parameters || toolData.input_schema || { type: "object", properties: {}, required: [] };
+ const rawSchema: Record<string, unknown> = toolData.parameters ||
+ toolData.input_schema || { type: "object", properties: {}, required: [] };
const normalizedSchema =
rawSchema.type === "object" && !rawSchema.properties
? { ...rawSchema, properties: {} }
@@ -317,6 +317,33 @@ export function openaiToClaudeRequest(model, body, stream) {
...(body.thinking.budget_tokens && { budget_tokens: body.thinking.budget_tokens }),
...(body.thinking.max_tokens && { max_tokens: body.thinking.max_tokens }),
};
+ } else if (body.reasoning_effort) {
+ // Convert OpenAI reasoning_effort to Claude thinking format (#627)
+ // Clients like OpenCode send reasoning_effort via @ai-sdk/openai-compatible
+ const effortBudgetMap: Record<string, number> = {
+ low: 1024,
+ medium: 10240,
+ high: 131072,
+ max: 131072,
+ };
+ const effort = String(body.reasoning_effort).toLowerCase();
+ const budget = effortBudgetMap[effort];
+ if (budget !== undefined && budget > 0) {
+ result.thinking = {
+ type: "enabled",
+ budget_tokens: budget,
+ };
+ // Claude requires max_tokens > budget_tokens
+ if (result.max_tokens <= budget) {
+ result.max_tokens = budget + 8192;
+ }
+ }
+ }
+
+ // Ensure max_tokens > budget_tokens for all thinking configurations (#627)
+ const budgetTokens = Number(result.thinking?.budget_tokens) || 0;
+ if (budgetTokens > 0 && result.max_tokens <= budgetTokens) {
+ result.max_tokens = budgetTokens + 8192;
}
// Attach toolNameMap to result for response translation
diff --git a/src/lib/tokenHealthCheck.ts b/src/lib/tokenHealthCheck.ts
index 051470cf5e..3fd05d53b5 100644
--- a/src/lib/tokenHealthCheck.ts
+++ b/src/lib/tokenHealthCheck.ts
@@ -187,11 +187,18 @@ async function checkConnection(conn) {
const intervalMs = intervalMin * 60 * 1000;
const lastCheck = conn.lastHealthCheckAt ? new Date(conn.lastHealthCheckAt).getTime() : 0;
- // Not yet due
- if (Date.now() - lastCheck < intervalMs) return;
+ // Proactive pre-expiry check (#631): if token is about to expire, refresh immediately
+ // regardless of the health check interval — prevents request failures between checks
+ const TOKEN_EXPIRY_BUFFER = 5 * 60 * 1000; // 5 minutes
+ const tokenExpiresAt = conn.tokenExpiresAt ? new Date(conn.tokenExpiresAt).getTime() : 0;
+ const isAboutToExpire = tokenExpiresAt > 0 && tokenExpiresAt - Date.now() < TOKEN_EXPIRY_BUFFER;
+ // Not yet due: skip if (a) interval hasn't elapsed AND (b) token is not about to expire
+ if (Date.now() - lastCheck < intervalMs && !isAboutToExpire) return;
+
+ const reason = isAboutToExpire ? "token expiring soon" : `interval: ${intervalMin}min`;
log(
- `${LOG_PREFIX} Refreshing ${conn.provider}/${conn.name || conn.email || conn.id} (interval: ${intervalMin}min)`
+ `${LOG_PREFIX} Refreshing ${conn.provider}/${conn.name || conn.email || conn.id} (${reason})`
);
const credentials = {