import * as plugins from './plugins.js'; import type { TOpenAiReasoningEffort } from './index.js'; import type { IOpenAiChatGptModelReasoningEffort, IOpenAiChatGptModelServiceTier, TOpenAiChatGptInputModality, } from './smartai.chatgpt.catalog.js'; /** * A model of OpenAI's public API, as OpenAI's model page states it, in the vocabulary of `IOpenAiChatGptModel` * so a consumer reads both catalogs alike. Only what the page states is here: a field the page leaves out is absent, * never filled in. */ export interface IOpenAiApiModel { readonly providerId: 'openai'; readonly modelId: string; /** Other ids the model page lists for the model, such as dated snapshots. */ readonly snapshots: readonly string[]; readonly displayName: string; readonly description: string; /** The `reasoning.effort` values the model accepts, lowest first. The pages give no per-effort description. */ readonly reasoningEfforts: readonly IOpenAiChatGptModelReasoningEffort[]; /** The effort the model uses when a request names none; absent when OpenAI's pages do not state it. */ readonly defaultReasoningEffort?: TOpenAiReasoningEffort; readonly inputModalities: readonly TOpenAiChatGptInputModality[]; /** * The `service_tier` values besides Standard (`default`) the model is served in: `flex`, `priority` (Fast mode; * `fast` names it in a request too) and `ultrafast`. The pages give no per-tier description. */ readonly serviceTiers: readonly IOpenAiChatGptModelServiceTier[]; /** The context window in tokens. */ readonly contextWindow: number; /** The input limit in tokens, when the page states one. */ readonly maxInputTokens?: number; readonly maxOutputTokens: number; readonly source: plugins.IModelSource; } const checkedAt = '2026-10-21'; const modelPage = (modelIdArg: string): plugins.IModelSource => ({ url: `https://developers.openai.com/api/docs/models/${modelIdArg}`, checkedAt }); const efforts = (...effortsArg: TOpenAiReasoningEffort[]): IOpenAiChatGptModelReasoningEffort[] => effortsArg.map((effort) => ({ effort, description: '' })); const tier = (idArg: 'flex' | 'priority' | 'ultrafast', nameArg: string): IOpenAiChatGptModelServiceTier => ({ id: idArg, name: nameArg, description: '' }); const flexAndFast = [tier('flex', 'Flex'), tier('priority', 'Fast')]; const flexFastAndUltrafast = [...flexAndFast, tier('ultrafast', 'Ultrafast')]; const textAndImage: TOpenAiChatGptInputModality[] = ['text', 'image']; const deepFreeze = (valueArg: T): T => { if (typeof valueArg === 'object' && valueArg !== null && !Object.isFrozen(valueArg)) { Object.freeze(valueArg); for (const entry of Object.values(valueArg)) deepFreeze(entry); } return valueArg; }; /** * The models of OpenAI's public API this package describes, newest first, each read from its model page * (`source`) on 2026-10-21. GPT-6 Astra's page states no default effort, and the GPT-5.5 and GPT-5.4 pages state * no input limit. The `reasoningEfforts`, defaults and tiers of GPT-6 Astra and GPT-6.1 Sol are also stated in * https://developers.openai.com/api/docs/guides/latest-model and the tiers in * https://developers.openai.com/api/docs/pricing. */ export const OPENAI_API_MODELS: readonly IOpenAiApiModel[] = deepFreeze([ { providerId: 'openai', modelId: 'gpt-6-astra', snapshots: [], displayName: 'GPT-6 Astra', description: 'Our most capable model for the most demanding work.', reasoningEfforts: efforts('low', 'medium', 'high', 'xhigh', 'max'), inputModalities: textAndImage, serviceTiers: flexFastAndUltrafast, contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6-astra'), }, { providerId: 'openai', modelId: 'gpt-6.1-sol', snapshots: [], displayName: 'GPT-6.1 Sol', description: 'Near-Astra performance for complex work at a lower cost.', reasoningEfforts: efforts('low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium', inputModalities: textAndImage, serviceTiers: flexFastAndUltrafast, contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6.1-sol'), }, { providerId: 'openai', modelId: 'gpt-6-sol', snapshots: [], displayName: 'GPT-6 Sol', description: 'Built to power complex coding and agentic workflows.', reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium', inputModalities: textAndImage, serviceTiers: flexAndFast, contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6-sol'), }, { providerId: 'openai', modelId: 'gpt-6-luna', snapshots: [], displayName: 'GPT-6 Luna', description: 'Our most efficient model for focused, high-volume tasks.', reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium', inputModalities: textAndImage, serviceTiers: flexAndFast, contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-6-luna'), }, { providerId: 'openai', modelId: 'gpt-5.6-sol', snapshots: [], displayName: 'GPT-5.6 Sol', description: 'GPT-5.6 flagship model for complex professional work', reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium', inputModalities: textAndImage, serviceTiers: flexAndFast, contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.6-sol'), }, { providerId: 'openai', modelId: 'gpt-5.6-terra', snapshots: [], displayName: 'GPT-5.6 Terra', description: 'GPT-5.6 model that balances intelligence and cost', reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium', inputModalities: textAndImage, serviceTiers: flexAndFast, contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.6-terra'), }, { providerId: 'openai', modelId: 'gpt-5.6-luna', snapshots: [], displayName: 'GPT-5.6 Luna', description: 'GPT-5.6 model optimized for cost-sensitive workloads', reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh', 'max'), defaultReasoningEffort: 'medium', inputModalities: textAndImage, serviceTiers: flexAndFast, contextWindow: 1_050_000, maxInputTokens: 922_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.6-luna'), }, { providerId: 'openai', modelId: 'gpt-5.5', snapshots: ['gpt-5.5-2026-04-23'], displayName: 'GPT-5.5', description: 'A new class of intelligence for coding and professional work.', reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh'), defaultReasoningEffort: 'medium', inputModalities: textAndImage, serviceTiers: flexAndFast, contextWindow: 1_050_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.5'), }, { providerId: 'openai', modelId: 'gpt-5.4', snapshots: ['gpt-5.4-2026-03-05'], displayName: 'GPT-5.4', description: 'A more affordable model for coding and professional work.', reasoningEfforts: efforts('none', 'low', 'medium', 'high', 'xhigh'), defaultReasoningEffort: 'none', inputModalities: textAndImage, serviceTiers: flexAndFast, contextWindow: 1_050_000, maxOutputTokens: 128_000, source: modelPage('gpt-5.4'), }, ] satisfies IOpenAiApiModel[]); /** The `OPENAI_API_MODELS` entry for exactly this model id or one of its listed snapshots; nothing else matches. */ export function getOpenAiApiModel(modelIdArg: string): IOpenAiApiModel | undefined { return OPENAI_API_MODELS.find((model) => model.modelId === modelIdArg || model.snapshots.includes(modelIdArg)); } // --------------------------------------------------------------------------------------------------------------- // Prices /** USD per million tokens as an integer rate (micro-USD per million tokens). */ const usd = (dollarsArg: number): number => Math.round(dollarsArg * 1_000_000); /** A price row as the pricing page prints it: input, output, cached input, 30-minute write, 12-hour write. */ type TOpenAiPriceRow = readonly [input: number, output: number, cached: number, write30m: number, write12h: number]; const rates = ([input, output, cached, write30m, write12h]: TOpenAiPriceRow): plugins.IModelTokenRates => ({ input: usd(input), output: usd(output), cacheRead: usd(cached), cacheWrite: { '30m': usd(write30m), '12h': usd(write12h) }, }); /** Short context is a prompt of at most 272K input tokens; long context is above it. */ const shortContextTokens = 272_000; const service = (shortArg: TOpenAiPriceRow, longArg?: TOpenAiPriceRow): plugins.IModelServicePrice => ({ bands: longArg ? [{ maxPromptTokens: shortContextTokens, rates: rates(shortArg) }, { rates: rates(longArg) }] : [{ maxPromptTokens: shortContextTokens, rates: rates(shortArg) }], }); /** * GPT-5.5 and earlier charge no more for a cache write than for ordinary input * (https://developers.openai.com/api/docs/guides/prompt-caching, "Summary of model differences"), so a write is * priced at the input rate under either lifetime name. */ const withoutWriteCharge = (inputArg: number, outputArg: number, cachedArg: number): TOpenAiPriceRow => [inputArg, outputArg, cachedArg, inputArg, inputArg]; const pricingSource: plugins.IModelSource = { url: 'https://developers.openai.com/api/docs/pricing', checkedAt }; /** Web search $10 and file search $2.50 per 1,000 calls; a reader of Responses usage cannot count them (see readme). */ const toolCallFees = { web_search: 10_000, file_search: 2_500 }; const openAiPrice = ( modelIdArg: string, servicesArg: { standard: plugins.IModelServicePrice; flex: plugins.IModelServicePrice; fast: plugins.IModelServicePrice; ultrafast?: plugins.IModelServicePrice }, ): plugins.IModelPrice => { const model = getOpenAiApiModel(modelIdArg); if (!model) throw new Error(`No OpenAI API model ${modelIdArg}.`); return { providerId: 'openai', modelId: modelIdArg, aliases: model.snapshots, // The response reports Fast mode as `priority`; OpenAI's Fast-mode guide names `fast` for responses of later // models, so both names carry its price. services: { default: servicesArg.standard, flex: servicesArg.flex, priority: servicesArg.fast, fast: servicesArg.fast, ...(servicesArg.ultrafast ? { ultrafast: servicesArg.ultrafast } : {}), }, toolCallFees, source: pricingSource, }; }; /** * The prices of `OPENAI_API_MODELS` from https://developers.openai.com/api/docs/pricing, checked 2026-10-21, keyed * by the response's `service_tier`. A tier the page prices no long context for (Fast on GPT-5.5 and GPT-5.4) has * only the short band, so a longer prompt is refused. GPT-5.6 Sol's prices are promotional, at least through * 2026-11-21. */ export const OPENAI_MODEL_PRICES: readonly plugins.IModelPrice[] = deepFreeze([ openAiPrice('gpt-6-astra', { standard: service([10, 50, 1, 12.5, 25], [20, 75, 2, 25, 50]), flex: service([5, 25, 0.5, 6.25, 12.5], [10, 37.5, 1, 12.5, 25]), fast: service([20, 100, 2, 25, 50], [40, 150, 4, 50, 100]), ultrafast: service([60, 300, 6, 75, 150], [120, 450, 12, 150, 300]), }), openAiPrice('gpt-6.1-sol', { standard: service([2, 10, 0.1, 2.5, 5], [4, 15, 0.2, 5, 10]), flex: service([1, 5, 0.05, 1.25, 2.5], [2, 7.5, 0.1, 2.5, 5]), fast: service([4, 20, 0.2, 5, 10], [8, 30, 0.4, 10, 20]), ultrafast: service([12, 60, 0.6, 15, 30], [24, 90, 1.2, 30, 60]), }), openAiPrice('gpt-6-sol', { standard: service([2, 10, 0.2, 2.5, 5], [4, 15, 0.4, 5, 10]), flex: service([1, 5, 0.1, 1.25, 2.5], [2, 7.5, 0.2, 2.5, 5]), fast: service([4, 20, 0.4, 5, 10], [8, 30, 0.8, 10, 20]), }), openAiPrice('gpt-6-luna', { standard: service([0.1, 0.5, 0.01, 0.125, 0.25], [0.2, 0.75, 0.02, 0.25, 0.5]), flex: service([0.05, 0.25, 0.005, 0.0625, 0.125], [0.1, 0.375, 0.01, 0.125, 0.25]), fast: service([0.2, 1, 0.02, 0.25, 0.5], [0.4, 1.5, 0.04, 0.5, 1]), }), openAiPrice('gpt-5.6-sol', { standard: service([4, 20, 0.4, 5, 10], [8, 30, 0.8, 10, 20]), flex: service([2, 10, 0.2, 2.5, 5], [4, 15, 0.4, 5, 10]), fast: service([8, 40, 0.8, 10, 20], [16, 60, 1.6, 20, 40]), }), openAiPrice('gpt-5.6-terra', { standard: service([2, 12, 0.2, 2.5, 5], [4, 18, 0.4, 5, 10]), flex: service([1, 6, 0.1, 1.25, 2.5], [2, 9, 0.2, 2.5, 5]), fast: service([4, 24, 0.4, 5, 10], [8, 36, 0.8, 10, 20]), }), openAiPrice('gpt-5.6-luna', { standard: service([0.2, 1.2, 0.02, 0.25, 0.5], [0.4, 1.8, 0.04, 0.5, 1]), flex: service([0.1, 0.6, 0.01, 0.125, 0.25], [0.2, 0.9, 0.02, 0.25, 0.5]), fast: service([0.4, 2.4, 0.04, 0.5, 1], [0.8, 3.6, 0.08, 1, 2]), }), openAiPrice('gpt-5.5', { standard: service(withoutWriteCharge(5, 30, 0.5), withoutWriteCharge(10, 45, 1)), flex: service(withoutWriteCharge(2.5, 15, 0.25), withoutWriteCharge(5, 22.5, 0.5)), fast: service(withoutWriteCharge(12.5, 75, 1.25)), }), openAiPrice('gpt-5.4', { standard: service(withoutWriteCharge(2.5, 15, 0.25), withoutWriteCharge(5, 22.5, 0.5)), flex: service(withoutWriteCharge(1.25, 7.5, 0.13), withoutWriteCharge(2.5, 11.25, 0.25)), fast: service(withoutWriteCharge(5, 30, 0.5)), }), ] satisfies plugins.IModelPrice[]); /** The price of exactly this model id or one of its listed snapshots; `undefined` for any other id. */ export function getOpenAiModelPrice(modelIdArg: string): plugins.IModelPrice | undefined { return OPENAI_MODEL_PRICES.find((price) => price.modelId === modelIdArg || (price.aliases ?? []).includes(modelIdArg)); } // --------------------------------------------------------------------------------------------------------------- // Usage const isRecord = (valueArg: unknown): valueArg is Record => typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg); const tokenCount = (valueArg: unknown, nameArg: string): number => { if (!Number.isSafeInteger(valueArg) || (valueArg as number) < 0) { throw new plugins.ModelUsageReadError(`OpenAI usage ${nameArg} is not a token count.`); } return valueArg as number; }; const optionalTokenCount = (valueArg: unknown, nameArg: string): number | undefined => valueArg === undefined || valueArg === null ? undefined : tokenCount(valueArg, nameArg); /** * Reads a Responses API `usage` object, raw as the API sends it, with the response's `service_tier` (the tier that * served the call). `input_tokens` counts every input token, cache reads and writes included; the writes are split * by lifetime from `input_tokens_details.cache_write_tokens_details` (`ttl_30m_tokens`, `ttl_12h_tokens`), and * writes the response does not split go to `unknownLifetimeCacheWriteTokens`. Output includes reasoning. The usage * names no server-tool calls: count `web_search_call` and `file_search_call` output items into `toolCalls`. * A usage that is not complete throws a `ModelUsageReadError`. */ export function readOpenAiResponsesUsage(rawUsageArg: unknown, serviceTierArg: unknown): plugins.IModelPricedUsage { if (!isRecord(rawUsageArg)) throw new plugins.ModelUsageReadError('OpenAI usage is not an object.'); if (typeof serviceTierArg !== 'string' || serviceTierArg.length === 0) { throw new plugins.ModelUsageReadError('OpenAI response names no service tier.'); } const inputTokens = tokenCount(rawUsageArg.input_tokens, 'input_tokens'); const outputTokens = tokenCount(rawUsageArg.output_tokens, 'output_tokens'); const rawDetails = rawUsageArg.input_tokens_details; if (rawDetails !== undefined && rawDetails !== null && !isRecord(rawDetails)) { throw new plugins.ModelUsageReadError('OpenAI usage input_tokens_details is not an object.'); } const details = isRecord(rawDetails) ? rawDetails : undefined; const cachedTokens = optionalTokenCount(details?.cached_tokens, 'cached_tokens') ?? 0; const writeTokens = optionalTokenCount(details?.cache_write_tokens, 'cache_write_tokens') ?? 0; const writeDetails = details?.cache_write_tokens_details; const cacheWriteTokens: Record = {}; let unsplitWrites = writeTokens; if (writeDetails !== undefined && writeDetails !== null) { if (!isRecord(writeDetails)) { throw new plugins.ModelUsageReadError('OpenAI usage cache_write_tokens_details is not an object.'); } const write30m = optionalTokenCount(writeDetails.ttl_30m_tokens, 'ttl_30m_tokens') ?? 0; const write12h = optionalTokenCount(writeDetails.ttl_12h_tokens, 'ttl_12h_tokens') ?? 0; unsplitWrites = writeTokens - write30m - write12h; if (unsplitWrites < 0) throw new plugins.ModelUsageReadError('OpenAI usage splits more cache writes than it counts.'); if (write30m > 0) cacheWriteTokens['30m'] = write30m; if (write12h > 0) cacheWriteTokens['12h'] = write12h; } const uncachedInputTokens = inputTokens - cachedTokens - writeTokens; if (uncachedInputTokens < 0) throw new plugins.ModelUsageReadError('OpenAI usage counts more cached input than input.'); return { service: serviceTierArg, uncachedInputTokens, cacheReadTokens: cachedTokens, cacheWriteTokens, ...(unsplitWrites > 0 ? { unknownLifetimeCacheWriteTokens: unsplitWrites } : {}), outputTokens, }; }