Skip to content
59 changes: 59 additions & 0 deletions apps/gateway/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ const completionsRequestSchema = z.object({
description: "Controls the reasoning effort for reasoning-capable models",
example: "medium",
}),
effort: z
.enum(["minimum", "low", "medium", "high"])
.nullable()
.optional()
.transform((val) => (val === null ? undefined : val))
.openapi({
description:
"Controls the computational effort for supported models (currently only claude-opus-4-5-20251101)",
example: "medium",
}),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
free_models_only: z.boolean().optional().default(false).openapi({
description:
"When used with auto routing, only route to free models (models with zero input and output pricing)",
Expand Down Expand Up @@ -412,6 +422,7 @@ chat.openapi(completions, async (c) => {
no_reasoning,
sensitive_word_check,
image_config,
effort,
} = validationResult.data;

// Extract reasoning_effort as mutable variable for auto-routing modification
Expand Down Expand Up @@ -668,6 +679,26 @@ chat.openapi(completions, async (c) => {
}
}

// Check if effort is specified but model doesn't support it
// Skip this check for "auto" and "custom" models as they will be resolved dynamically
if (
effort !== undefined &&
requestedModel !== "auto" &&
requestedModel !== "custom"
) {
// Check if any provider for this model has "effort" in supportedParameters
const supportsEffort = modelInfo.providers.some((provider) => {
const params = (provider as ProviderModelMapping).supportedParameters;
return params?.includes("effort");
});

if (!supportsEffort) {
throw new HTTPException(400, {
message: `Model ${requestedModel} does not support the effort parameter. Remove the effort parameter or use a model that supports it (e.g., claude-opus-4-5-20251101).`,
});
}
}

// Check if tools are specified but model doesn't support them
// Skip this check for "auto" and "custom" models as they will be resolved dynamically
if (
Expand Down Expand Up @@ -1760,6 +1791,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -1865,6 +1897,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -1979,6 +2012,7 @@ chat.openapi(completions, async (c) => {
userPlan,
sensitive_word_check,
image_config,
effort,
);

// Validate effective max_tokens value after prepareRequestBody
Expand Down Expand Up @@ -2074,6 +2108,14 @@ chat.openapi(completions, async (c) => {
const headers = getProviderHeaders(usedProvider, usedToken);
headers["Content-Type"] = "application/json";

// Add effort beta header for Anthropic if effort parameter is specified
if (usedProvider === "anthropic" && effort !== undefined) {
const currentBeta = headers["anthropic-beta"];
headers["anthropic-beta"] = currentBeta
? `${currentBeta},effort-2025-11-24`
: "effort-2025-11-24";
}

res = await fetch(url, {
method: "POST",
headers,
Expand Down Expand Up @@ -2103,6 +2145,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -2184,6 +2227,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -2332,6 +2376,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -3350,6 +3395,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -3500,6 +3546,15 @@ chat.openapi(completions, async (c) => {
try {
const headers = getProviderHeaders(usedProvider, usedToken);
headers["Content-Type"] = "application/json";

// Add effort beta header for Anthropic if effort parameter is specified
if (usedProvider === "anthropic" && effort !== undefined) {
const currentBeta = headers["anthropic-beta"];
headers["anthropic-beta"] = currentBeta
? `${currentBeta},effort-2025-11-24`
: "effort-2025-11-24";
}

res = await fetch(url, {
method: "POST",
headers,
Expand Down Expand Up @@ -3551,6 +3606,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -3637,6 +3693,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -3728,6 +3785,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down Expand Up @@ -3948,6 +4006,7 @@ chat.openapi(completions, async (c) => {
frequency_penalty,
presence_penalty,
reasoning_effort,
effort,
response_format,
tools,
tool_choice,
Expand Down
2 changes: 2 additions & 0 deletions apps/gateway/src/chat/tools/create-log-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function createLogEntry(
frequency_penalty: number | undefined,
presence_penalty: number | undefined,
reasoningEffort: "minimal" | "low" | "medium" | "high" | undefined,
effort: "minimum" | "low" | "medium" | "high" | undefined,
responseFormat: any | undefined,
tools: OpenAIToolInput[] | undefined,
toolChoice: any | undefined,
Expand Down Expand Up @@ -56,6 +57,7 @@ export function createLogEntry(
frequencyPenalty: frequency_penalty || null,
presencePenalty: presence_penalty || null,
reasoningEffort: reasoningEffort || null,
effort: effort || null,
responseFormat: responseFormat || null,
tools: tools || null,
toolChoice: toolChoice || null,
Expand Down
1 change: 1 addition & 0 deletions packages/db/migrations/1764069190_clever_anthem.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "log" ADD COLUMN "effort" text;
Loading