6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
129 lines
3.6 KiB
TypeScript
129 lines
3.6 KiB
TypeScript
import type { BlockTokens } from '@/executor/types'
|
|
import { LIST_PRICE_POLICY, type ModelUsage, priceModelUsage } from '@/providers/cost-policy'
|
|
import {
|
|
OPENAI_CACHE_WRITE_MULTIPLIER,
|
|
type ResponsesUsageTokens,
|
|
splitOpenAIUsage,
|
|
} from '@/providers/openai/utils'
|
|
import type { ModelPricing } from '@/providers/types'
|
|
|
|
export interface OpenAIUsageAccumulator {
|
|
/**
|
|
* Tokens billed at the base input rate. EXCLUDES cache reads and writes,
|
|
* which OpenAI reports as subsets of `input_tokens` and which are billed at
|
|
* their own rates.
|
|
*/
|
|
input: number
|
|
output: number
|
|
/** Every token the request consumed, cache reads and writes included. */
|
|
total: number
|
|
cacheRead: number
|
|
cacheWrite: number
|
|
}
|
|
|
|
interface OpenAIUsageCost {
|
|
input: number
|
|
output: number
|
|
total: number
|
|
toolCost?: number
|
|
pricing: ModelPricing
|
|
}
|
|
|
|
function roundedCost(value: number): number {
|
|
return Number.parseFloat(value.toFixed(8))
|
|
}
|
|
|
|
/**
|
|
* Creates an empty accumulator for one OpenAI provider request.
|
|
*/
|
|
export function createOpenAIUsageAccumulator(): OpenAIUsageAccumulator {
|
|
return {
|
|
input: 0,
|
|
output: 0,
|
|
total: 0,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds one Responses API turn's usage without counting cache tokens as
|
|
* uncached input.
|
|
*
|
|
* Normalization goes through {@link splitOpenAIUsage} so that subtracting the
|
|
* cache buckets out of the prompt total — and clamping a vendor payload that
|
|
* reports more cache tokens than it processed — stays in one place.
|
|
*/
|
|
export function addOpenAIUsage(
|
|
accumulator: OpenAIUsageAccumulator,
|
|
usage: ResponsesUsageTokens | undefined
|
|
): void {
|
|
if (!usage) return
|
|
|
|
const split = splitOpenAIUsage(usage)
|
|
|
|
accumulator.input += split.input
|
|
accumulator.output += split.output
|
|
accumulator.cacheRead += split.cacheRead
|
|
accumulator.cacheWrite += split.cacheWrite
|
|
accumulator.total += usage.totalTokens
|
|
}
|
|
|
|
/**
|
|
* Builds the block token shape. `total` is OpenAI's own reported total, which
|
|
* already counts cache reads and writes alongside the uncached remainder.
|
|
*/
|
|
export function buildOpenAIUsageTokens(
|
|
accumulator: OpenAIUsageAccumulator
|
|
): Required<Pick<BlockTokens, 'input' | 'output' | 'total' | 'cacheRead' | 'cacheWrite'>> {
|
|
return {
|
|
input: accumulator.input,
|
|
output: accumulator.output,
|
|
total: accumulator.total,
|
|
cacheRead: accumulator.cacheRead,
|
|
cacheWrite: accumulator.cacheWrite,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Builds the normalized usage for one OpenAI request.
|
|
*
|
|
* `input` is already the uncached remainder because {@link addOpenAIUsage}
|
|
* subtracted the cache buckets per turn — unlike Anthropic, whose
|
|
* `input_tokens` arrives exclusive of them.
|
|
*/
|
|
export function buildOpenAIModelUsage(accumulator: OpenAIUsageAccumulator): ModelUsage {
|
|
return {
|
|
input: accumulator.input,
|
|
output: accumulator.output,
|
|
cacheRead: accumulator.cacheRead,
|
|
cacheWrites: [
|
|
{ tokens: accumulator.cacheWrite, inputRateMultiplier: OPENAI_CACHE_WRITE_MULTIPLIER },
|
|
],
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prices one OpenAI request, cache reads and writes included, through the
|
|
* shared pricing function.
|
|
*
|
|
* Always at list price. Billability and the margin are applied once, centrally,
|
|
* by `executeProviderRequest` — a provider applying them here would double-count
|
|
* the multiplier.
|
|
*/
|
|
export function buildOpenAIUsageCost(
|
|
model: string,
|
|
accumulator: OpenAIUsageAccumulator,
|
|
toolCost = 0
|
|
): OpenAIUsageCost {
|
|
const cost = priceModelUsage(model, buildOpenAIModelUsage(accumulator), LIST_PRICE_POLICY)
|
|
|
|
return {
|
|
input: cost.input,
|
|
output: cost.output,
|
|
total: roundedCost(cost.total + toolCost),
|
|
...(toolCost > 0 ? { toolCost } : {}),
|
|
pricing: cost.pricing,
|
|
}
|
|
}
|