Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.4 KB
Raw Blame
import { AiChargeBasis } from '@activepieces/core-utils'
import { AiUsageCharge } from '@activepieces/shared'
import { system } from '../helper/system/system'
import { AppSystemProp } from '../helper/system/system-props'

export function chargeFor({ usage, toolCalls = 0 }: { usage: AiUsageCharge, toolCalls?: number }): number {
    return creditsForModelCall(usage) + toolCalls * CREDITS_PER_TOOL_CALL
}

function creditsForModelCall(usage: AiUsageCharge): number {
    switch (usage.type) {
        case AiChargeBasis.FIXED_CREDITS:
            return usage.credits
        case AiChargeBasis.USAGE_ONLY:
            return 0
        case AiChargeBasis.PROVIDER_REPORTED_COST:
            return usage.costUsd / dollarsPerCredit()
    }
}

export function costBasisOf(usage: AiUsageCharge): CostBasis | undefined {
    if (usage.type !== AiChargeBasis.PROVIDER_REPORTED_COST) {
        return undefined
    }
    return { costUsd: usage.costUsd, creditUsdValue: dollarsPerCredit() }
}

export function dollarsPerCredit(): number {
    const value = system.getDecimalOrThrow(AppSystemProp.AI_CREDIT_USD_VALUE)
    if (value <= 0) {
        throw new Error(`AP_${AppSystemProp.AI_CREDIT_USD_VALUE} must be greater than zero, so a call cannot bill an unbounded number of credits`)
    }
    return value
}

const CREDITS_PER_TOOL_CALL = 1

export type CostBasis = {
    costUsd: number
    creditUsdValue: number
}