Skip to content
Closed
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ IMAGE_SIZE_LIMIT_PRO_MB=100
# OpenAI
LLM_OPENAI_API_KEY=sk-your_openai_key_here

# OpenAI Discount (OpenAI-compatible endpoint via custom base URL + API key)
LLM_OPENAI_DISCOUNT_BASE_URL=https://your-openai-discount-endpoint
LLM_OPENAI_DISCOUNT_API_KEY=your_openai_discount_key_here

# Anthropic
LLM_ANTHROPIC_API_KEY=sk-ant-your_anthropic_key_here

Expand Down
9 changes: 7 additions & 2 deletions apps/gateway/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7550,7 +7550,9 @@ chat.openapi(completions, async (c) => {
// Extract usage data from transformedData to update tracking variables
if (
transformedData.usage &&
(usedProvider === "openai" || usedProvider === "azure")
(usedProvider === "openai" ||
usedProvider === "openai-discount" ||
usedProvider === "azure")
) {
const usage = transformedData.usage;
if (
Expand Down Expand Up @@ -7654,7 +7656,10 @@ chat.openapi(completions, async (c) => {
webSearchCount = 1;
}
}
} else if (usedProvider === "openai") {
} else if (
usedProvider === "openai" ||
usedProvider === "openai-discount"
) {
// For OpenAI Responses API, count web_search_call.completed events
if (data.type === "response.web_search_call.completed") {
webSearchCount++;
Expand Down
1 change: 1 addition & 0 deletions apps/gateway/src/chat/tools/extract-tool-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function extractToolCalls(
return null;
}
case "openai":
case "openai-discount":
case "azure":
return (transformedData ?? data).choices?.[0]?.delta?.tool_calls ?? null;
default: // OpenAI-compatible format
Expand Down
3 changes: 2 additions & 1 deletion apps/gateway/src/chat/tools/transform-response-to-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,8 @@ export function transformResponseToOpenai(
case "azure":
case "mistral":
case "novita":
case "openai": {
case "openai":
case "openai-discount": {
// Handle OpenAI / Azure image generation responses (e.g. gpt-image-2)
// Format: { created: number, data: [{ b64_json?: string, url?: string }], usage?: {...} }
if (
Expand Down
3 changes: 2 additions & 1 deletion apps/gateway/src/chat/tools/transform-streaming-to-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,8 @@ export function transformStreamingToOpenai(
}

case "azure":
case "openai": {
case "openai":
case "openai-discount": {
// Azure precedes every stream with a prompt-filter-only chunk that has
// empty id/object/model and no choices. The default OpenAI fallback
// path passes the empty values through and breaks downstream
Expand Down
15 changes: 13 additions & 2 deletions packages/actions/src/get-provider-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ export function getProviderEndpoint(
case "openai":
url = "https://api.openai.com";
break;
case "openai-discount":
url = skipEnvVars
? undefined
: getProviderEnvValue("openai-discount", "baseUrl", configIndex);
if (!url) {
throw new Error(
"OpenAI Discount provider requires LLM_OPENAI_DISCOUNT_BASE_URL environment variable",
);
}
break;
case "anthropic":
url = "https://api.anthropic.com";
break;
Expand Down Expand Up @@ -543,15 +553,16 @@ export function getProviderEndpoint(
"2024-05-01-preview";
return `${url}/models/chat/completions?api-version=${apiVersion}`;
}
case "openai": {
case "openai":
case "openai-discount": {
if (imageGenerations) {
return `${url}/v1/images/generations`;
}
// Use responses endpoint for models that support responses API
if (model) {
const modelDef = models.find((m) => m.id === (modelId ?? model));
const providerMapping = modelDef?.providers.find(
(p) => p.providerId === "openai",
(p) => p.providerId === provider,
);
const supportsResponsesApi =
(providerMapping as ProviderModelMapping)?.supportsResponsesApi ===
Expand Down
12 changes: 8 additions & 4 deletions packages/actions/src/prepare-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ export async function prepareRequestBody(
// unsupported enum value.
const handlesNoneNatively =
usedProvider === "openai" ||
usedProvider === "openai-discount" ||
usedProvider === "azure" ||
usedProvider === "google-ai-studio" ||
usedProvider === "glacier" ||
Expand All @@ -764,7 +765,9 @@ export async function prepareRequestBody(
// Handle OpenAI / Azure image generation models (e.g. gpt-image-2)
if (
imageGenerations &&
(usedProvider === "openai" || usedProvider === "azure")
(usedProvider === "openai" ||
usedProvider === "openai-discount" ||
usedProvider === "azure")
) {
// Extract prompt and image URLs from last user message
const lastUserMessage = [...messages]
Expand Down Expand Up @@ -1243,7 +1246,8 @@ export async function prepareRequestBody(

switch (usedProvider) {
case "azure":
case "openai": {
case "openai":
case "openai-discount": {
// Determine whether to use Responses API format.
// If useResponsesApi is explicitly passed (derived from endpoint URL), use it.
// Otherwise, fall back to checking the model definition.
Expand Down Expand Up @@ -1281,7 +1285,7 @@ export async function prepareRequestBody(
},
};

if (usedProvider === "openai") {
if (usedProvider === "openai" || usedProvider === "openai-discount") {
if (prompt_cache_key !== undefined) {
responsesBody.prompt_cache_key = prompt_cache_key;
}
Expand Down Expand Up @@ -1365,7 +1369,7 @@ export async function prepareRequestBody(
return responsesBody;
} else {
// Use regular chat completions format
if (usedProvider === "openai") {
if (usedProvider === "openai" || usedProvider === "openai-discount") {
if (prompt_cache_key !== undefined) {
requestBody.prompt_cache_key = prompt_cache_key;
}
Expand Down
28 changes: 28 additions & 0 deletions packages/models/src/models/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,34 @@ export const openaiModels = [
],
jsonOutput: true,
},
{
providerId: "openai-discount",
externalId: "gpt-5-5",
inputPrice: "5.0e-6",
outputPrice: "30.0e-6",
cachedInputPrice: "0.5e-6",
discount: "0.1",
requestPrice: "0",
contextSize: 1050000,
maxOutput: 128000,
streaming: true,
vision: true,
tools: true,
webSearch: true,
webSearchPrice: "0.01",
reasoning: true,
reasoningOutput: "omit",
supportsResponsesApi: true,
jsonOutputSchema: true,
supportedParameters: [
"temperature",
"top_p",
"frequency_penalty",
"presence_penalty",
"response_format",
],
jsonOutput: true,
},
],
},
{
Expand Down
22 changes: 22 additions & 0 deletions packages/models/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ export const providers: ProviderDefinition[] = [
gdpr: true,
},
},
{
id: "openai-discount",
name: "OpenAI (Discount)",
description:
"OpenAI-compatible provider routed through a discounted endpoint configured via environment variables.",
env: {
required: {
apiKey: "LLM_OPENAI_DISCOUNT_API_KEY",
baseUrl: "LLM_OPENAI_DISCOUNT_BASE_URL",
},
},
priority: 1.5,
streaming: true,
cancellation: true,
color: "#0ea5e9",
website: null,
announcement: null,
termsUrl: null,
privacyPolicyUrl: null,
headquarters: null,
dataPolicy: null,
},
{
id: "anthropic",
name: "Anthropic",
Expand Down
Loading