Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/models/src/providers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
jsonOnlyPrompts,
type ModelResponse,
} from "../types";
import type { ModelSamplingConfig } from "@codraoss/schema";
import { assertPublicBaseUrl } from "../url-guard";
import { MODEL_TIMEOUT_MAX_MS, resolveOutputTokenCeiling } from "../limits";
import { profileSampling } from "./profile-sampling";
Expand Down Expand Up @@ -39,6 +40,23 @@ export function isOpenAIReasoningModel(model: string): boolean {
);
}

/**
* Maps a profile's thinking budget to OpenAI's `reasoning_effort`. Numbers above 10000 tokens are
* "high", above 3000 "medium", anything else "low"; "low", "medium" and "high" pass through. "off",
* "auto" and no budget return undefined, so the parameter is left out.
*/
export function reasoningEffortFor(
thinkingBudget: ModelSamplingConfig["thinkingBudget"] | undefined,
): "low" | "medium" | "high" | undefined {
if (typeof thinkingBudget === "number") {
return thinkingBudget > 10000 ? "high" : thinkingBudget > 3000 ? "medium" : "low";
}
if (thinkingBudget === "low" || thinkingBudget === "medium" || thinkingBudget === "high") {
return thinkingBudget;
}
return undefined;
}

function isEndpointDomain(baseUrl: string, domains: string[]): boolean {
try {
const host = new URL(baseUrl).hostname.toLowerCase();
Expand Down Expand Up @@ -159,6 +177,11 @@ export async function reviewWithOpenAI(
response_format: { type: "json_object" },
};

if (isReasoning) {
const effort = reasoningEffortFor(sampling?.thinkingBudget);
if (effort) bodyPayload.reasoning_effort = effort;
}

if (isZai) {
bodyPayload.thinking = { type: "disabled" };
}
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/schema-model-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const ModelSamplingConfigSchema = z.object({
temperature: z.number().min(0).max(2).default(0.7),
topP: z.number().min(0).max(1).optional(),
topK: z.number().int().min(1).optional(),
thinkingBudget: z.union([z.literal("off"), z.literal("auto"), z.number().int().min(0)]).default("auto"),
thinkingBudget: z.union([z.literal("off"), z.literal("auto"), z.literal("low"), z.literal("medium"), z.literal("high"), z.number().int().min(0)]).default("auto"),
maxOutputTokens: z.number().int().positive().optional(),
});

Expand Down
73 changes: 73 additions & 0 deletions test/openai-reasoning-effort.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import type { ModelSamplingConfig } from "@codraoss/schema";

const sampling = vi.hoisted(() => ({ current: undefined as unknown }));
vi.mock("../packages/models/src/providers/profile-sampling", () => ({
profileSampling: () => sampling.current,
geminiThinkingBudget: () => 0,
}));

import { callOpenAi, reasoningEffortFor } from "../packages/models/src/providers/openai";

const input = { systemPrompt: "system", userPrompt: "user" };
const okBody = JSON.stringify({
choices: [{ message: { content: '{"findings":[]}' } }],
usage: { prompt_tokens: 1, completion_tokens: 1 },
});

async function bodyFor(model: string, thinkingBudget?: ModelSamplingConfig["thinkingBudget"]) {
sampling.current = thinkingBudget === undefined ? undefined : { temperature: 0.7, thinkingBudget };
const fetchSpy = vi
.spyOn(globalThis, "fetch")
.mockResolvedValueOnce(new Response(okBody, { status: 200 }));
await callOpenAi(
{ apiKey: "k", baseUrl: "https://api.openai.com/v1", providerName: "OpenAI" },
model,
input,
);
return JSON.parse(String((fetchSpy.mock.calls.at(-1)![1] as RequestInit).body));
}

describe("reasoning_effort from the profile's thinking budget", () => {
afterEach(() => {
vi.restoreAllMocks();
sampling.current = undefined;
});

it("maps numeric budgets at the 3000 and 10000 boundaries", () => {
expect(reasoningEffortFor(0)).toBe("low");
expect(reasoningEffortFor(3000)).toBe("low");
expect(reasoningEffortFor(3001)).toBe("medium");
expect(reasoningEffortFor(10000)).toBe("medium");
expect(reasoningEffortFor(10001)).toBe("high");
});

it("passes low, medium and high through and leaves off, auto and none unset", () => {
expect(reasoningEffortFor("low")).toBe("low");
expect(reasoningEffortFor("medium")).toBe("medium");
expect(reasoningEffortFor("high")).toBe("high");
expect(reasoningEffortFor("off")).toBeUndefined();
expect(reasoningEffortFor("auto")).toBeUndefined();
expect(reasoningEffortFor(undefined)).toBeUndefined();
});

it("sends reasoning_effort for a reasoning model with a numeric budget", async () => {
expect((await bodyFor("o3-mini", 20000)).reasoning_effort).toBe("high");
expect((await bodyFor("o4-mini", 5000)).reasoning_effort).toBe("medium");
expect((await bodyFor("o3", 1000)).reasoning_effort).toBe("low");
});

it("sends a string budget as is", async () => {
expect((await bodyFor("gpt-6", "medium")).reasoning_effort).toBe("medium");
});

it("omits it when no budget is configured, or the budget is off or auto", async () => {
expect(await bodyFor("o3")).not.toHaveProperty("reasoning_effort");
expect(await bodyFor("o3", "auto")).not.toHaveProperty("reasoning_effort");
expect(await bodyFor("o3", "off")).not.toHaveProperty("reasoning_effort");
});

it("does not touch standard models", async () => {
expect(await bodyFor("gpt-4o", 20000)).not.toHaveProperty("reasoning_effort");
});
});