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

feat(ai): bill AI on what the call actually cost, and delete the weight tables (#15494)

Abdul011bf82d704d
diff --git a/packages/core/execution/src/lib/workers/worker-contract.ts b/packages/core/execution/src/lib/workers/worker-contract.ts
index 18de920ab47..a95a6acd25b 100644
--- a/packages/core/execution/src/lib/workers/worker-contract.ts
+++ b/packages/core/execution/src/lib/workers/worker-contract.ts
@@ -180,7 +180,6 @@ export type SaveAgentMessagesRequest = {
uiMessages: unknown[]
title?: string
modelName?: string
- aiCredits?: number
}
export type SaveAgentFileRequest = {
@@ -454,7 +453,7 @@ export type ReportAiUsageRequest = {
export type AiUsageFlowRunContext = {
flowId: string
flowRunId: string
- environment: string
+ environment?: string
}
export type AiUsageChatContext = {
diff --git a/packages/core/piece-types/src/lib/ai-providers.test.ts b/packages/core/piece-types/src/lib/ai-providers.test.ts
index 2c73090b35e..10532834a1b 100644
--- a/packages/core/piece-types/src/lib/ai-providers.test.ts
+++ b/packages/core/piece-types/src/lib/ai-providers.test.ts
@@ -1,6 +1,6 @@
-import { AIProviderName, isNil } from '@activepieces/core-utils'
+import { AIProviderName } from '@activepieces/core-utils'
import { describe, expect, it } from 'vitest'
-import { ACTIVEPIECES_CHAT_TIERS, AI_PROVIDER_CAPABILITIES, aiProviderUtils, ALLOWED_CHAT_MODELS_BY_PROVIDER, MANAGED_MODEL_WEIGHTS, MODELS_AWAITING_A_CREDIT_WEIGHT } from './ai-providers'
+import { ACTIVEPIECES_CHAT_TIERS, AI_PROVIDER_CAPABILITIES, aiProviderUtils, ALLOWED_CHAT_MODELS_BY_PROVIDER } from './ai-providers'
describe('AI_PROVIDER_CAPABILITIES', () => {
it('has an entry for every provider', () => {
@@ -148,26 +148,3 @@ describe('tier native model ids', () => {
}
})
})
-
-describe('every managed model we offer has a credit weight somebody chose', () => {
- const offered = ALLOWED_CHAT_MODELS_BY_PROVIDER[AIProviderName.ACTIVEPIECES] ?? []
-
- const priced = (model: string) =>
- ACTIVEPIECES_CHAT_TIERS.some((tier) => tier.modelId === model) || !isNil(MANAGED_MODEL_WEIGHTS[model])
-
- it('offers nothing that quietly falls back to the default rate, apart from what is still pending', () => {
- expect(offered.filter((model) => !priced(model))).toEqual(MODELS_AWAITING_A_CREDIT_WEIGHT)
- })
-
- it('leaves nothing in the pending list that is no longer offered, so the exception cannot outlive the model', () => {
- for (const model of MODELS_AWAITING_A_CREDIT_WEIGHT) {
- expect(offered, model).toContain(model)
- }
- })
-
- it('never lists a model as both priced and pending', () => {
- for (const model of MODELS_AWAITING_A_CREDIT_WEIGHT) {
- expect(priced(model), model).toBe(false)
- }
- })
-})
diff --git a/packages/core/piece-types/src/lib/ai-providers.ts b/packages/core/piece-types/src/lib/ai-providers.ts
index 44359a4b403..3c082bf0ee9 100644
--- a/packages/core/piece-types/src/lib/ai-providers.ts
+++ b/packages/core/piece-types/src/lib/ai-providers.ts
@@ -369,97 +369,11 @@ function buildProviderCapabilities(provider: AIProviderName): AIProviderCapabili
}
export const ACTIVEPIECES_CHAT_TIERS = [
- { id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5', nativeModelId: 'claude-haiku-4-5', thinkingBudget: 5_000, creditWeight: 2 },
- { id: 'smart', label: 'Expert', modelId: 'anthropic/claude-sonnet-4.6', nativeModelId: 'claude-sonnet-4-6', thinkingBudget: 10_000, creditWeight: 10 },
- { id: 'premium', label: 'Heavy', modelId: 'anthropic/claude-opus-4.8', nativeModelId: 'claude-opus-4-7', thinkingBudget: 20_000, creditWeight: 20 },
+ { id: 'fast', label: 'Fast', modelId: 'anthropic/claude-haiku-4.5', nativeModelId: 'claude-haiku-4-5', thinkingBudget: 5_000 },
+ { id: 'smart', label: 'Expert', modelId: 'anthropic/claude-sonnet-4.6', nativeModelId: 'claude-sonnet-4-6', thinkingBudget: 10_000 },
+ { id: 'premium', label: 'Heavy', modelId: 'anthropic/claude-opus-4.8', nativeModelId: 'claude-opus-4-7', thinkingBudget: 20_000 },
] as const
-export const MANAGED_MODEL_WEIGHTS: Record<string, number> = {
- 'ai21/jamba-large-1.7': 6,
- 'amazon/nova-premier-v1': 6,
- 'anthropic/claude-fable-5': 45,
- 'anthropic/claude-opus-4': 45,
- 'anthropic/claude-opus-4.1': 45,
- 'anthropic/claude-opus-4.5': 20,
- 'anthropic/claude-opus-4.6': 20,
- 'anthropic/claude-opus-4.7': 20,
- 'anthropic/claude-opus-4.7-fast': 200,
- 'anthropic/claude-opus-4.8-fast': 45,
- 'anthropic/claude-opus-5': 20,
- 'anthropic/claude-opus-5-fast': 45,
- 'anthropic/claude-sonnet-4': 10,
- 'anthropic/claude-sonnet-4.5': 10,
- 'anthropic/claude-sonnet-5': 6,
- 'cohere/command-a': 6,
- 'cohere/command-r-plus-08-2024': 6,
- 'google/gemini-2.5-flash': 2,
- 'google/gemini-2.5-pro': 6,
- 'google/gemini-2.5-pro-preview': 6,
- 'google/gemini-2.5-pro-preview-05-06': 6,
- 'google/gemini-3-flash-preview': 2,
- 'google/gemini-3-pro-image': 6,
- 'google/gemini-3-pro-image-preview': 6,
- 'google/gemini-3.1-pro-preview': 6,
- 'google/gemini-3.1-pro-preview-customtools': 6,
- 'google/gemini-3.5-flash': 6,
- 'google/gemini-3.6-flash': 6,
- 'google/gemini-3.7-flash': 6,
- 'mistralai/mistral-medium-3-5': 6,
- 'moonshotai/kimi-k3': 10,
- 'openai/gpt-4.1-mini': 2,
- 'openai/gpt-5.4-mini': 2,
- 'openai/gpt-5.4-nano': 2,
- 'openai/gpt-4': 45,
- 'openai/gpt-4-turbo': 20,
- 'openai/gpt-4-turbo-preview': 20,
- 'openai/gpt-4.1': 6,
- 'openai/gpt-4o': 6,
- 'openai/gpt-4o-2024-05-13': 10,
- 'openai/gpt-4o-2024-08-06': 6,
- 'openai/gpt-4o-2024-11-20': 6,
- 'openai/gpt-5': 6,
- 'openai/gpt-5-image': 6,
- 'openai/gpt-5-pro': 90,
- 'openai/gpt-5.1': 6,
- 'openai/gpt-5.1-codex': 6,
- 'openai/gpt-5.1-codex-max': 6,
- 'openai/gpt-5.2': 10,
- 'openai/gpt-5.2-chat': 10,
- 'openai/gpt-5.2-codex': 10,
- 'openai/gpt-5.2-pro': 200,
- 'openai/gpt-5.3-chat': 10,
- 'openai/gpt-5.3-codex': 10,
- 'openai/gpt-5.4': 10,
- 'openai/gpt-5.4-image-2': 10,
- 'openai/gpt-5.4-pro': 200,
- 'openai/gpt-5.5': 20,
- 'openai/gpt-5.5-pro': 200,
- 'openai/gpt-5.6-sol': 20,
- 'openai/gpt-5.6-sol-pro': 20,
- 'openai/gpt-audio': 6,
- 'openai/gpt-chat-latest': 20,
- 'openai/o1': 45,
- 'openai/o1-pro': 500,
- 'openai/o3': 6,
- 'openai/o3-pro': 90,
- 'perplexity/sonar-deep-research': 6,
- 'perplexity/sonar-pro': 10,
- 'perplexity/sonar-pro-search': 10,
- 'perplexity/sonar-reasoning-pro': 6,
- 'sakana/fugu-ultra': 20,
- '~anthropic/claude-fable-latest': 45,
- '~anthropic/claude-opus-latest': 20,
- '~anthropic/claude-sonnet-latest': 6,
- '~google/gemini-flash-latest': 6,
- '~google/gemini-pro-latest': 6,
- '~moonshotai/kimi-latest': 10,
- '~openai/gpt-latest': 20,
-}
-
-export const MODELS_AWAITING_A_CREDIT_WEIGHT = ['x-ai/grok-4.20']
-
-export const DEFAULT_MANAGED_MODEL_WEIGHT = 2
-
export const DEFAULT_CHAT_TIER_ID = 'smart' as const
export type ActivepiecesChatTier = typeof ACTIVEPIECES_CHAT_TIERS[number]
@@ -484,20 +398,8 @@ export const AI_PROVIDER_CAPABILITIES: Record<AIProviderName, AIProviderCapabili
[AIProviderName.MOONSHOT]: buildProviderCapabilities(AIProviderName.MOONSHOT),
}
-function resolveAiCreditWeight({ provider, model }: { provider: string, model: string }): number {
- if (provider !== AIProviderName.ACTIVEPIECES) {
- return 1
- }
- const tierWeight = ACTIVEPIECES_CHAT_TIERS.find((tier) => tier.modelId === model)?.creditWeight
- if (tierWeight !== undefined) {
- return tierWeight
- }
- return MANAGED_MODEL_WEIGHTS[model] ?? DEFAULT_MANAGED_MODEL_WEIGHT
-}
-
export const aiProviderUtils = {
getMaxContextTokens,
- resolveAiCreditWeight,
getCuratedChatModels,
isCuratedChatModelId,
managedChatModelIds,
diff --git a/packages/core/shared/src/lib/ee/agent/index.ts b/packages/core/shared/src/lib/ee/agent/index.ts
index 2cb873c3744..250dca8c863 100644
--- a/packages/core/shared/src/lib/ee/agent/index.ts
+++ b/packages/core/shared/src/lib/ee/agent/index.ts
@@ -255,7 +255,6 @@ export const UpdateAgentMemoryRequest = z.object({
})
export type UpdateAgentMemoryRequest = z.infer<typeof UpdateAgentMemoryRequest>
-export const CHAT_BYOK_CREDIT_WEIGHT = 1
export const CHAT_CREDITS_PER_TOOL_CALL = 1
export const ImportAgentMemoryRequest = z.object({
diff --git a/packages/core/shared/src/lib/management/ai-providers/index.ts b/packages/core/shared/src/lib/management/ai-providers/index.ts
index 37c18813b10..2bdd7c0e9c5 100644
--- a/packages/core/shared/src/lib/management/ai-providers/index.ts
+++ b/packages/core/shared/src/lib/management/ai-providers/index.ts
@@ -455,9 +455,6 @@ export {
AI_PROVIDER_ENTITY_TYPES,
ALLOWED_CHAT_MODELS_BY_PROVIDER,
ACTIVEPIECES_CHAT_TIERS,
- MANAGED_MODEL_WEIGHTS,
- MODELS_AWAITING_A_CREDIT_WEIGHT,
- DEFAULT_MANAGED_MODEL_WEIGHT,
DEFAULT_CHAT_TIER_ID,
AI_PROVIDER_CAPABILITIES,
OPENAI_COMPATIBLE_VENDOR_BASE_URLS,
diff --git a/packages/core/utils/src/lib/activepieces-ai-billing.ts b/packages/core/utils/src/lib/activepieces-ai-billing.ts
index 5c28f995e6c..43961088177 100644
--- a/packages/core/utils/src/lib/activepieces-ai-billing.ts
+++ b/packages/core/utils/src/lib/activepieces-ai-billing.ts
@@ -8,16 +8,25 @@ export enum ActivepiecesAiBillingScope {
export type ActivepiecesAiBilling =
| { scope: ActivepiecesAiBillingScope.PLATFORM, platformId: string }
- | { scope: ActivepiecesAiBillingScope.PROJECT, platformId: string, projectId: string }
- | { scope: ActivepiecesAiBillingScope.CONVERSATION, platformId: string, projectId: string | null, conversationId: string }
+ | { scope: ActivepiecesAiBillingScope.PROJECT, platformId: string, projectId: string, flowRun?: ActivepiecesAiFlowRun }
+ | { scope: ActivepiecesAiBillingScope.CONVERSATION, platformId: string, projectId: string | null, conversationId: string, chat?: ActivepiecesAiChat }
-export type ActivepiecesAiCall = {
- generationId: string
- costUsd: number
- inputTokens?: number
- outputTokens?: number
+export type ActivepiecesAiFlowRun = {
+ flowId: string
+ flowRunId: string
+ environment?: string
}
+export type ActivepiecesAiChat = {
+ userId: string
+ turnIndex: number
+ tier: string
+}
+
+export type ActivepiecesAiCall =
+ | { charge: 'observed-cost', generationId: string, costUsd: number, inputTokens?: number, outputTokens?: number }
+ | { charge: 'flat-credits', credits: number, generationId?: string }
+
export type ActivepiecesAiCostEvent = {
billing: ActivepiecesAiBilling
provider: AIProviderName
diff --git a/packages/server/api/src/app/ai/ai-usage-hooks.ts b/packages/server/api/src/app/ai/ai-usage-hooks.ts
new file mode 100644
index 00000000000..c781f510a61
--- /dev/null
+++ b/packages/server/api/src/app/ai/ai-usage-hooks.ts
@@ -0,0 +1,16 @@
+import { hooksFactory } from '../helper/hooks-factory'
+
+export const aiUsageHooks = hooksFactory.create<AiUsageHooks>(() => ({
+ addConversationCredits: async (_params: AddConversationCreditsParams) => {
+ return
+ },
+}))
+
+export type AddConversationCreditsParams = {
+ conversationId: string
+ credits: number
+}
+
+export type AiUsageHooks = {
+ addConversationCredits(params: AddConversationCreditsParams): Promise<void>
+}
diff --git a/packages/server/api/src/app/ai/ai-usage-service.ts b/packages/server/api/src/app/ai/ai-usage-service.ts
index 81353ea228f..b49c76daced 100644
--- a/packages/server/api/src/app/ai/ai-usage-service.ts
+++ b/packages/server/api/src/app/ai/ai-usage-service.ts
@@ -1,10 +1,11 @@
-import { ActivepiecesAiBillingScope, isNil } from '@activepieces/core-utils'
+import { ActivepiecesAiBillingScope, isNil, tryCatch } from '@activepieces/core-utils'
import { isAppSumoCreditedPlan, ReportAiUsageRequest } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { platformPlanService } from '../ee/platform/platform-plan/platform-plan.service'
import { trackBillingAndSendTelemetry } from '../platform/billing-and-telemetry'
import { AiCreditConsumptionProperties, ChatAppSumoConsumptionProperties, ChatCreditConsumptionProperties, CreditUsageSource } from '../platform/billing-provider'
import { chargeFor } from './ai-credits'
+import { aiUsageHooks } from './ai-usage-hooks'
export const aiUsageService = (log: FastifyBaseLogger) => ({
async report(input: ReportAiUsageRequest): Promise<void> {
@@ -21,18 +22,31 @@ export const aiUsageService = (log: FastifyBaseLogger) => ({
credits: { platformId, value: credits, source: CreditUsageSource.CHAT, idempotencyKey: input.idempotencyKey, properties: chat.credits },
appSumo: appSumo ? { platformId, value: credits, source: CreditUsageSource.CHAT, idempotencyKey: `${input.idempotencyKey}:appSumo`, properties: chat.appSumo } : undefined,
})
- return
}
- const properties = billingProperties(input, toolCalls)
- await trackBillingAndSendTelemetry({
- log,
- licenseKey: platformPlan.licenseKey,
- credits: { platformId, value: credits, source: CreditUsageSource.AI, idempotencyKey: input.idempotencyKey, properties },
- appSumo: appSumo ? { platformId, value: credits, source: CreditUsageSource.AI, idempotencyKey: `${input.idempotencyKey}:appSumo`, properties } : undefined,
- })
+ else {
+ const properties = billingProperties(input, toolCalls)
+ await trackBillingAndSendTelemetry({
+ log,
+ licenseKey: platformPlan.licenseKey,
+ credits: { platformId, value: credits, source: CreditUsageSource.AI, idempotencyKey: input.idempotencyKey, properties },
+ appSumo: appSumo ? { platformId, value: credits, source: CreditUsageSource.AI, idempotencyKey: `${input.idempotencyKey}:appSumo`, properties } : undefined,
+ })
+ }
+ await addConversationCredits({ input, credits, log })
},
})
+async function addConversationCredits({ input, credits, log }: { input: ReportAiUsageRequest, credits: number, log: FastifyBaseLogger }): Promise<void> {
+ if (input.billing.scope !== ActivepiecesAiBillingScope.CONVERSATION) {
+ return
+ }
+ const { conversationId } = input.billing
+ const { error } = await tryCatch(() => aiUsageHooks.get(log).addConversationCredits({ conversationId, credits }))
+ if (!isNil(error)) {
+ log.warn({ conversation: { id: conversationId }, error }, '[aiUsageService] Could not add this call to the conversation credit total')
+ }
+}
+
function chatEventOf(input: ReportAiUsageRequest, toolCalls: number): { credits: ChatCreditConsumptionProperties, appSumo: ChatAppSumoConsumptionProperties } | undefined {
if (input.billing.scope !== ActivepiecesAiBillingScope.CONVERSATION || isNil(input.chat)) {
return undefined
@@ -63,7 +77,7 @@ function billingProperties(input: ReportAiUsageRequest, toolCalls: number): AiCr
projectId: projectIdOf(input),
flowId: input.flowRun?.flowId ?? OUTSIDE_A_FLOW,
flowRunId: input.flowRun?.flowRunId ?? input.requestId ?? OUTSIDE_A_FLOW,
- environment: input.flowRun?.environment ?? OUTSIDE_A_FLOW,
+ environment: input.flowRun?.environment ?? UNKNOWN_ENVIRONMENT,
messages: MESSAGES_PER_MODEL_CALL,
toolCalls,
breakdown: [{
@@ -85,3 +99,4 @@ function projectIdOf(input: ReportAiUsageRequest): string {
const MESSAGES_PER_MODEL_CALL = 1
const PROJECTLESS_CHAT = 'chat'
const OUTSIDE_A_FLOW = 'none'
+const UNKNOWN_ENVIRONMENT = 'unknown'
diff --git a/packages/server/api/src/app/app.ts b/packages/server/api/src/app/app.ts
index 8bc10276d9a..0609e82c7b6 100644
--- a/packages/server/api/src/app/app.ts
+++ b/packages/server/api/src/app/app.ts
@@ -13,6 +13,7 @@ import { agentsModule } from './agents/agents-module'
import { aiProviderService } from './ai/ai-provider-service'
import { aiProviderModule } from './ai/ai-provider.module'
import { aiToolConfigModule } from './ai/ai-tool-config.module'
+import { aiUsageHooks } from './ai/ai-usage-hooks'
import { platformAnalyticsModule } from './analytics/platform-analytics.module'
import { setPlatformOAuthService } from './app-connection/app-connection-service/oauth2'
import { appConnectionModule } from './app-connection/app-connection.module'
@@ -27,6 +28,7 @@ import { rateLimitModule } from './core/security/rate-limit'
import { authenticationMiddleware } from './core/security/v2/authn/authentication-middleware'
import { authorizationMiddleware } from './core/security/v2/authz/authorization-middleware'
import { distributedLock, redisConnections } from './database/redis-connections'
+import { agentConversationCreditsHooks } from './ee/agent/agent-conversation-credits'
import { agentEvalModule } from './ee/agent/agent-eval-controller'
import { agentHelpers } from './ee/agent/agent-helpers'
import { assertAgentsResolveInProject } from './ee/agent/agent-service'
@@ -358,6 +360,7 @@ export const setupApp = async (app: FastifyInstance): Promise<FastifyInstance> =
billingProvider.set(autumnBillingProvider)
resumePageHooks.set((log) => ({ getTheme: (params) => appearanceHelper.getTheme({ ...params, log }) }))
flowPublishHooks.set(() => ({ assertReferencesResolve: assertAgentsResolveInProject }))
+ aiUsageHooks.set(agentConversationCreditsHooks)
exceptionHandler.initializeSentry(system.get(AppSystemProp.SENTRY_DSN))
systemJobHandlers.registerJobHandler(SystemJobName.HARD_DELETE_PLATFORM, (data) => platformTeardownJobs(app.log).hardDeletePlatformHandler(data))
break
@@ -397,6 +400,7 @@ export const setupApp = async (app: FastifyInstance): Promise<FastifyInstance> =
billingProvider.set(autumnBillingProvider)
resumePageHooks.set((log) => ({ getTheme: (params) => appearanceHelper.getTheme({ ...params, log }) }))
flowPublishHooks.set(() => ({ assertReferencesResolve: assertAgentsResolveInProject }))
+ aiUsageHooks.set(agentConversationCreditsHooks)
break
case ApEdition.COMMUNITY:
await app.register(platformProjectModule)
diff --git a/packages/server/api/src/app/ee/agent/agent-conversation-credits.ts b/packages/server/api/src/app/ee/agent/agent-conversation-credits.ts
new file mode 100644
index 00000000000..f15b7823379
--- /dev/null
+++ b/packages/server/api/src/app/ee/agent/agent-conversation-credits.ts
@@ -0,0 +1,16 @@
+import { FastifyBaseLogger } from 'fastify'
+import { AddConversationCreditsParams, AiUsageHooks } from '../../ai/ai-usage-hooks'
+import { AgentConversationEntity } from './agent-conversation-entity'
+import { agentHelpers } from './agent-helpers'
+
+export const agentConversationCreditsHooks = (_log: FastifyBaseLogger): AiUsageHooks => ({
+ async addConversationCredits({ conversationId, credits }: AddConversationCreditsParams): Promise<void> {
+ await agentHelpers.conversationRepo()
+ .createQueryBuilder()
+ .update(AgentConversationEntity)
+ .set({ aiCredits: () => 'COALESCE("aiCredits", 0) + :credits' })
+ .setParameter('credits', credits)
+ .where('id = :conversationId', { conversationId })
+ .execute()
+ },
+})
diff --git a/packages/server/api/src/app/ee/agent/agent-draft-ai.ts b/packages/server/api/src/app/ee/agent/agent-draft-ai.ts
index 93d42e2ddbb..2411ea1679e 100644
--- a/packages/server/api/src/app/ee/agent/agent-draft-ai.ts
+++ b/packages/server/api/src/app/ee/agent/agent-draft-ai.ts
@@ -1,8 +1,8 @@
import { readFileSync } from 'node:fs'
import path from 'node:path'
-import { ActivepiecesError, AIProviderName, apId, ErrorCode, isNil, PlatformId, ProjectId, tryCatch, tryCatchSync } from '@activepieces/core-utils'
+import { ActivepiecesError, apId, ErrorCode, isNil, PlatformId, ProjectId, tryCatch, tryCatchSync } from '@activepieces/core-utils'
import { agentAiUtils } from '@activepieces/server-utils'
-import { AgentDraftFields, AgentTool, AgentToolType, CHAT_BYOK_CREDIT_WEIGHT, DEFAULT_CHAT_TIER_ID, DraftAgentResponse, isAppSumoCreditedPlan, MAX_SUGGESTED_AGENT_TOOLS, mcpToolNameUtils } from '@activepieces/shared'
+import { AgentDraftFields, AgentTool, AgentToolType, DEFAULT_CHAT_TIER_ID, DraftAgentResponse, isAppSumoCreditedPlan, MAX_SUGGESTED_AGENT_TOOLS, mcpToolNameUtils } from '@activepieces/shared'
import { APICallError, generateText, LanguageModel } from 'ai'
import { FastifyBaseLogger } from 'fastify'
import { z } from 'zod'
@@ -17,6 +17,7 @@ const DRAFT_TIMEOUT_MS = 30_000
const REPLY_LOG_LIMIT = 500
const REASON_LIMIT = 200
const FAST_TIER_ID = 'fast'
+const CREDITS_PER_DRAFT = 1
const CANDIDATE_PIECE_LIMIT = 8
const DRAFT_SYSTEM_PROMPT = readFileSync(path.resolve('packages/server/api/src/assets/prompts/agent-draft-prompt.md'), 'utf8')
@@ -196,11 +197,10 @@ function parseDraft(raw: string): DraftReply | null {
async function debitDraft({ platformId, projectId, log }: { platformId: PlatformId, projectId: ProjectId, log: FastifyBaseLogger }): Promise<void> {
const { error } = await tryCatch(async () => {
const provider = await agentHelpers.resolveChatProviderName({ platformId, projectId, log })
- const value = provider === AIProviderName.ACTIVEPIECES ? agentHelpers.resolveTier({ tierId: FAST_TIER_ID }).creditWeight : CHAT_BYOK_CREDIT_WEIGHT
const platformPlan = await platformPlanService(log).getOrCreateForPlatform(platformId)
const usage = {
platformId,
- value,
+ value: CREDITS_PER_DRAFT,
source: CreditUsageSource.AGENT_DRAFT as const,
idempotencyKey: `agent-draft:${apId()}`,
properties: { platformId, projectId, provider },
diff --git a/packages/server/api/src/app/ee/agent/chat-tool-billing.ts b/packages/server/api/src/app/ee/agent/chat-tool-billing.ts
index 906fd8755ec..7f9c242ddec 100644
--- a/packages/server/api/src/app/ee/agent/chat-tool-billing.ts
+++ b/packages/server/api/src/app/ee/agent/chat-tool-billing.ts
@@ -1,4 +1,12 @@
-import { PersistedAgentMessage, PersistedAgentPartType, PersistedAgentRole, PersistedToolCallStatus } from '@activepieces/shared'
+import { AIProviderName, isNil, spreadIfDefined } from '@activepieces/core-utils'
+import { AgentConversation, CHAT_CREDITS_PER_TOOL_CALL, isAppSumoCreditedPlan, PersistedAgentMessage, PersistedAgentPartType, PersistedAgentRole, PersistedToolCallStatus } from '@activepieces/shared'
+import { FastifyBaseLogger } from 'fastify'
+import { LicenseKeyPostHogEvents } from '../../helper/telemetry.utils'
+import { trackBillingAndSendTelemetry } from '../../platform/billing-and-telemetry'
+import { CreditUsageSource } from '../../platform/billing-provider'
+import { platformPlanService } from '../platform/platform-plan/platform-plan.service'
+import { agentHelpers } from './agent-helpers'
+import { agentHistory } from './history/agent-history'
const BILLABLE_EXTERNAL_TOOL_NAMES = new Set<string>([
'ap_web_search',
@@ -23,7 +31,79 @@ function countBillableToolCallsInLatestTurn({ messages }: { messages: PersistedA
).length, 0)
}
+async function chargeForLatestTurn({ conversation, runId, log }: ChargeForLatestTurnParams): Promise<void> {
+ const messages = agentHistory.resolveMessages({ conversation, log })
+ const billableToolCalls = countBillableToolCallsInLatestTurn({ messages })
+ const turnIndex = messages.filter((message) => message.role === PersistedAgentRole.USER).length
+ const idempotencyScope = runId ?? turnIndex
+ const provider = await agentHelpers.resolveChatProviderName({
+ platformId: conversation.platformId,
+ projectId: conversation.projectId ?? null,
+ log,
+ })
+ const model = agentHelpers.resolveModelIdForAnalytics({ selectedModel: conversation.modelName ?? null, provider })
+ const tier = agentHelpers.resolveTier({ tierId: conversation.modelName ?? null })
+ const platformPlan = await platformPlanService(log).getOrCreateForPlatform(conversation.platformId)
+
+ const turnCredits = provider === AIProviderName.ACTIVEPIECES ? 0 : CREDITS_PER_OWN_KEY_TURN
+ const creditValue = turnCredits + billableToolCalls * CHAT_CREDITS_PER_TOOL_CALL
+ const charge = creditValue === 0 ? undefined : {
+ platformId: conversation.platformId,
+ value: creditValue,
+ source: CreditUsageSource.CHAT,
+ idempotencyKey: `${conversation.id}:chatTurn:${idempotencyScope}`,
+ properties: {
+ platformId: conversation.platformId,
+ projectId: conversation.projectId ?? PROJECTLESS_CHAT,
+ userId: conversation.userId,
+ conversationId: conversation.id,
+ turnIndex,
+ messages: turnCredits,
+ toolCalls: billableToolCalls,
+ provider,
+ model,
+ tier: tier.id,
+ },
+ }
+ const appSumoCharge = isNil(charge) || !isAppSumoCreditedPlan(platformPlan.plan) ? undefined : {
+ ...charge,
+ idempotencyKey: `${conversation.id}:appSumoChatTurn:${idempotencyScope}`,
+ properties: {
+ platformId: conversation.platformId,
+ projectId: conversation.projectId ?? PROJECTLESS_CHAT,
+ conversationId: conversation.id,
+ turnIndex,
+ tier: tier.id,
+ },
+ }
+
+ await trackBillingAndSendTelemetry({
+ log,
+ licenseKey: platformPlan.licenseKey,
+ ...spreadIfDefined('credits', charge),
+ ...spreadIfDefined('appSumo', appSumoCharge),
+ telemetry: {
+ event: LicenseKeyPostHogEvents.CHAT_MESSAGE,
+ properties: {
+ provider,
+ model,
+ toolsUsed: billableToolCalls,
+ },
+ },
+ })
+}
+
export const chatToolBilling = {
isBillableChatToolCall,
countBillableToolCallsInLatestTurn,
+ chargeForLatestTurn,
+}
+
+const PROJECTLESS_CHAT = 'chat'
+const CREDITS_PER_OWN_KEY_TURN = 1
+
+type ChargeForLatestTurnParams = {
+ conversation: AgentConversation
+ runId?: string
+ log: FastifyBaseLogger
}
diff --git a/packages/server/api/src/app/ee/agent/chat-usage-tracker.ts b/packages/server/api/src/app/ee/agent/chat-usage-tracker.ts
deleted file mode 100644
index 1fecb9dfc70..00000000000
--- a/packages/server/api/src/app/ee/agent/chat-usage-tracker.ts
+++ /dev/null
@@ -1,86 +0,0 @@
-import { AIProviderName } from '@activepieces/core-utils'
-import { AgentConversation, AgentRunSource, CHAT_BYOK_CREDIT_WEIGHT, CHAT_CREDITS_PER_TOOL_CALL, isAppSumoCreditedPlan, PersistedAgentRole } from '@activepieces/shared'
-import { FastifyBaseLogger } from 'fastify'
-import { LicenseKeyPostHogEvents } from '../../helper/telemetry.utils'
-import { trackBillingAndSendTelemetry } from '../../platform/billing-and-telemetry'
-import { CreditUsageSource } from '../../platform/billing-provider'
-import { platformPlanService } from '../platform/platform-plan/platform-plan.service'
-import { agentHelpers } from './agent-helpers'
-import { chatToolBilling } from './chat-tool-billing'
-import { agentHistory } from './history/agent-history'
-
-export const chatUsageTracker = (log: FastifyBaseLogger) => ({
- async track({ conversation, runId }: TrackParams): Promise<void> {
- if (conversation.source === AgentRunSource.FLOW_STEP) {
- return
- }
- const messages = agentHistory.resolveMessages({ conversation, log })
- const billableToolCalls = chatToolBilling.countBillableToolCallsInLatestTurn({ messages })
- const turnIndex = messages.filter((message) => message.role === PersistedAgentRole.USER).length
- const idempotencyScope = runId ?? turnIndex
-
- const provider = await agentHelpers.resolveChatProviderName({
- platformId: conversation.platformId,
- projectId: conversation.projectId ?? null,
- log,
- })
- const model = agentHelpers.resolveModelIdForAnalytics({ selectedModel: conversation.modelName ?? null, provider })
-
- const isManagedProvider = provider === AIProviderName.ACTIVEPIECES
- const tier = agentHelpers.resolveTier({ tierId: conversation.modelName ?? null })
- const creditWeight = isManagedProvider ? tier.creditWeight : CHAT_BYOK_CREDIT_WEIGHT
- const creditValue = creditWeight + billableToolCalls * CHAT_CREDITS_PER_TOOL_CALL
-
- const platformPlan = await platformPlanService(log).getOrCreateForPlatform(conversation.platformId)
- const isAppSumoPlan = isAppSumoCreditedPlan(platformPlan.plan)
-
- await trackBillingAndSendTelemetry({
- log,
- licenseKey: platformPlan.licenseKey,
- credits: {
- platformId: conversation.platformId,
- value: creditValue,
- source: CreditUsageSource.CHAT,
- idempotencyKey: `${conversation.id}:chat:${idempotencyScope}`,
- properties: {
- platformId: conversation.platformId,
- projectId: conversation.projectId ?? 'chat',
- userId: conversation.userId,
- conversationId: conversation.id,
- turnIndex,
- messages: 1,
- toolCalls: billableToolCalls,
- provider,
- model,
- tier: tier.id,
- },
- },
- appSumo: isAppSumoPlan ? {
- platformId: conversation.platformId,
- value: creditValue,
- source: CreditUsageSource.CHAT,
- idempotencyKey: `${conversation.id}:appSumoAi:${idempotencyScope}`,
- properties: {
- platformId: conversation.platformId,
- projectId: conversation.projectId ?? 'chat',
- conversationId: conversation.id,
- turnIndex,
- tier: tier.id,
- },
- } : undefined,
- telemetry: {
- event: LicenseKeyPostHogEvents.CHAT_MESSAGE,
- properties: {
- provider,
- model,
- toolsUsed: billableToolCalls,
- },
- },
- })
- },
-})
-
-type TrackParams = {
- conversation: AgentConversation
- runId?: string
-}
diff --git a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
index 818ec88844b..f70f6e6db65 100644
--- a/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
+++ b/packages/server/api/src/app/ee/agent/rpc/conversation-rpc.ts
@@ -3,7 +3,7 @@ import { AgentConversationStatus, AgentRunSource, FileCompression, FileType, Hea
import { FastifyBaseLogger } from 'fastify'
import { agentHelpers } from '.././agent-helpers'
import { chatAnalyticsTelemetry } from '.././chat-analytics-sync'
-import { chatUsageTracker } from '.././chat-usage-tracker'
+import { chatToolBilling } from '.././chat-tool-billing'
import { fileService } from '../../../file/file.service'
import { filesService } from '../../../file/files-service'
import { rejectedPromiseHandler } from '../../../helper/promise-handler'
@@ -40,7 +40,6 @@ export const conversationRpc = (log: FastifyBaseLogger) => ({
const isSuccessfulCompletion = input.messages.length > 0
const updates: Record<string, unknown> = {
status: isSuccessfulCompletion ? AgentConversationStatus.IDLE : AgentConversationStatus.ERROR,
- ...spreadIfDefined('aiCredits', input.aiCredits),
}
// No-shrink guard against silent context loss. The LLM history only ever grows within a
@@ -87,7 +86,7 @@ export const conversationRpc = (log: FastifyBaseLogger) => ({
const conversation = await agentHelpers.conversationRepo().findOneBy({ id: input.conversationId })
if (conversation) {
chatAnalyticsTelemetry(log).sendConversationUpdate({ conversation })
- rejectedPromiseHandler(chatUsageTracker(log).track({ conversation, runId: input.runId }), log)
+ rejectedPromiseHandler(chatToolBilling.chargeForLatestTurn({ conversation, runId: input.runId, log }), log)
}
}
},
diff --git a/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-extractor.ts b/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-extractor.ts
deleted file mode 100644
index 82c4c262204..00000000000
--- a/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-extractor.ts
+++ /dev/null
@@ -1,212 +0,0 @@
-import { isNil } from '@activepieces/core-utils'
-import { AgentResult, AgentStepBlock, AI_PIECE_NAME, ContentBlockType, FlowActionType, flowStructureUtil, FlowVersion, LogSliceRef, LoopStepResult, Step, StepOutput, StepOutputStatus, StepOutputType } from '@activepieces/shared'
-import { sleep } from '../../helper/sleep'
-
-async function extractAiUsage({ steps, flowVersion, stepNameToTest, fetchSlice }: ExtractParams): Promise<AiUsage> {
- const aiStepsByName = buildAiStepsByName(flowVersion)
- const loopStepsWithAi = buildLoopStepsWithAiDescendants(flowVersion)
- const executedSteps = isNil(stepNameToTest) ? steps : pickStep({ steps, stepName: stepNameToTest })
- const usages = await collectStepAiUsages({ steps: executedSteps, aiStepsByName, loopStepsWithAi, fetchSlice, walkCounter: { stepsWalked: 0 } })
- return summarize(usages)
-}
-
-function pickStep({ steps, stepName }: { steps: Record<string, StepOutput>, stepName: string }): Record<string, StepOutput> {
- const output = steps[stepName]
- return isNil(output) ? {} : { [stepName]: output }
-}
-
-function flowVersionHasAiStep(flowVersion: FlowVersion): boolean {
- return flowStructureUtil.getAllSteps(flowVersion.trigger).some(isAiPieceStep)
-}
-
-function isAiPieceStep(step: Step): boolean {
- return step.type === FlowActionType.PIECE && step.settings.pieceName === AI_PIECE_NAME
-}
-
-function buildAiStepsByName(flowVersion: FlowVersion): Map<string, AiStepConfig> {
- return flowStructureUtil.getAllSteps(flowVersion.trigger)
- .filter(isAiPieceStep)
- .reduce((map, step) => map.set(step.name, {
- actionName: step.type === FlowActionType.PIECE ? step.settings.actionName : undefined,
- input: step.type === FlowActionType.PIECE ? asRecord(step.settings.input) : undefined,
- }), new Map<string, AiStepConfig>())
-}
-
-function buildLoopStepsWithAiDescendants(flowVersion: FlowVersion): Set<string> {
- return flowStructureUtil.getAllSteps(flowVersion.trigger)
- .filter((step) => step.type === FlowActionType.LOOP_ON_ITEMS)
- .filter((loop) => flowStructureUtil.getAllChildSteps(loop).some(isAiPieceStep))
- .reduce((set, loop) => set.add(loop.name), new Set<string>())
-}
-
-async function collectStepAiUsages({ steps, aiStepsByName, loopStepsWithAi, fetchSlice, walkCounter }: CollectParams): Promise<AiStepUsage[]> {
- const usages: AiStepUsage[] = []
- for (const [name, output] of Object.entries(steps)) {
- await sleepEveryNSteps(walkCounter)
- const stepConfig = aiStepsByName.get(name)
- if (!isNil(stepConfig)) {
- const usage = await toStepAiUsage({ output, stepConfig, fetchSlice })
- if (!isNil(usage)) {
- usages.push(usage)
- }
- }
- if (output.type === FlowActionType.LOOP_ON_ITEMS && loopStepsWithAi.has(name)) {
- const iterations = (output.output as LoopStepResult | undefined)?.iterations ?? []
- for (const iteration of iterations) {
- usages.push(...await collectStepAiUsages({ steps: iteration, aiStepsByName, loopStepsWithAi, fetchSlice, walkCounter }))
- }
- }
- }
- return usages
-}
-
-async function sleepEveryNSteps(walkCounter: WalkCounter): Promise<void> {
- walkCounter.stepsWalked += 1
- if (walkCounter.stepsWalked % SLEEP_EVERY_N_STEPS === 0) {
- await sleep(500)
- }
-}
-
-async function toStepAiUsage({ output, stepConfig, fetchSlice }: ToStepAiUsageParams): Promise<AiStepUsage | null> {
- const isAgent = stepConfig.actionName === RUN_AGENT_ACTION_NAME
- let toolCalls = 0
- if (isAgent) {
- const blocks = await resolveAgentBlocks({ output, fetchSlice })
- if (blocks.length === 0) {
- return null
- }
- toolCalls = blocks.filter((block) => block.type === ContentBlockType.TOOL_CALL).length
- }
- else if (output.status !== StepOutputStatus.SUCCEEDED) {
- return null
- }
- const { provider, model } = resolveProviderModel({ output, stepConfig })
- return { provider, model, toolCalls }
-}
-
-async function resolveAgentBlocks({ output, fetchSlice }: ResolveAgentBlocksParams): Promise<AgentStepBlock[]> {
- const raw = output.outputType === StepOutputType.SLICE
- ? await fetchSlice(output.output as LogSliceRef)
- : output.output
- return (raw as AgentResult | undefined)?.steps ?? []
-}
-
-function resolveProviderModel({ output, stepConfig }: ResolveProviderModelParams): { provider: string, model: string } {
- const fromLog = extractProviderModel(asRecord(output.input))
- const fromSettings = extractProviderModel(stepConfig.input)
- return {
- provider: fromLog.provider ?? fromSettings.provider ?? UNKNOWN,
- model: fromLog.model ?? fromSettings.model ?? UNKNOWN,
- }
-}
-
-function extractProviderModel(input: Record<string, unknown> | undefined): { provider?: string, model?: string } {
- if (isNil(input)) {
- return {}
- }
- const aiProviderModel = asRecord(input.aiProviderModel)
- return {
- provider: cleanString(aiProviderModel?.provider ?? input.provider),
- model: cleanString(aiProviderModel?.model ?? input.model),
- }
-}
-
-function summarize(usages: AiStepUsage[]): AiUsage {
- const breakdown = usages.reduce((map, usage) => {
- const key = `${usage.provider}|${usage.model}`
- const entry = map.get(key) ?? { provider: usage.provider, model: usage.model, messages: 0, toolCalls: 0 }
- return map.set(key, {
- provider: entry.provider,
- model: entry.model,
- messages: entry.messages + 1,
- toolCalls: entry.toolCalls + usage.toolCalls,
- })
- }, new Map<string, AiUsageBreakdownEntry>())
- return {
- messages: usages.length,
- toolCalls: usages.reduce((sum, usage) => sum + usage.toolCalls, 0),
- breakdown: [...breakdown.values()],
- }
-}
-
-function asRecord(value: unknown): Record<string, unknown> | undefined {
- if (typeof value === 'object' && !isNil(value) && !Array.isArray(value)) {
- return value as Record<string, unknown>
- }
- return undefined
-}
-
-function cleanString(value: unknown): string | undefined {
- if (typeof value !== 'string' || value.length === 0 || value === REDACTED_MARKER) {
- return undefined
- }
- return value
-}
-
-const RUN_AGENT_ACTION_NAME = 'run_agent'
-const UNKNOWN = 'unknown'
-const REDACTED_MARKER = '**REDACTED**'
-const SLEEP_EVERY_N_STEPS = 500
-
-export const flowRunAiUsageExtractor = { extractAiUsage, flowVersionHasAiStep }
-
-export type SliceFetcher = (ref: LogSliceRef) => Promise<unknown>
-
-export type AiUsage = {
- messages: number
- toolCalls: number
- breakdown: AiUsageBreakdownEntry[]
-}
-
-type ExtractParams = {
- steps: Record<string, StepOutput>
- flowVersion: FlowVersion
- stepNameToTest?: string
- fetchSlice: SliceFetcher
-}
-
-type WalkCounter = {
- stepsWalked: number
-}
-
-type CollectParams = {
- steps: Record<string, StepOutput>
- aiStepsByName: Map<string, AiStepConfig>
- loopStepsWithAi: Set<string>
- fetchSlice: SliceFetcher
- walkCounter: WalkCounter
-}
-
-type ToStepAiUsageParams = {
- output: StepOutput
- stepConfig: AiStepConfig
- fetchSlice: SliceFetcher
-}
-
-type ResolveAgentBlocksParams = {
- output: StepOutput
- fetchSlice: SliceFetcher
-}
-
-type ResolveProviderModelParams = {
- output: StepOutput
- stepConfig: AiStepConfig
-}
-
-type AiStepConfig = {
- actionName: string | undefined
- input: Record<string, unknown> | undefined
-}
-
-type AiStepUsage = {
- provider: string
- model: string
- toolCalls: number
-}
-
-type AiUsageBreakdownEntry = {
- provider: string
- model: string
- messages: number
- toolCalls: number
-}
diff --git a/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-tracker.ts b/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-tracker.ts
deleted file mode 100644
index bfb9aef88a9..00000000000
--- a/packages/server/api/src/app/flows/flow-run/flow-run-ai-usage-tracker.ts
+++ /dev/null
@@ -1,108 +0,0 @@
-import { aiProviderUtils } from '@activepieces/core-piece-types'
-import { isNil } from '@activepieces/core-utils'
-import { FileType, FlowRun, FlowVersion, isAppSumoCreditedPlan, LogSliceRef } from '@activepieces/shared'
-import { FastifyBaseLogger } from 'fastify'
-import { platformPlanService } from '../../ee/platform/platform-plan/platform-plan.service'
-import { fileService } from '../../file/file.service'
-import { system } from '../../helper/system/system'
-import { LicenseKeyPostHogEvents } from '../../helper/telemetry.utils'
-import { trackBillingAndSendTelemetry } from '../../platform/billing-and-telemetry'
-import { AiCreditConsumptionProperties, CreditUsageSource, toFlowRunCreditProperties } from '../../platform/billing-provider'
-import { projectService } from '../../project/project-service'
-import { flowRunAiUsageExtractor } from './flow-run-ai-usage-extractor'
-import { flowRunService } from './flow-run-service'
-
-export const flowRunAiUsageTracker = (log: FastifyBaseLogger) => ({
- async track({ flowRun, flowVersion }: TrackParams): Promise<void> {
- if (!flowRunAiUsageExtractor.flowVersionHasAiStep(flowVersion)) {
- return
- }
- const project = await projectService(log).getOne(flowRun.projectId)
- if (isNil(project)) {
- return
- }
- const steps = await flowRunService(log).getStepsOrNull({ flowRun })
- if (isNil(steps)) {
- return
- }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps,
- flowVersion,
- stepNameToTest: flowRun.stepNameToTest,
- fetchSlice: (ref) => fetchSlice({ log, projectId: flowRun.projectId, ref }),
- })
- if (usage.messages === 0 && usage.toolCalls === 0) {
- return
- }
- const creditValue = usage.breakdown.reduce((sum, entry) => sum + entry.messages * resolveAiCreditWeight({ provider: entry.provider, model: entry.model }) + entry.toolCalls, 0)
- const attempt = flowRun.startTime ?? flowRun.created
- const platformPlan = await platformPlanService(log).getOrCreateForPlatform(project.platformId)
- const isAppSumoPlan = isAppSumoCreditedPlan(platformPlan.plan)
- const aiProperties: AiCreditConsumptionProperties = {
- ...toFlowRunCreditProperties({ platformId: project.platformId, flowRun }),
- messages: usage.messages,
- toolCalls: usage.toolCalls,
- breakdown: usage.breakdown,
- }
- await trackBillingAndSendTelemetry({
- log,
- licenseKey: platformPlan.licenseKey,
- credits: {
- platformId: project.platformId,
- value: creditValue,
- source: CreditUsageSource.AI,
- idempotencyKey: `${flowRun.id}:ai:${attempt}`,
- properties: aiProperties,
- },
- appSumo: creditValue > 0 && isAppSumoPlan ? {
- platformId: project.platformId,
- value: creditValue,
- source: CreditUsageSource.AI,
- idempotencyKey: `${flowRun.id}:appSumoAi:${attempt}`,
- properties: aiProperties,
- } : undefined,
- telemetry: {
- event: LicenseKeyPostHogEvents.AI_USAGE_PER_RUN,
- properties: {
- platformId: project.platformId,
- projectId: flowRun.projectId,
- edition: system.getEdition(),
- flowRunId: flowRun.id,
- flowId: flowRun.flowId,
- status: flowRun.status,
- environment: flowRun.environment,
- messages: usage.messages,
- toolCalls: usage.toolCalls,
- breakdown: usage.breakdown,
- },
- },
- })
- },
-})
-
-export const resolveAiCreditWeight = aiProviderUtils.resolveAiCreditWeight
-
-
-
-async function fetchSlice({ log, projectId, ref }: FetchSliceParams): Promise<unknown> {
- const file = await fileService(log).getDataOrUndefined({
- projectId,
- fileId: ref.fileId,
- type: FileType.FLOW_RUN_LOG_SLICE,
- })
- if (isNil(file)) {
- return undefined
- }
- return JSON.parse(file.data.toString('utf-8'))
-}
-
-type TrackParams = {
- flowRun: FlowRun
- flowVersion: FlowVersion
-}
-
-type FetchSliceParams = {
- log: FastifyBaseLogger
- projectId: string
- ref: LogSliceRef
-}
diff --git a/packages/server/api/src/app/flows/flow-run/flow-run-hooks.ts b/packages/server/api/src/app/flows/flow-run/flow-run-hooks.ts
index b097678167e..ae1398d64a1 100644
--- a/packages/server/api/src/app/flows/flow-run/flow-run-hooks.ts
+++ b/packages/server/api/src/app/flows/flow-run/flow-run-hooks.ts
@@ -8,7 +8,6 @@ import { system } from '../../helper/system/system'
import { billingProvider, CreditUsageSource, toFlowRunCreditProperties } from '../../platform/billing-provider'
import { projectService } from '../../project/project-service'
import { flowVersionService } from '../flow-version/flow-version.service'
-import { flowRunAiUsageTracker } from './flow-run-ai-usage-tracker'
const paidEditions = [ApEdition.CLOUD, ApEdition.ENTERPRISE].includes(system.getEdition())
export const flowRunHooks = (log: FastifyBaseLogger) => ({
@@ -44,13 +43,9 @@ export const flowRunHooks = (log: FastifyBaseLogger) => ({
})
}
}
- if (!paidEditions || isNil(flowVersion)) {
+ if (!paidEditions) {
return
}
- const { error } = await tryCatch(() => flowRunAiUsageTracker(log).track({ flowRun, flowVersion }))
- if (error) {
- log.warn({ error, flowRun: { id: flowRun.id } }, 'Failed to capture AI usage event')
- }
if (flowRun.environment === RunEnvironment.PRODUCTION && flowRun.status !== FlowRunStatus.QUOTA_EXCEEDED) {
const { error: creditError } = await tryCatch(() => trackProductionRunCredit(log, flowRun))
if (creditError) {
diff --git a/packages/server/api/src/app/platform/billing-and-telemetry.ts b/packages/server/api/src/app/platform/billing-and-telemetry.ts
index 63d7dff1322..f9199834730 100644
--- a/packages/server/api/src/app/platform/billing-and-telemetry.ts
+++ b/packages/server/api/src/app/platform/billing-and-telemetry.ts
@@ -6,9 +6,9 @@ import { billingProvider, TrackAppSumoAiUsageParams, TrackCreditsParams, TrackFe
export async function trackBillingAndSendTelemetry({ log, licenseKey, credits, appSumo, telemetry }: TrackBillingAndSendTelemetryParams): Promise<void> {
const provider = billingProvider.get(log)
- const creditsEvent: TrackFeatureParams = { ...credits, featureId: ConsumableFeatureId.AP_CREDITS }
+ const creditsEvent: TrackFeatureParams | undefined = isNil(credits) ? undefined : { ...credits, featureId: ConsumableFeatureId.AP_CREDITS }
const appSumoEvent: TrackFeatureParams | undefined = isNil(appSumo) ? undefined : { ...appSumo, featureId: ConsumableFeatureId.APP_SUMO_AI_CREDITS }
- const tracked = isNil(appSumoEvent) ? [creditsEvent] : [creditsEvent, appSumoEvent]
+ const tracked = [creditsEvent, appSumoEvent].filter((event): event is TrackFeatureParams => !isNil(event))
await Promise.all(tracked.map((params) => provider.trackFeature(params)))
if (isNil(licenseKey) || licenseKey.length === 0 || isNil(telemetry)) {
return
@@ -19,7 +19,7 @@ export async function trackBillingAndSendTelemetry({ log, licenseKey, credits, a
type TrackBillingAndSendTelemetryParams = {
log: FastifyBaseLogger
licenseKey: string | null | undefined
- credits: TrackCreditsParams
+ credits?: TrackCreditsParams
appSumo?: TrackAppSumoAiUsageParams
telemetry?: LicenseKeyEventPayload
}
diff --git a/packages/server/api/test/unit/app/ai/ai-usage-conversation-credits.test.ts b/packages/server/api/test/unit/app/ai/ai-usage-conversation-credits.test.ts
new file mode 100644
index 00000000000..5cd10230053
--- /dev/null
+++ b/packages/server/api/test/unit/app/ai/ai-usage-conversation-credits.test.ts
@@ -0,0 +1,97 @@
+import { ActivepiecesAiBillingScope, AIProviderName, apId } from '@activepieces/core-utils'
+import { ReportAiUsageRequest } from '@activepieces/shared'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+const addConversationCredits = vi.fn<(params: { conversationId: string, credits: number }) => Promise<void>>()
+const trackBillingAndSendTelemetry = vi.fn<() => Promise<void>>()
+
+vi.mock('../../../../src/app/helper/system/system', () => ({
+ system: { getDecimalOrThrow: () => 0.0005 },
+}))
+
+vi.mock('../../../../src/app/ee/platform/platform-plan/platform-plan.service', () => ({
+ platformPlanService: () => ({
+ getOrCreateForPlatform: async () => ({ plan: 'free', licenseKey: null }),
+ }),
+}))
+
+vi.mock('../../../../src/app/platform/billing-and-telemetry', () => ({
+ trackBillingAndSendTelemetry: (...args: unknown[]) => trackBillingAndSendTelemetry(...args as []),
+}))
+
+vi.mock('../../../../src/app/ai/ai-usage-hooks', () => ({
+ aiUsageHooks: { get: () => ({ addConversationCredits }) },
+}))
+
+const { aiUsageService } = await import('../../../../src/app/ai/ai-usage-service')
+
+const noopLogger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
+
+function reportOf(billing: ReportAiUsageRequest['billing']): ReportAiUsageRequest {
+ return {
+ billing,
+ provider: AIProviderName.ACTIVEPIECES,
+ modelId: 'anthropic/claude-sonnet-4.6',
+ idempotencyKey: apId(),
+ usage: { type: 'observed-cost', costUsd: 0.005 },
+ }
+}
+
+describe('aiUsageService.report — the conversation credit total', () => {
+ beforeEach(() => {
+ addConversationCredits.mockReset()
+ addConversationCredits.mockResolvedValue(undefined)
+ trackBillingAndSendTelemetry.mockReset()
+ trackBillingAndSendTelemetry.mockResolvedValue(undefined)
+ noopLogger.warn.mockReset()
+ })
+
+ it('adds what a conversation-scoped call cost to that conversation, so a run shows its own total', async () => {
+ await aiUsageService(noopLogger as never).report(reportOf({
+ scope: ActivepiecesAiBillingScope.CONVERSATION,
+ platformId: 'platform-1',
+ projectId: 'project-1',
+ conversationId: 'conversation-1',
+ }))
+
+ expect(addConversationCredits).toHaveBeenCalledWith({ conversationId: 'conversation-1', credits: 10 })
+ })
+
+ it('accumulates rather than replaces, so several calls in one turn all count', async () => {
+ const billing = {
+ scope: ActivepiecesAiBillingScope.CONVERSATION,
+ platformId: 'platform-1',
+ projectId: 'project-1',
+ conversationId: 'conversation-1',
+ } as const
+ await aiUsageService(noopLogger as never).report(reportOf(billing))
+ await aiUsageService(noopLogger as never).report(reportOf(billing))
+
+ expect(addConversationCredits).toHaveBeenCalledTimes(2)
+ expect(addConversationCredits).toHaveBeenLastCalledWith({ conversationId: 'conversation-1', credits: 10 })
+ })
+
+ it('leaves a flow run alone, since the total belongs to a conversation', async () => {
+ await aiUsageService(noopLogger as never).report(reportOf({
+ scope: ActivepiecesAiBillingScope.PROJECT,
+ platformId: 'platform-1',
+ projectId: 'project-1',
+ }))
+
+ expect(addConversationCredits).not.toHaveBeenCalled()
+ })
+
+ it('still bills when the total cannot be written, since the credit event is the money', async () => {
+ addConversationCredits.mockRejectedValue(new Error('conversation is gone'))
+
+ await aiUsageService(noopLogger as never).report(reportOf({
+ scope: ActivepiecesAiBillingScope.CONVERSATION,
+ platformId: 'platform-1',
+ projectId: 'project-1',
+ conversationId: 'conversation-1',
+ }))
+
+ expect(trackBillingAndSendTelemetry).toHaveBeenCalledTimes(1)
+ expect(noopLogger.warn).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/packages/server/api/test/unit/app/ee/agent/agent-draft-billing.test.ts b/packages/server/api/test/unit/app/ee/agent/agent-draft-billing.test.ts
index 67dc82450fe..ea3f98e8ce8 100644
--- a/packages/server/api/test/unit/app/ee/agent/agent-draft-billing.test.ts
+++ b/packages/server/api/test/unit/app/ee/agent/agent-draft-billing.test.ts
@@ -38,7 +38,7 @@ vi.mock('../../../../../src/app/ee/agent/agent-helpers', () => ({
resolveTierModel: mockResolveTierModel,
runScopeOrThrow: ({ projectId }: { projectId: string }) => ({ type: 'project', projectId }),
resolveChatProviderName: vi.fn().mockResolvedValue(AIProviderName.ACTIVEPIECES),
- resolveTier: vi.fn().mockReturnValue({ id: 'fast', creditWeight: 3 }),
+ resolveTier: vi.fn().mockReturnValue({ id: 'fast' }),
resolveModelIdForProvider: vi.fn().mockReturnValue('model-x'),
defaultModelIdForProvider: vi.fn().mockReturnValue('model-x'),
},
diff --git a/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts b/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts
index d883f4480f1..cad4c7fb51e 100644
--- a/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts
+++ b/packages/server/api/test/unit/app/ee/agent/agent-model-resolution.test.ts
@@ -139,17 +139,6 @@ describe('resolveModelIdForAnalytics', () => {
})
})
-describe('chatUsageTracker — a flow step is not billed as a chat message', () => {
- it('returns before doing any work, since the flow run meters its own AI usage', async () => {
- const { chatUsageTracker } = await import('../../../../../src/app/ee/agent/chat-usage-tracker')
- const log = { info: () => undefined, warn: () => undefined, error: () => undefined }
-
- await expect(chatUsageTracker(log as never).track({
- conversation: { id: 'conv-1', source: 'FLOW_STEP', platformId: 'plat-1', modelName: 'anthropic/claude-opus-4.6' } as never,
- })).resolves.toBeUndefined()
- })
-})
-
describe('runScopeOrThrow', () => {
it('scopes a run to its project', () => {
expect(agentHelpers.runScopeOrThrow({ projectId: 'proj-1' })).toEqual({ type: 'project', projectId: 'proj-1' })
diff --git a/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts b/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts
index 2f0c97a5bb8..91b2a348c72 100644
--- a/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts
+++ b/packages/server/api/test/unit/app/ee/agent/agent-rpc-handlers.test.ts
@@ -135,8 +135,8 @@ vi.mock('../../../../../src/app/ee/agent/chat-analytics-sync', () => ({
chatAnalyticsTelemetry: () => ({ sendConversationUpdate: mockSendConversationUpdate }),
}))
-vi.mock('../../../../../src/app/ee/agent/chat-usage-tracker', () => ({
- chatUsageTracker: () => ({ track: mockTrack }),
+vi.mock('../../../../../src/app/ee/agent/chat-tool-billing', () => ({
+ chatToolBilling: { chargeForLatestTurn: mockTrack },
}))
const noopLogger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
diff --git a/packages/server/api/test/unit/app/ee/agent/chat-usage-tracker.test.ts b/packages/server/api/test/unit/app/ee/agent/chat-usage-tracker.test.ts
deleted file mode 100644
index c5d07c65c47..00000000000
--- a/packages/server/api/test/unit/app/ee/agent/chat-usage-tracker.test.ts
+++ /dev/null
@@ -1,110 +0,0 @@
-import { AIProviderName } from '@activepieces/core-utils'
-import { PersistedAgentRole } from '@activepieces/shared'
-import { beforeEach, describe, expect, it, vi } from 'vitest'
-
-const { mockTrackBillableUsage, mockResolveMessages, mockCountBillableToolCalls, mockGetOrCreateForPlatform } = vi.hoisted(() => ({
- mockTrackBillableUsage: vi.fn().mockResolvedValue(undefined),
- mockResolveMessages: vi.fn(),
- mockCountBillableToolCalls: vi.fn().mockReturnValue(0),
- mockGetOrCreateForPlatform: vi.fn(),
-}))
-
-vi.mock('../../../../../src/app/helper/telemetry.utils', () => ({
- LicenseKeyPostHogEvents: { CHAT_MESSAGE: 'chat_message' },
- captureLicenseKeyEvent: vi.fn(),
-}))
-
-vi.mock('../../../../../src/app/platform/billing-provider', () => ({
- CreditUsageSource: { CHAT: 'chat' },
-}))
-
-vi.mock('../../../../../src/app/platform/billing-and-telemetry', () => ({
- trackBillingAndSendTelemetry: mockTrackBillableUsage,
-}))
-
-vi.mock('../../../../../src/app/ee/platform/platform-plan/platform-plan.service', () => ({
- platformPlanService: () => ({ getOrCreateForPlatform: mockGetOrCreateForPlatform }),
-}))
-
-vi.mock('../../../../../src/app/ee/agent/agent-helpers', () => ({
- agentHelpers: {
- resolveChatProviderName: vi.fn().mockResolvedValue(AIProviderName.ACTIVEPIECES),
- resolveModelIdForAnalytics: vi.fn().mockReturnValue('model-x'),
- resolveTier: vi.fn().mockReturnValue({ id: 'tier-1', creditWeight: 5 }),
- providerScopeFor: ({ projectId }: { projectId: string | null }) =>
- projectId === null ? { type: 'platform' } : { type: 'project', projectId },
- },
-}))
-
-vi.mock('../../../../../src/app/ee/agent/chat-tool-billing', () => ({
- chatToolBilling: { countBillableToolCallsInLatestTurn: mockCountBillableToolCalls },
-}))
-
-vi.mock('../../../../../src/app/ee/agent/history/agent-history', () => ({
- agentHistory: { resolveMessages: mockResolveMessages },
-}))
-
-const noopLogger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
-
-const conversation = {
- id: 'conv-1',
- platformId: 'plat-1',
- projectId: 'proj-1',
- userId: 'user-1',
- modelName: 'tier-1',
-}
-
-async function callTrack({ runId }: { runId?: string }): Promise<void> {
- const { chatUsageTracker } = await import('../../../../../src/app/ee/agent/chat-usage-tracker')
- await chatUsageTracker(noopLogger as never).track({ conversation: conversation as never, runId })
-}
-
-function creditsKeyFromLastCall(): string | undefined {
- return mockTrackBillableUsage.mock.calls[0][0].credits.idempotencyKey
-}
-
-function appSumoKeyFromLastCall(): string | undefined {
- return mockTrackBillableUsage.mock.calls[0][0].appSumo?.idempotencyKey
-}
-
-describe('chatUsageTracker.track — idempotency key scoping', () => {
- beforeEach(() => {
- mockTrackBillableUsage.mockClear()
- mockGetOrCreateForPlatform.mockResolvedValue({ plan: 'plus', licenseKey: null })
- mockResolveMessages.mockReturnValue([
- { role: PersistedAgentRole.USER },
- { role: PersistedAgentRole.ASSISTANT },
- { role: PersistedAgentRole.USER },
- { role: PersistedAgentRole.ASSISTANT },
- ])
- })
-
- it('scopes the key to the owning run so two overlapping runs cannot collide', async () => {
- await callTrack({ runId: 'run-9' })
-
- expect(creditsKeyFromLastCall()).toBe('conv-1:chat:run-9')
- })
-
- it('falls back to the turn index when no run id is supplied', async () => {
- await callTrack({ runId: undefined })
-
- expect(creditsKeyFromLastCall()).toBe('conv-1:chat:2')
- })
-
- it('a run-scoped key never equals the turn-index key it replaces', async () => {
- await callTrack({ runId: 'run-9' })
- const runScoped = creditsKeyFromLastCall()
- mockTrackBillableUsage.mockClear()
- await callTrack({ runId: undefined })
-
- expect(runScoped).not.toBe(creditsKeyFromLastCall())
- })
-
- it('scopes the AppSumo hard-cap key the same way', async () => {
- mockGetOrCreateForPlatform.mockResolvedValue({ plan: 'appsumo_activepieces_tier2', licenseKey: null })
-
- await callTrack({ runId: 'run-9' })
-
- expect(appSumoKeyFromLastCall()).toBe('conv-1:appSumoAi:run-9')
- })
-})
diff --git a/packages/server/api/test/unit/app/flows/flow-run/flow-run-ai-usage-extractor.test.ts b/packages/server/api/test/unit/app/flows/flow-run/flow-run-ai-usage-extractor.test.ts
deleted file mode 100644
index 752cd4fd7d4..00000000000
--- a/packages/server/api/test/unit/app/flows/flow-run/flow-run-ai-usage-extractor.test.ts
+++ /dev/null
@@ -1,694 +0,0 @@
-import {
- AI_PIECE_NAME,
- ContentBlockType,
- FlowActionType,
- FlowTriggerType,
- FlowVersion,
- StepOutput,
- StepOutputStatus,
- StepOutputType,
-} from '@activepieces/shared'
-import { beforeEach, describe, expect, it, vi } from 'vitest'
-import { flowRunAiUsageExtractor } from '../../../../../src/app/flows/flow-run/flow-run-ai-usage-extractor'
-import { sleep } from '../../../../../src/app/helper/sleep'
-
-vi.mock('../../../../../src/app/helper/sleep', () => ({
- sleep: vi.fn(async () => undefined),
-}))
-
-const RUN_AGENT = 'run_agent'
-const ASK_AI = 'askAi'
-
-function sleepCallCount(): number {
- return vi.mocked(sleep).mock.calls.length
-}
-
-beforeEach(() => {
- vi.mocked(sleep).mockClear()
-})
-
-function aiAction({ name, actionName, input }: { name: string, actionName: string, input?: Record<string, unknown> }) {
- return {
- name,
- type: FlowActionType.PIECE,
- settings: {
- pieceName: AI_PIECE_NAME,
- actionName,
- input: input ?? {},
- },
- nextAction: undefined,
- }
-}
-
-function loopAction({ name, firstLoopAction }: { name: string, firstLoopAction: Record<string, unknown> }) {
- return {
- name,
- type: FlowActionType.LOOP_ON_ITEMS,
- settings: { items: '{{ [] }}' },
- firstLoopAction,
- nextAction: undefined,
- }
-}
-
-function routerAction({ name, children }: { name: string, children: (Record<string, unknown> | null)[] }) {
- return {
- name,
- type: FlowActionType.ROUTER,
- settings: { branches: [] },
- children,
- nextAction: undefined,
- }
-}
-
-function pieceActionWithFailureBranch({ name, onFailure }: { name: string, onFailure: Record<string, unknown> }) {
- return {
- name,
- type: FlowActionType.PIECE,
- settings: { pieceName: '@activepieces/piece-store', actionName: 'put', input: {} },
- continueOnFailureBranches: { onFailure },
- nextAction: undefined,
- }
-}
-
-function succeededAskAi({ provider, model }: { provider: string, model: string }) {
- return {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider, model },
- }
-}
-
-function nonAiSteps(count: number): Record<string, unknown> {
- return Array.from({ length: count }, (_unused, index) => index).reduce<Record<string, unknown>>((record, index) => ({
- ...record,
- [`filler_${index}`]: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: {},
- output: 'noise',
- },
- }), {})
-}
-
-function flowVersionWith(actions: Record<string, unknown>[]): FlowVersion {
- const chained = actions.reduceRight<unknown>((next, action) => ({ ...action, nextAction: next }), undefined)
- return {
- trigger: {
- name: 'trigger',
- type: FlowTriggerType.EMPTY,
- settings: {},
- nextAction: chained,
- },
- } as unknown as FlowVersion
-}
-
-function toolCallBlock() {
- return { type: ContentBlockType.TOOL_CALL, toolName: 'store-put', toolCallId: 'id', status: 'completed', input: {} }
-}
-
-function markdownBlock() {
- return { type: ContentBlockType.MARKDOWN, markdown: 'hi' }
-}
-
-function steps(record: Record<string, unknown>): Record<string, StepOutput> {
- return record as Record<string, StepOutput>
-}
-
-const noopFetchSlice = vi.fn(async () => undefined)
-
-describe('flowRunAiUsageExtractor', () => {
- it('returns zero usage for a run with no AI steps', async () => {
- const flowVersion = flowVersionWith([])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({ trigger: { type: FlowTriggerType.EMPTY, status: StepOutputStatus.SUCCEEDED } }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({ messages: 0, toolCalls: 0, breakdown: [] })
- })
-
- it('counts a succeeded askAI step as one message with its provider/model', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'step_1', actionName: ASK_AI })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- step_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider: 'openai', model: 'gpt-4o' },
- output: 'answer',
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({
- messages: 1,
- toolCalls: 0,
- breakdown: [{ provider: 'openai', model: 'gpt-4o', messages: 1, toolCalls: 0 }],
- })
- })
-
- it('excludes a failed non-agent AI step', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'step_1', actionName: ASK_AI })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- step_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.FAILED,
- input: { provider: 'openai', model: 'gpt-4o' },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({ messages: 0, toolCalls: 0, breakdown: [] })
- })
-
- it('counts a run_agent step as message + tool calls', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'agent_1', actionName: RUN_AGENT })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- agent_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { aiProviderModel: { provider: 'anthropic', model: 'claude' } },
- output: { status: 'COMPLETED', steps: [markdownBlock(), toolCallBlock(), markdownBlock()] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({
- messages: 1,
- toolCalls: 1,
- breakdown: [{ provider: 'anthropic', model: 'claude', messages: 1, toolCalls: 1 }],
- })
- })
-
- it('counts a markdown-only run_agent as one message with zero tool calls', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'agent_1', actionName: RUN_AGENT })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- agent_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { aiProviderModel: { provider: 'anthropic', model: 'claude' } },
- output: { status: 'COMPLETED', steps: [markdownBlock()] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(1)
- expect(usage.toolCalls).toBe(0)
- })
-
- it('counts a FAILED run_agent that still made tool calls', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'agent_1', actionName: RUN_AGENT })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- agent_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.FAILED,
- input: { aiProviderModel: { provider: 'anthropic', model: 'claude' } },
- output: { status: 'FAILED', steps: [toolCallBlock(), toolCallBlock()] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({
- messages: 1,
- toolCalls: 2,
- breakdown: [{ provider: 'anthropic', model: 'claude', messages: 1, toolCalls: 2 }],
- })
- })
-
- it('does not count a run_agent that crashed with no output', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'agent_1', actionName: RUN_AGENT })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- agent_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.FAILED,
- input: { aiProviderModel: { provider: 'anthropic', model: 'claude' } },
- output: undefined,
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({ messages: 0, toolCalls: 0, breakdown: [] })
- })
-
- it('counts each loop iteration of an AI step', async () => {
- const flowVersion = flowVersionWith([loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'step_1', actionName: ASK_AI }) })])
- const iteration = {
- step_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider: 'openai', model: 'gpt-4o' },
- },
- }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: [iteration, iteration, iteration] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(3)
- expect(usage.breakdown).toEqual([{ provider: 'openai', model: 'gpt-4o', messages: 3, toolCalls: 0 }])
- })
-
- it('counts an AI step nested inside loops within loops', async () => {
- const flowVersion = flowVersionWith([
- loopAction({
- name: 'loop_1',
- firstLoopAction: loopAction({
- name: 'loop_2',
- firstLoopAction: aiAction({ name: 'step_1', actionName: ASK_AI }),
- }),
- }),
- ])
- const innerIteration = {
- step_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider: 'openai', model: 'gpt-4o' },
- },
- }
- const outerIteration = {
- loop_2: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: [innerIteration, innerIteration] },
- },
- }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: [outerIteration, outerIteration] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(4)
- expect(usage.breakdown).toEqual([{ provider: 'openai', model: 'gpt-4o', messages: 4, toolCalls: 0 }])
- })
-
- it('skips loops whose subtree has no AI step', async () => {
- const flowVersion = flowVersionWith([
- loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'step_1', actionName: ASK_AI }) }),
- ])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_2: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: {
- iterations: [{
- other_step: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider: 'openai', model: 'gpt-4o' },
- },
- }],
- },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({ messages: 0, toolCalls: 0, breakdown: [] })
- })
-
- it('counts every iteration of a large AI loop that crosses the event-loop yield threshold', async () => {
- const flowVersion = flowVersionWith([loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'step_1', actionName: ASK_AI }) })])
- const iteration = {
- step_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider: 'openai', model: 'gpt-4o' },
- },
- }
- const iterationCount = 2500
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: Array.from({ length: iterationCount }, () => iteration) },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(iterationCount)
- expect(usage.breakdown).toEqual([{ provider: 'openai', model: 'gpt-4o', messages: iterationCount, toolCalls: 0 }])
- })
-
- it('counts only the tested step in single-step test mode, ignoring seeded sample-data AI steps', async () => {
- const flowVersion = flowVersionWith([
- aiAction({ name: 'step_1', actionName: ASK_AI }),
- aiAction({ name: 'agent_1', actionName: RUN_AGENT }),
- ])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- // The tested step that actually executed this run.
- step_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider: 'openai', model: 'gpt-4o' },
- },
- // Seeded sample data from a prior test: SUCCEEDED with cached agent output, but not run now.
- agent_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { aiProviderModel: { provider: 'anthropic', model: 'claude' } },
- output: { status: 'COMPLETED', steps: [toolCallBlock(), toolCallBlock()] },
- },
- }),
- flowVersion,
- stepNameToTest: 'step_1',
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({
- messages: 1,
- toolCalls: 0,
- breakdown: [{ provider: 'openai', model: 'gpt-4o', messages: 1, toolCalls: 0 }],
- })
- })
-
- it('groups breakdown per provider/model across a multi-model run', async () => {
- const flowVersion = flowVersionWith([
- aiAction({ name: 'step_1', actionName: ASK_AI }),
- aiAction({ name: 'step_2', actionName: ASK_AI }),
- aiAction({ name: 'step_3', actionName: ASK_AI }),
- ])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- step_1: { type: FlowActionType.PIECE, status: StepOutputStatus.SUCCEEDED, input: { provider: 'openai', model: 'gpt-4o' } },
- step_2: { type: FlowActionType.PIECE, status: StepOutputStatus.SUCCEEDED, input: { provider: 'openai', model: 'gpt-4o' } },
- step_3: { type: FlowActionType.PIECE, status: StepOutputStatus.SUCCEEDED, input: { provider: 'anthropic', model: 'claude' } },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(3)
- expect(usage.breakdown).toEqual([
- { provider: 'openai', model: 'gpt-4o', messages: 2, toolCalls: 0 },
- { provider: 'anthropic', model: 'claude', messages: 1, toolCalls: 0 },
- ])
- })
-
- it('falls back to flow-version settings when the logged model is redacted', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'step_1', actionName: ASK_AI, input: { provider: 'openai', model: 'gpt-4o' } })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- step_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { provider: '**REDACTED**', model: '**REDACTED**' },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.breakdown).toEqual([{ provider: 'openai', model: 'gpt-4o', messages: 1, toolCalls: 0 }])
- })
-
- it('reports unknown provider/model when neither log nor settings has them', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'step_1', actionName: ASK_AI })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- step_1: { type: FlowActionType.PIECE, status: StepOutputStatus.SUCCEEDED, input: {} },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.breakdown).toEqual([{ provider: 'unknown', model: 'unknown', messages: 1, toolCalls: 0 }])
- })
-
- it('fetches the slice file for a sliced run_agent output and counts its tool calls', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'agent_1', actionName: RUN_AGENT })])
- const fetchSlice = vi.fn(async () => ({
- status: 'COMPLETED',
- steps: [toolCallBlock(), toolCallBlock(), markdownBlock()],
- }))
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- agent_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { aiProviderModel: { provider: 'anthropic', model: 'claude' } },
- outputType: StepOutputType.SLICE,
- output: { fileId: 'file-1', size: 99999, url: 'http://x' },
- },
- }),
- flowVersion,
- fetchSlice,
- })
- expect(fetchSlice).toHaveBeenCalledTimes(1)
- expect(usage).toEqual({
- messages: 1,
- toolCalls: 2,
- breakdown: [{ provider: 'anthropic', model: 'claude', messages: 1, toolCalls: 2 }],
- })
- })
-})
-
-describe('flowRunAiUsageExtractor · container shapes', () => {
- it('counts an AI step inside a router branch', async () => {
- const flowVersion = flowVersionWith([
- routerAction({ name: 'router_1', children: [aiAction({ name: 'step_1', actionName: ASK_AI }), null] }),
- ])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- router_1: { type: FlowActionType.ROUTER, status: StepOutputStatus.SUCCEEDED, output: { branches: [true, false] } },
- step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }),
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(1)
- expect(usage.breakdown).toEqual([{ provider: 'openai', model: 'gpt-4o', messages: 1, toolCalls: 0 }])
- })
-
- it('counts an AI step inside an on-failure branch', async () => {
- const flowVersion = flowVersionWith([
- pieceActionWithFailureBranch({ name: 'store_1', onFailure: aiAction({ name: 'step_1', actionName: ASK_AI }) }),
- ])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- store_1: { type: FlowActionType.PIECE, status: StepOutputStatus.FAILED, input: {} },
- step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }),
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(1)
- })
-
- it('counts an AI step inside a router nested in a loop', async () => {
- const flowVersion = flowVersionWith([
- loopAction({
- name: 'loop_1',
- firstLoopAction: routerAction({ name: 'router_1', children: [aiAction({ name: 'step_1', actionName: ASK_AI })] }),
- }),
- ])
- const iteration = {
- router_1: { type: FlowActionType.ROUTER, status: StepOutputStatus.SUCCEEDED },
- step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }),
- }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: [iteration, iteration] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(2)
- })
-
- it('counts two distinct AI steps in the same loop iteration', async () => {
- const flowVersion = flowVersionWith([
- loopAction({
- name: 'loop_1',
- firstLoopAction: {
- ...aiAction({ name: 'step_1', actionName: ASK_AI }),
- nextAction: aiAction({ name: 'step_2', actionName: ASK_AI }),
- },
- }),
- ])
- const iteration = {
- step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }),
- step_2: succeededAskAi({ provider: 'anthropic', model: 'claude' }),
- }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: [iteration, iteration, iteration] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(6)
- expect(usage.breakdown).toHaveLength(2)
- expect(usage.breakdown).toEqual(expect.arrayContaining([
- { provider: 'openai', model: 'gpt-4o', messages: 3, toolCalls: 0 },
- { provider: 'anthropic', model: 'claude', messages: 3, toolCalls: 0 },
- ]))
- })
-
- it('ignores non-AI steps sharing an iteration record with an AI step', async () => {
- const flowVersion = flowVersionWith([loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'step_1', actionName: ASK_AI }) })])
- const iteration = {
- ...nonAiSteps(9),
- step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }),
- }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: [iteration, iteration] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({
- messages: 2,
- toolCalls: 0,
- breakdown: [{ provider: 'openai', model: 'gpt-4o', messages: 2, toolCalls: 0 }],
- })
- })
-
- it('counts a top-level AI step and a loop AI step without double counting', async () => {
- const flowVersion = flowVersionWith([
- aiAction({ name: 'step_top', actionName: ASK_AI }),
- loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'step_nested', actionName: ASK_AI }) }),
- ])
- const iteration = { step_nested: succeededAskAi({ provider: 'anthropic', model: 'claude' }) }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- step_top: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }),
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: [iteration, iteration] },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(3)
- expect(usage.breakdown).toHaveLength(2)
- expect(usage.breakdown).toEqual(expect.arrayContaining([
- { provider: 'openai', model: 'gpt-4o', messages: 1, toolCalls: 0 },
- { provider: 'anthropic', model: 'claude', messages: 2, toolCalls: 0 },
- ]))
- })
-
- it('ignores an AI step present in the log but absent from the flow version', async () => {
- const flowVersion = flowVersionWith([aiAction({ name: 'step_1', actionName: ASK_AI })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }),
- step_deleted: succeededAskAi({ provider: 'anthropic', model: 'claude' }),
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage).toEqual({
- messages: 1,
- toolCalls: 0,
- breakdown: [{ provider: 'openai', model: 'gpt-4o', messages: 1, toolCalls: 0 }],
- })
- })
-
- it('counts only the succeeded iterations when one loop iteration failed', async () => {
- const flowVersion = flowVersionWith([loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'step_1', actionName: ASK_AI }) })])
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.FAILED,
- output: {
- iterations: [
- { step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }) },
- { step_1: { type: FlowActionType.PIECE, status: StepOutputStatus.FAILED, input: { provider: 'openai', model: 'gpt-4o' } } },
- ],
- },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(1)
- })
-
- it('fetches one slice per sliced agent iteration and counts every one', async () => {
- const iterationCount = 25
- const flowVersion = flowVersionWith([loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'agent_1', actionName: RUN_AGENT }) })])
- const fetchSlice = vi.fn(async () => ({ status: 'COMPLETED', steps: [toolCallBlock(), toolCallBlock()] }))
- const iteration = {
- agent_1: {
- type: FlowActionType.PIECE,
- status: StepOutputStatus.SUCCEEDED,
- input: { aiProviderModel: { provider: 'anthropic', model: 'claude' } },
- outputType: StepOutputType.SLICE,
- output: { fileId: 'file-1', size: 99999, url: 'http://x' },
- },
- }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: Array.from({ length: iterationCount }, () => iteration) },
- },
- }),
- flowVersion,
- fetchSlice,
- })
- expect(fetchSlice).toHaveBeenCalledTimes(iterationCount)
- expect(usage.messages).toBe(iterationCount)
- expect(usage.toolCalls).toBe(iterationCount * 2)
- })
-})
-
-describe('flowRunAiUsageExtractor · walk cost', () => {
- it('still yields the event loop while walking a large AI loop', async () => {
- const flowVersion = flowVersionWith([loopAction({ name: 'loop_1', firstLoopAction: aiAction({ name: 'step_1', actionName: ASK_AI }) })])
- const iteration = { step_1: succeededAskAi({ provider: 'openai', model: 'gpt-4o' }) }
- const usage = await flowRunAiUsageExtractor.extractAiUsage({
- steps: steps({
- loop_1: {
- type: FlowActionType.LOOP_ON_ITEMS,
- status: StepOutputStatus.SUCCEEDED,
- output: { iterations: Array.from({ length: 2500 }, () => iteration) },
- },
- }),
- flowVersion,
- fetchSlice: noopFetchSlice,
- })
- expect(usage.messages).toBe(2500)
- expect(sleepCallCount()).toBeGreaterThan(0)
- })
-})
diff --git a/packages/server/api/test/unit/app/flows/flow-run/flow-run-ai-usage-tracker.test.ts b/packages/server/api/test/unit/app/flows/flow-run/flow-run-ai-usage-tracker.test.ts
deleted file mode 100644
index 9884ea27ad0..00000000000
--- a/packages/server/api/test/unit/app/flows/flow-run/flow-run-ai-usage-tracker.test.ts
+++ /dev/null
@@ -1,161 +0,0 @@
-import { AIProviderName } from '@activepieces/core-utils'
-import { ACTIVEPIECES_CHAT_TIERS, DEFAULT_MANAGED_MODEL_WEIGHT } from '@activepieces/shared'
-import { resolveAiCreditWeight } from '../../../../../src/app/flows/flow-run/flow-run-ai-usage-tracker'
-import { beforeEach, describe, expect, it, vi } from 'vitest'
-
-const { mockTrackBillableUsage, mockExtractAiUsage, mockFlowVersionHasAiStep, mockGetOrCreateForPlatform, mockGetProject, mockGetStepsOrNull } = vi.hoisted(() => ({
- mockTrackBillableUsage: vi.fn().mockResolvedValue(undefined),
- mockExtractAiUsage: vi.fn(),
- mockFlowVersionHasAiStep: vi.fn().mockReturnValue(true),
- mockGetOrCreateForPlatform: vi.fn(),
- mockGetProject: vi.fn(),
- mockGetStepsOrNull: vi.fn(),
-}))
-
-vi.mock('../../../../../src/app/helper/telemetry.utils', () => ({
- LicenseKeyPostHogEvents: { AI_USAGE_PER_RUN: 'ai_usage_per_run' },
- captureLicenseKeyEvent: vi.fn(),
-}))
-
-vi.mock('../../../../../src/app/helper/system/system', () => ({
- system: { getEdition: vi.fn().mockReturnValue('cloud') },
-}))
-
-vi.mock('../../../../../src/app/platform/billing-provider', () => ({
- CreditUsageSource: { AI: 'ai' },
- toFlowRunCreditProperties: ({ platformId, flowRun }: { platformId: string, flowRun: { projectId: string, flowId: string, id: string, environment: string } }) => ({
- platformId,
- projectId: flowRun.projectId,
- flowId: flowRun.flowId,
- flowRunId: flowRun.id,
- environment: flowRun.environment,
- }),
-}))
-
-vi.mock('../../../../../src/app/platform/billing-and-telemetry', () => ({
- trackBillingAndSendTelemetry: mockTrackBillableUsage,
-}))
-
-vi.mock('../../../../../src/app/ee/platform/platform-plan/platform-plan.service', () => ({
- platformPlanService: () => ({ getOrCreateForPlatform: mockGetOrCreateForPlatform }),
-}))
-
-vi.mock('../../../../../src/app/project/project-service', () => ({
- projectService: () => ({ getOne: mockGetProject }),
-}))
-
-vi.mock('../../../../../src/app/file/file.service', () => ({
- fileService: () => ({ getDataOrUndefined: vi.fn() }),
-}))
-
-vi.mock('../../../../../src/app/flows/flow-run/flow-run-service', () => ({
- flowRunService: () => ({ getStepsOrNull: mockGetStepsOrNull }),
-}))
-
-vi.mock('../../../../../src/app/flows/flow-run/flow-run-ai-usage-extractor', () => ({
- flowRunAiUsageExtractor: {
- extractAiUsage: mockExtractAiUsage,
- flowVersionHasAiStep: mockFlowVersionHasAiStep,
- },
-}))
-
-const noopLogger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }
-
-const CREATED = '2026-07-01T00:00:00.000Z'
-const FIRST_ATTEMPT_START = '2026-07-01T00:00:05.000Z'
-const RETRY_ATTEMPT_START = '2026-07-02T09:30:00.000Z'
-
-async function callTrack({ startTime }: { startTime?: string | null }): Promise<void> {
- const { flowRunAiUsageTracker } = await import('../../../../../src/app/flows/flow-run/flow-run-ai-usage-tracker')
- const flowRun = {
- id: 'run-1',
- projectId: 'proj-1',
- flowId: 'flow-1',
- created: CREATED,
- startTime,
- environment: 'PRODUCTION',
- status: 'SUCCEEDED',
- }
- await flowRunAiUsageTracker(noopLogger as never).track({ flowRun: flowRun as never, flowVersion: {} as never })
-}
-
-function creditsKeyFromLastCall(): string | undefined {
- return mockTrackBillableUsage.mock.calls[0][0].credits.idempotencyKey
-}
-
-function appSumoKeyFromLastCall(): string | undefined {
- return mockTrackBillableUsage.mock.calls[0][0].appSumo?.idempotencyKey
-}
-
-describe('flowRunAiUsageTracker.track — idempotency key scoping', () => {
- beforeEach(() => {
- mockTrackBillableUsage.mockClear()
- mockGetProject.mockResolvedValue({ platformId: 'plat-1' })
- mockGetStepsOrNull.mockResolvedValue({})
- mockGetOrCreateForPlatform.mockResolvedValue({ plan: 'plus', licenseKey: null })
- mockExtractAiUsage.mockResolvedValue({
- messages: 1,
- toolCalls: 0,
- breakdown: [{ provider: AIProviderName.ACTIVEPIECES, model: 'anthropic/claude-haiku-4.5', messages: 1, toolCalls: 0 }],
- })
- })
-
- it('scopes the key to the attempt start so a retry is not swallowed as a duplicate', async () => {
- await callTrack({ startTime: FIRST_ATTEMPT_START })
-
- expect(creditsKeyFromLastCall()).toBe(`run-1:ai:${FIRST_ATTEMPT_START}`)
- })
-
- it('falls back to the run creation time when no start time is recorded', async () => {
- await callTrack({ startTime: null })
-
- expect(creditsKeyFromLastCall()).toBe(`run-1:ai:${CREATED}`)
- })
-
- it('gives a retried run a different key than its first attempt', async () => {
- await callTrack({ startTime: FIRST_ATTEMPT_START })
- const firstAttemptKey = creditsKeyFromLastCall()
- mockTrackBillableUsage.mockClear()
-
- await callTrack({ startTime: RETRY_ATTEMPT_START })
-
- expect(creditsKeyFromLastCall()).not.toBe(firstAttemptKey)
- })
-
- it('repeats the same key when the terminal update is redelivered for one attempt', async () => {
- await callTrack({ startTime: FIRST_ATTEMPT_START })
- const firstDeliveryKey = creditsKeyFromLastCall()
- mockTrackBillableUsage.mockClear()
-
- await callTrack({ startTime: FIRST_ATTEMPT_START })
-
- expect(creditsKeyFromLastCall()).toBe(firstDeliveryKey)
- })
-
- it('scopes the AppSumo hard-cap key the same way', async () => {
- mockGetOrCreateForPlatform.mockResolvedValue({ plan: 'appsumo_activepieces_tier2', licenseKey: null })
-
- await callTrack({ startTime: FIRST_ATTEMPT_START })
-
- expect(appSumoKeyFromLastCall()).toBe(`run-1:appSumoAi:${FIRST_ATTEMPT_START}`)
- })
-})
-
-describe('resolveAiCreditWeight', () => {
- it('bills each tier model at the tier weight, not the table', () => {
- for (const tier of ACTIVEPIECES_CHAT_TIERS) {
- expect(resolveAiCreditWeight({ provider: AIProviderName.ACTIVEPIECES, model: tier.modelId }), tier.id).toBe(tier.creditWeight)
- }
- })
-
- it('charges more for a frontier model than for a mini one, so the table is not uniformly the default', () => {
- const frontier = resolveAiCreditWeight({ provider: AIProviderName.ACTIVEPIECES, model: 'openai/gpt-5.5' })
- const mini = resolveAiCreditWeight({ provider: AIProviderName.ACTIVEPIECES, model: 'openai/gpt-5.4-mini' })
-
- expect(frontier).toBeGreaterThan(mini)
- })
-
- it('leaves a model nobody offers on the default rate', () => {
- expect(resolveAiCreditWeight({ provider: AIProviderName.ACTIVEPIECES, model: 'someone/never-offered' })).toBe(DEFAULT_MANAGED_MODEL_WEIGHT)
- })
-})
diff --git a/packages/server/utils/src/activepieces-ai-cost.ts b/packages/server/utils/src/activepieces-ai-cost.ts
index 91e9487dfea..00e6b00de1e 100644
--- a/packages/server/utils/src/activepieces-ai-cost.ts
+++ b/packages/server/utils/src/activepieces-ai-cost.ts
@@ -1,12 +1,15 @@
-import { AIProviderName, isNil, ActivepiecesAiBilling, ActivepiecesAiCall, ActivepiecesAiCostReporter } from '@activepieces/core-utils'
+import { ActivepiecesAiBilling, ActivepiecesAiCall, ActivepiecesAiCostReporter, AIProviderName, isNil, spreadIfDefined } from '@activepieces/core-utils'
import { isJSONObject, LanguageModelV4GenerateResult, LanguageModelV4StreamPart, SharedV4ProviderMetadata } from '@ai-sdk/provider'
import { LanguageModel, wrapLanguageModel } from 'ai'
import { z } from 'zod'
-export function wrapActivepiecesLanguageModel({ model, provider, modelId, billing }: WrapActivepiecesLanguageModelParams): LanguageModel {
- if (provider !== AIProviderName.ACTIVEPIECES || typeof model === 'string') {
+export function billedLanguageModel({ model, provider, modelId, billing, ownKeyCredit = 'per-call' }: BilledLanguageModelParams): LanguageModel {
+ if (isNil(billing) || typeof model === 'string') {
return model
}
+ if (provider !== AIProviderName.ACTIVEPIECES) {
+ return ownKeyCredit === 'per-call' ? billedAtAFlatRate({ model, provider, modelId, billing }) : model
+ }
return wrapLanguageModel({
model,
middleware: {
@@ -23,8 +26,33 @@ export function wrapActivepiecesLanguageModel({ model, provider, modelId, billin
})
}
+function billedAtAFlatRate({ model, provider, modelId, billing }: BilledAtAFlatRateParams): LanguageModel {
+ return wrapLanguageModel({
+ model,
+ middleware: {
+ wrapGenerate: async ({ doGenerate }) => {
+ const result = await doGenerate()
+ reportFlatCredits({ billing, provider, modelId, generationId: result.response?.id })
+ return result
+ },
+ wrapStream: async ({ doStream }) => {
+ const { stream, ...rest } = await doStream()
+ return { ...rest, stream: stream.pipeThrough(flatCreditObserver({ billing, provider, modelId })) }
+ },
+ },
+ })
+}
+
+export function reportFlatCredits({ billing, provider, modelId, generationId }: ReportFlatCreditsParams): void {
+ if (isNil(reporter)) {
+ unreportedCalls += 1
+ return
+ }
+ reporter({ billing, provider, modelId, call: { charge: 'flat-credits', credits: FLAT_CREDITS_PER_MODEL_CALL, ...spreadIfDefined('generationId', generationId) } })
+}
+
export function observedEmbeddingFetch({ provider, modelId, billing, wrapped }: ObservedEmbeddingFetchParams): typeof globalThis.fetch | undefined {
- if (provider !== AIProviderName.ACTIVEPIECES) {
+ if (provider !== AIProviderName.ACTIVEPIECES || isNil(billing)) {
return wrapped
}
const send = wrapped ?? fetch
@@ -43,12 +71,14 @@ function toGeneratedCall(result: LanguageModelV4GenerateResult): ActivepiecesAiC
function streamCostObserver({ billing, provider, modelId }: ObserverParams): TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart> {
let generationId: string | undefined
+ let reported = false
return new TransformStream({
transform: (chunk, controller) => {
if (chunk.type === 'response-metadata') {
generationId = chunk.id ?? generationId
}
- if (chunk.type === 'finish') {
+ if (chunk.type === 'finish' && !reported) {
+ reported = true
reportUsage({ billing, provider, modelId, call: toCall({ generationId, providerMetadata: chunk.providerMetadata }) })
}
controller.enqueue(chunk)
@@ -56,12 +86,30 @@ function streamCostObserver({ billing, provider, modelId }: ObserverParams): Tra
})
}
+function flatCreditObserver({ billing, provider, modelId }: ObserverParams): TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart> {
+ let generationId: string | undefined
+ let reported = false
+ return new TransformStream({
+ transform: (chunk, controller) => {
+ if (chunk.type === 'response-metadata') {
+ generationId = chunk.id ?? generationId
+ }
+ if (chunk.type === 'finish' && !reported) {
+ reported = true
+ reportFlatCredits({ billing, provider, modelId, generationId })
+ }
+ controller.enqueue(chunk)
+ },
+ })
+}
+
function toCall({ generationId, providerMetadata }: { generationId: string | undefined, providerMetadata: SharedV4ProviderMetadata | undefined }): ActivepiecesAiCall | undefined {
const usage = openRouterUsageOf(providerMetadata)
if (isNil(generationId) || isNil(usage) || isNil(usage.cost)) {
return undefined
}
return {
+ charge: 'observed-cost',
generationId,
costUsd: usage.cost,
...(isNil(usage.promptTokens) ? {} : { inputTokens: usage.promptTokens }),
@@ -90,6 +138,7 @@ async function readEmbeddingCall(response: Response): Promise<ActivepiecesAiCall
return undefined
}
return {
+ charge: 'observed-cost',
generationId: body.id,
costUsd: body.usage.cost,
...(isNil(body.usage.prompt_tokens) ? {} : { inputTokens: body.usage.prompt_tokens }),
@@ -103,6 +152,7 @@ function parseOrUndefined<T extends z.ZodType>(schema: T, value: unknown): z.inf
function reportUsage({ billing, provider, modelId, call }: ReportUsageParams): void {
if (isNil(call)) {
+ unreportedCalls += 1
return
}
if (isNil(reporter)) {
@@ -129,37 +179,51 @@ const EmbeddingCostResponse = z.object({
let reporter: ActivepiecesAiCostReporter | undefined
let unreportedCalls = 0
+const FLAT_CREDITS_PER_MODEL_CALL = 1
+
export const activepiecesAiCost = {
setReporter: (next: ActivepiecesAiCostReporter): void => {
reporter = next
},
- hasReporter: (): boolean => !isNil(reporter),
- assertReporterInstalled: (): void => {
- if (isNil(reporter)) {
- throw new Error('No Activepieces AI cost reporter was installed, so every call on the Activepieces AI provider would run unbilled')
- }
- },
unreportedCallCount: (): number => unreportedCalls,
- wrapActivepiecesLanguageModel,
+ billedLanguageModel,
observedEmbeddingFetch,
+ reportFlatCredits,
}
type OpenRouterUsage = z.infer<typeof OpenRouterUsage>
-type WrapActivepiecesLanguageModelParams = {
+type BilledLanguageModelParams = {
model: LanguageModel
provider: AIProviderName
modelId: string
- billing: ActivepiecesAiBilling
+ billing: ActivepiecesAiBilling | undefined
+ ownKeyCredit?: OwnKeyCredit
}
+export type OwnKeyCredit = 'per-call' | 'charged-with-the-turn'
+
type ObservedEmbeddingFetchParams = {
provider: AIProviderName
modelId: string
- billing: ActivepiecesAiBilling
+ billing: ActivepiecesAiBilling | undefined
wrapped: typeof globalThis.fetch | undefined
}
+type BilledAtAFlatRateParams = {
+ model: Exclude<LanguageModel, string>
+ provider: AIProviderName
+ modelId: string
+ billing: ActivepiecesAiBilling
+}
+
+type ReportFlatCreditsParams = {
+ billing: ActivepiecesAiBilling
+ provider: AIProviderName
+ modelId: string
+ generationId?: string
+}
+
type ObserverParams = {
billing: ActivepiecesAiBilling
provider: AIProviderName
diff --git a/packages/server/utils/src/ai-utils.ts b/packages/server/utils/src/ai-utils.ts
index 44b10bb3063..c2ec61f4274 100644
--- a/packages/server/utils/src/ai-utils.ts
+++ b/packages/server/utils/src/ai-utils.ts
@@ -1,4 +1,4 @@
-import { AIProviderName, isNil, observedProviderFetch, ProviderOutcomeReporter, spreadIfDefined } from '@activepieces/core-utils';
+import { ActivepiecesAiBilling, AIProviderName, isNil, observedProviderFetch, ProviderOutcomeReporter, spreadIfDefined } from '@activepieces/core-utils';
import { CloudflareGatewayMetadata, createCloudflareGatewayModel, createImageModel, createLanguageModel } from '@activepieces/ai-providers';
import { AI_PROVIDER_CAPABILITIES, AIWebSearchMode, BaseAIProviderAuthConfig, getEffectiveProviderAndModel } from '@activepieces/shared';
import { createAnthropic } from '@ai-sdk/anthropic'
@@ -8,10 +8,12 @@ import { createOpenAI } from '@ai-sdk/openai'
import { SharedV3ProviderOptions } from '@ai-sdk/provider'
import { createOpenRouter, OpenRouterChatSettings } from '@openrouter/ai-sdk-provider'
import { EmbeddingModel, ImageModel, LanguageModel, ToolSet } from 'ai'
+import { billedLanguageModel, OwnKeyCredit } from './activepieces-ai-cost'
const DEFAULT_WEB_SEARCH_RESULTS = 5
const MIN_OPENROUTER_WEB_SEARCH_RESULTS = 1
const MAX_OPENROUTER_WEB_SEARCH_RESULTS = 10
+const REPORT_WHAT_THE_CALL_COST = { usage: { include: true } } as const
const NATIVE_WEB_SEARCH_TOOLS: Record<string, (params: NativeWebSearchToolParams) => ToolSet> = {
[AIProviderName.ANTHROPIC]: ({ auth: { apiKey }, options }) => ({
@@ -98,27 +100,51 @@ function openRouterModelSettings({ provider, webSearchEnabled, options }: {
webSearchEnabled: boolean
options?: WebSearchOptions
}): OpenRouterChatSettings | undefined {
- if (!webSearchEnabled || AI_PROVIDER_CAPABILITIES[provider].webSearch !== 'plugin') {
- return undefined
+ const settings: OpenRouterChatSettings = {
+ ...(provider === AIProviderName.ACTIVEPIECES ? REPORT_WHAT_THE_CALL_COST : {}),
+ ...(webSearchEnabled && AI_PROVIDER_CAPABILITIES[provider].webSearch === 'plugin'
+ ? { plugins: [{ id: 'web' as const, max_results: openRouterWebSearchResults(options) }] }
+ : {}),
}
- const maxResults = Math.min(
+ return Object.keys(settings).length === 0 ? undefined : settings
+}
+
+function openRouterWebSearchResults(options?: WebSearchOptions): number {
+ return Math.min(
Math.max(options?.maxUses ?? DEFAULT_WEB_SEARCH_RESULTS, MIN_OPENROUTER_WEB_SEARCH_RESULTS),
MAX_OPENROUTER_WEB_SEARCH_RESULTS,
)
- return { plugins: [{ id: 'web', max_results: maxResults }] }
}
-function createModel({ provider, auth, config, modelId, metadata, flowStep, openaiResponsesModel = false, webSearchEnabled = false, webSearchOptions, onOutcome }: {
+function createModel({ provider, auth, config, modelId, metadata, flowStep, billing, ownKeyCredit, openaiResponsesModel = false, webSearchEnabled = false, webSearchOptions, onOutcome }: {
provider: AIProviderName
auth: Record<string, unknown>
config: Record<string, unknown>
modelId: string
metadata?: ChatModelMetadata
flowStep?: FlowStepMetadata
+ billing?: ActivepiecesAiBilling
+ ownKeyCredit?: OwnKeyCredit
openaiResponsesModel?: boolean
webSearchEnabled?: boolean
webSearchOptions?: WebSearchOptions
onOutcome?: ProviderOutcomeReporter
+}): LanguageModel {
+ const model = buildModel({ provider, auth, config, modelId, metadata, flowStep, openaiResponsesModel, webSearchEnabled, webSearchOptions, onOutcome })
+ return billedLanguageModel({ model, provider, modelId, billing, ...spreadIfDefined('ownKeyCredit', ownKeyCredit) })
+}
+
+function buildModel({ provider, auth, config, modelId, metadata, flowStep, openaiResponsesModel, webSearchEnabled, webSearchOptions, onOutcome }: {
+ provider: AIProviderName
+ auth: Record<string, unknown>
+ config: Record<string, unknown>
+ modelId: string
+ metadata?: ChatModelMetadata
+ flowStep?: FlowStepMetadata
+ openaiResponsesModel: boolean
+ webSearchEnabled: boolean
+ webSearchOptions?: WebSearchOptions
+ onOutcome?: ProviderOutcomeReporter
}): LanguageModel {
if (provider === AIProviderName.CLOUDFLARE_GATEWAY) {
return createCloudflareGatewayModel({
diff --git a/packages/server/utils/test/activepieces-ai-cost.test.ts b/packages/server/utils/test/activepieces-ai-cost.test.ts
new file mode 100644
index 00000000000..800fd81fb3a
--- /dev/null
+++ b/packages/server/utils/test/activepieces-ai-cost.test.ts
@@ -0,0 +1,152 @@
+import { ActivepiecesAiBilling, ActivepiecesAiBillingScope, ActivepiecesAiCostEvent, AIProviderName } from '@activepieces/core-utils'
+import { LanguageModelV4 } from '@ai-sdk/provider'
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { activepiecesAiCost } from '../src/activepieces-ai-cost'
+
+const BILLING: ActivepiecesAiBilling = {
+ scope: ActivepiecesAiBillingScope.PROJECT,
+ platformId: 'platform-1',
+ projectId: 'project-1',
+}
+
+const reported: ActivepiecesAiCostEvent[] = []
+
+function modelReturning(result: Record<string, unknown>): LanguageModelV4 {
+ return {
+ specificationVersion: 'v4',
+ provider: 'test',
+ modelId: 'test-model',
+ supportedUrls: {},
+ doGenerate: async () => result as unknown as Awaited<ReturnType<LanguageModelV4['doGenerate']>>,
+ doStream: async () => ({ stream: new ReadableStream() }),
+ }
+}
+
+async function generateWith(model: ReturnType<typeof activepiecesAiCost.billedLanguageModel>): Promise<void> {
+ if (typeof model === 'string') {
+ throw new Error('expected a model object')
+ }
+ await model.doGenerate({ prompt: [] })
+}
+
+beforeEach(() => {
+ reported.length = 0
+ activepiecesAiCost.setReporter((event) => reported.push(event))
+})
+
+afterEach(() => {
+ activepiecesAiCost.setReporter(() => undefined)
+})
+
+describe('what a model call reports back for billing', () => {
+ it('bills a managed call on the dollar cost OpenRouter reports', async () => {
+ const model = activepiecesAiCost.billedLanguageModel({
+ model: modelReturning({
+ content: [],
+ finishReason: 'stop',
+ usage: {},
+ response: { id: 'gen-abc' },
+ providerMetadata: { openrouter: { usage: { cost: 0.0042, promptTokens: 100, completionTokens: 20 } } },
+ }),
+ provider: AIProviderName.ACTIVEPIECES,
+ modelId: 'anthropic/claude-sonnet-5',
+ billing: BILLING,
+ })
+
+ await generateWith(model)
+
+ expect(reported).toHaveLength(1)
+ expect(reported[0].call).toEqual({
+ charge: 'observed-cost',
+ generationId: 'gen-abc',
+ costUsd: 0.0042,
+ inputTokens: 100,
+ outputTokens: 20,
+ })
+ })
+
+ it('bills a call on the customer own key at one flat credit, whatever it cost us', async () => {
+ const model = activepiecesAiCost.billedLanguageModel({
+ model: modelReturning({ content: [], finishReason: 'stop', usage: {}, response: { id: 'resp-1' } }),
+ provider: AIProviderName.OPENAI,
+ modelId: 'gpt-5',
+ billing: BILLING,
+ })
+
+ await generateWith(model)
+
+ expect(reported).toHaveLength(1)
+ expect(reported[0].call).toEqual({ charge: 'flat-credits', credits: 1, generationId: 'resp-1' })
+ })
+
+ it('counts a managed call whose cost never arrived, instead of quietly billing nothing', async () => {
+ const before = activepiecesAiCost.unreportedCallCount()
+ const model = activepiecesAiCost.billedLanguageModel({
+ model: modelReturning({ content: [], finishReason: 'stop', usage: {}, response: { id: 'gen-xyz' } }),
+ provider: AIProviderName.ACTIVEPIECES,
+ modelId: 'anthropic/claude-sonnet-5',
+ billing: BILLING,
+ })
+
+ await generateWith(model)
+
+ expect(reported).toHaveLength(0)
+ expect(activepiecesAiCost.unreportedCallCount()).toBe(before + 1)
+ })
+
+ it('leaves a model alone when nothing is being billed for it', async () => {
+ const raw = modelReturning({ content: [], finishReason: 'stop', usage: {} })
+ const model = activepiecesAiCost.billedLanguageModel({
+ model: raw,
+ provider: AIProviderName.ACTIVEPIECES,
+ modelId: 'anthropic/claude-sonnet-5',
+ billing: undefined,
+ })
+
+ expect(model).toBe(raw)
+ await generateWith(model)
+ expect(reported).toHaveLength(0)
+ })
+
+ it('leaves a customer own-key call unbilled when its credit is charged once for the whole turn', async () => {
+ const model = activepiecesAiCost.billedLanguageModel({
+ model: modelReturning({ content: [], finishReason: 'stop', usage: {}, response: { id: 'resp-2' } }),
+ provider: AIProviderName.OPENAI,
+ modelId: 'gpt-5',
+ billing: BILLING,
+ ownKeyCredit: 'charged-with-the-turn',
+ })
+
+ await generateWith(model)
+
+ expect(reported).toHaveLength(0)
+ })
+
+ it('still bills a managed call on cost when the turn carries the own-key credit, since we pay per call either way', async () => {
+ const model = activepiecesAiCost.billedLanguageModel({
+ model: modelReturning({
+ content: [],
+ finishReason: 'stop',
+ usage: {},
+ response: { id: 'gen-def' },
+ providerMetadata: { openrouter: { usage: { cost: 0.001 } } },
+ }),
+ provider: AIProviderName.ACTIVEPIECES,
+ modelId: 'anthropic/claude-sonnet-5',
+ billing: BILLING,
+ ownKeyCredit: 'charged-with-the-turn',
+ })
+
+ await generateWith(model)
+
+ expect(reported).toHaveLength(1)
+ expect(reported[0].call).toEqual({ charge: 'observed-cost', generationId: 'gen-def', costUsd: 0.001 })
+ })
+
+ it('bills an image model call that cannot be wrapped at the same flat credit', () => {
+ activepiecesAiCost.reportFlatCredits({ billing: BILLING, provider: AIProviderName.OPENAI, modelId: 'dall-e-3' })
+
+ expect(reported).toHaveLength(1)
+ expect(reported[0].call).toEqual({ charge: 'flat-credits', credits: 1 })
+ })
+})
diff --git a/packages/server/utils/test/ai-utils.test.ts b/packages/server/utils/test/ai-utils.test.ts
index 0c438da1328..35e0cd87a81 100644
--- a/packages/server/utils/test/ai-utils.test.ts
+++ b/packages/server/utils/test/ai-utils.test.ts
@@ -147,3 +147,25 @@ describe('aiUtils.createModelForImages', () => {
})).toBeUndefined()
})
})
+
+describe('what the managed provider is asked to send back', () => {
+ function settingsFor({ provider, webSearchEnabled = false }: { provider: AIProviderName, webSearchEnabled?: boolean }): Record<string, unknown> | undefined {
+ const model = aiUtils.createModel({ provider, auth: { apiKey: 'key' }, config: {}, modelId: 'anthropic/claude-sonnet-5', webSearchEnabled })
+ return (model as unknown as { settings?: Record<string, unknown> }).settings
+ }
+
+ it('asks OpenRouter to report what a managed call cost, which is the whole basis of the bill', () => {
+ expect(settingsFor({ provider: AIProviderName.ACTIVEPIECES })).toMatchObject({ usage: { include: true } })
+ })
+
+ it('keeps asking for the cost when web search is on, rather than letting the plugin setting replace it', () => {
+ expect(settingsFor({ provider: AIProviderName.ACTIVEPIECES, webSearchEnabled: true })).toMatchObject({
+ usage: { include: true },
+ plugins: [{ id: 'web', max_results: 5 }],
+ })
+ })
+
+ it('does not ask for cost accounting on a customer own OpenRouter key, which we never pay for', () => {
+ expect(settingsFor({ provider: AIProviderName.OPENROUTER })).toEqual({})
+ })
+})
diff --git a/packages/server/worker/src/lib/execute/jobs/ai/ai-cost-reporter.ts b/packages/server/worker/src/lib/execute/jobs/ai/ai-cost-reporter.ts
index 55616eb1f54..8fa7957bbc9 100644
--- a/packages/server/worker/src/lib/execute/jobs/ai/ai-cost-reporter.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ai/ai-cost-reporter.ts
@@ -1,7 +1,8 @@
-import { apId, isNil, tryCatch } from '@activepieces/core-utils'
-import { activepiecesAiCost } from '@activepieces/server-utils'
-import { WorkerToApiContract } from '@activepieces/shared'
+import { ActivepiecesAiBilling, ActivepiecesAiBillingScope, ActivepiecesAiCall, apId, isNil, spreadIfDefined, tryCatch, tryCatchSync } from '@activepieces/core-utils'
+import { activepiecesAiCost, onCallService } from '@activepieces/server-utils'
+import { AiUsageCharge, AiUsageChatContext, AiUsageFlowRunContext, WorkerToApiContract } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
+import { workerSettings } from '../../../config/worker-settings'
export function installAiCostReporter({ apiClient, log }: {
apiClient: WorkerToApiContract
@@ -13,13 +14,53 @@ export function installAiCostReporter({ apiClient, log }: {
provider,
modelId,
idempotencyKey: `ai:${apId()}`,
- usage: { type: 'observed-cost', costUsd: call.costUsd },
- generationId: call.generationId,
+ usage: chargeFor(call),
+ ...spreadIfDefined('generationId', call.generationId),
+ ...spreadIfDefined('flowRun', flowRunOf(billing)),
+ ...spreadIfDefined('chat', chatOf(billing)),
})).then(({ error }) => {
if (isNil(error)) {
return
}
- log.error({ error, provider, model: modelId, generationId: call.generationId }, '[aiCostReporter] A managed AI call went unbilled')
+ log.error({ error, provider, model: modelId, generationId: call.generationId }, '[aiCostReporter] An AI call went unbilled')
})
})
+ startUnbilledCallWatch(log)
}
+
+function startUnbilledCallWatch(log: FastifyBaseLogger): void {
+ let lastSeen = 0
+ const timer = setInterval(() => {
+ const unbilled = activepiecesAiCost.unreportedCallCount()
+ if (unbilled <= lastSeen) {
+ return
+ }
+ const since = unbilled - lastSeen
+ lastSeen = unbilled
+ log.error({ unbilledCallCount: unbilled, sinceLastCheck: since }, '[aiCostReporter] AI calls ran without a charge to bill')
+ const { data: settings } = tryCatchSync(() => workerSettings.getSettings())
+ void onCallService(log, settings?.PAGE_ONCALL_WEBHOOK).page({
+ code: 'AI_CALLS_WENT_UNBILLED',
+ message: 'AI calls ran without a charge to bill. Either a managed call reported no cost, or a call was made before the cost reporter was installed.',
+ params: { unbilledCallCount: unbilled, sinceLastCheck: since },
+ })
+ }, UNBILLED_CHECK_INTERVAL_MS)
+ timer.unref()
+}
+
+function flowRunOf(billing: ActivepiecesAiBilling): AiUsageFlowRunContext | undefined {
+ return billing.scope === ActivepiecesAiBillingScope.PROJECT ? billing.flowRun : undefined
+}
+
+function chatOf(billing: ActivepiecesAiBilling): AiUsageChatContext | undefined {
+ return billing.scope === ActivepiecesAiBillingScope.CONVERSATION ? billing.chat : undefined
+}
+
+function chargeFor(call: ActivepiecesAiCall): AiUsageCharge {
+ if (call.charge === 'flat-credits') {
+ return { type: 'flat-credits', credits: call.credits }
+ }
+ return { type: 'observed-cost', costUsd: call.costUsd }
+}
+
+const UNBILLED_CHECK_INTERVAL_MS = 5 * 60 * 1000
diff --git a/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts b/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts
index 6408385cfde..afb54d8c69f 100644
--- a/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ai/execute-ai.ts
@@ -1,4 +1,4 @@
-import { AIProviderName, isNil, spreadIfDefined, tryCatch } from '@activepieces/core-utils'
+import { ActivepiecesAiBilling, ActivepiecesAiBillingScope, AIProviderName, isNil, spreadIfDefined, tryCatch } from '@activepieces/core-utils'
import { aiUtils, FlowStepMetadata } from '@activepieces/server-utils'
import { AiStepAction, ClassifyTextJobData, EngineResponseStatus, ExecuteAiJobData, getEffectiveProviderAndModel, ResolveAiProviderResponse, WorkerJobType } from '@activepieces/shared'
import { generateText, ModelMessage, stepCountIs } from 'ai'
@@ -58,19 +58,20 @@ async function callTheModel({ ctx, data }: { ctx: JobContext, data: ExecuteAiJob
...spreadIfDefined('providerConfigId', data.providerConfigId),
})
const flowStep = flowStepMetadata(data)
+ const billing = billingFor(data)
switch (data.action) {
case AiStepAction.EXTRACT_STRUCTURED_DATA:
- return { answer: await extractStructuredData({ data, resolved, flowStep, files: await resolveAiFiles({ ctx, data }) }) }
+ return { answer: await extractStructuredData({ data, resolved, flowStep, billing, files: await resolveAiFiles({ ctx, data }) }) }
case AiStepAction.GENERATE_IMAGE:
- return { answer: await generateImageStep({ ctx, data, resolved, flowStep, inputImages: await resolveAiFiles({ ctx, data }) }) }
+ return { answer: await generateImageStep({ ctx, data, resolved, flowStep, billing, inputImages: await resolveAiFiles({ ctx, data }) }) }
case AiStepAction.ASK_AI:
case AiStepAction.SUMMARIZE_TEXT:
case AiStepAction.CLASSIFY_TEXT:
- return runTextStep({ data, resolved, flowStep })
+ return runTextStep({ data, resolved, flowStep, billing })
}
}
-async function runTextStep({ data, resolved, flowStep }: { data: ExecuteAiJobData, resolved: ResolveAiProviderResponse, flowStep: FlowStepMetadata }): Promise<unknown> {
+async function runTextStep({ data, resolved, flowStep, billing }: { data: ExecuteAiJobData, resolved: ResolveAiProviderResponse, flowStep: FlowStepMetadata, billing: ActivepiecesAiBilling }): Promise<unknown> {
const { provider, auth, config } = resolved
const webSearchEnabled = data.webSearch?.enabled ?? false
const webSearchOptions = data.webSearch?.options
@@ -82,6 +83,7 @@ async function runTextStep({ data, resolved, flowStep }: { data: ExecuteAiJobDat
config,
modelId: data.modelId,
flowStep,
+ billing,
openaiResponsesModel: webSearchEnabled && (effectiveProvider ?? provider) === AIProviderName.OPENAI,
webSearchEnabled,
webSearchOptions,
@@ -100,6 +102,15 @@ async function runTextStep({ data, resolved, flowStep }: { data: ExecuteAiJobDat
return toStepOutput({ data, text: response.text ?? '', sources: response.sources })
}
+function billingFor(data: ExecuteAiJobData): ActivepiecesAiBilling {
+ return {
+ scope: ActivepiecesAiBillingScope.PROJECT,
+ platformId: data.platformId,
+ projectId: data.projectId,
+ flowRun: { flowId: data.flowId, flowRunId: data.flowRunId },
+ }
+}
+
function flowStepMetadata(data: ExecuteAiJobData): FlowStepMetadata {
return {
projectId: data.projectId,
diff --git a/packages/server/worker/src/lib/execute/jobs/ai/extract-structured-data.ts b/packages/server/worker/src/lib/execute/jobs/ai/extract-structured-data.ts
index 99a6e6f0b17..32adef7c701 100644
--- a/packages/server/worker/src/lib/execute/jobs/ai/extract-structured-data.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ai/extract-structured-data.ts
@@ -1,13 +1,14 @@
-import { isNil, spreadIfDefined } from '@activepieces/core-utils'
+import { ActivepiecesAiBilling, isNil, spreadIfDefined } from '@activepieces/core-utils'
import { aiUtils, FlowStepMetadata } from '@activepieces/server-utils'
import { ExtractStructuredDataJobData, ResolveAiProviderResponse } from '@activepieces/shared'
import { generateText, jsonSchema, ModelMessage, tool, UserModelMessage } from 'ai'
import { ResolvedAiFile } from './ai-files'
-export async function extractStructuredData({ data, resolved, flowStep, files }: {
+export async function extractStructuredData({ data, resolved, flowStep, billing, files }: {
data: ExtractStructuredDataJobData
resolved: ResolveAiProviderResponse
flowStep: FlowStepMetadata
+ billing: ActivepiecesAiBilling
files: ResolvedAiFile[]
}): Promise<unknown> {
const model = aiUtils.createModel({
@@ -16,6 +17,7 @@ export async function extractStructuredData({ data, resolved, flowStep, files }:
config: resolved.config,
modelId: data.modelId,
flowStep,
+ billing,
})
const { schemaDefinition, sanitizedNameMap } = buildSchema(data)
const extractionTool = tool({
diff --git a/packages/server/worker/src/lib/execute/jobs/ai/generate-image.ts b/packages/server/worker/src/lib/execute/jobs/ai/generate-image.ts
index 3cb15121ca7..c3e5705976d 100644
--- a/packages/server/worker/src/lib/execute/jobs/ai/generate-image.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ai/generate-image.ts
@@ -1,18 +1,19 @@
-import { AIProviderName, isNil } from '@activepieces/core-utils'
-import { aiUtils, FlowStepMetadata } from '@activepieces/server-utils'
+import { ActivepiecesAiBilling, AIProviderName, isNil } from '@activepieces/core-utils'
+import { activepiecesAiCost, aiUtils, FlowStepMetadata } from '@activepieces/server-utils'
import { AI_PROVIDER_CAPABILITIES, GenerateImageJobData, getEffectiveProviderAndModel, ResolveAiProviderResponse } from '@activepieces/shared'
import { generateImage, generateText, ImageModel, ImagePart, LanguageModel } from 'ai'
import { JobContext } from '../../types'
import { ResolvedAiFile } from './ai-files'
-export async function generateImageStep({ ctx, data, resolved, flowStep, inputImages }: {
+export async function generateImageStep({ ctx, data, resolved, flowStep, billing, inputImages }: {
ctx: JobContext
data: GenerateImageJobData
resolved: ResolveAiProviderResponse
flowStep: FlowStepMetadata
+ billing: ActivepiecesAiBilling
inputImages: ResolvedAiFile[]
}): Promise<string> {
- const image = await getGeneratedImage({ data, resolved, inputImages, flowStep })
+ const image = await getGeneratedImage({ data, resolved, inputImages, flowStep, billing })
const imageData = !isNil(image.base64) && image.base64.length > 0
? Buffer.from(image.base64, 'base64')
: Buffer.from(image.uint8Array)
@@ -26,13 +27,14 @@ export async function generateImageStep({ ctx, data, resolved, flowStep, inputIm
return url
}
-async function getGeneratedImage({ data, resolved, inputImages, flowStep }: {
+async function getGeneratedImage({ data, resolved, inputImages, flowStep, billing }: {
data: GenerateImageJobData
resolved: ResolveAiProviderResponse
inputImages: ResolvedAiFile[]
flowStep: FlowStepMetadata
+ billing: ActivepiecesAiBilling
}): Promise<GeneratedImage> {
- const model = createImageCapableModel({ resolved, modelId: data.modelId, flowStep })
+ const model = createImageCapableModel({ resolved, modelId: data.modelId, flowStep, billing })
const { provider: effectiveProvider } = getEffectiveProviderAndModel({ provider: resolved.provider, model: data.modelId })
const resolvedProvider = effectiveProvider ?? resolved.provider
const prompt = data.prompt ?? ''
@@ -55,14 +57,16 @@ async function getGeneratedImage({ data, resolved, inputImages, flowStep }: {
: prompt,
providerOptions: { [resolvedProvider]: { ...stripLegacyImageField(data.advancedOptions) } } as Parameters<typeof generateImage>[0]['providerOptions'],
})
+ activepiecesAiCost.reportFlatCredits({ billing, provider: resolved.provider, modelId: data.modelId })
return image
})
}
-function createImageCapableModel({ resolved, modelId, flowStep }: {
+function createImageCapableModel({ resolved, modelId, flowStep, billing }: {
resolved: ResolveAiProviderResponse
modelId: string
flowStep: FlowStepMetadata
+ billing: ActivepiecesAiBilling
}): ImageCapableModel {
if (!AI_PROVIDER_CAPABILITIES[resolved.provider].supportsImageGeneration) {
throw new Error(`Provider ${resolved.provider} does not support image models`)
@@ -72,7 +76,7 @@ function createImageCapableModel({ resolved, modelId, flowStep }: {
if (!isNil(imageModel)) {
return { kind: 'image', model: imageModel }
}
- return { kind: 'language', model: aiUtils.createModel({ provider, auth, config, modelId, flowStep }) }
+ return { kind: 'language', model: aiUtils.createModel({ provider, auth, config, modelId, flowStep, billing }) }
}
async function generateImageUsingGenerateText({ model, prompt, inputImages }: {
diff --git a/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts b/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts
index 93cf70418e0..740a0c233e2 100644
--- a/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts
+++ b/packages/server/worker/src/lib/execute/jobs/ee/agent/execute-agent-run.ts
@@ -1,6 +1,6 @@
-import { AIProviderName, ErrorCode, formatPieceError, isNil, isObject, spreadIfDefined, tryCatch, tryCatchSync } from '@activepieces/core-utils'
+import { ActivepiecesAiBilling, ActivepiecesAiBillingScope, AIProviderName, ErrorCode, formatPieceError, isNil, isObject, spreadIfDefined, tryCatch, tryCatchSync } from '@activepieces/core-utils'
import { agentAiUtils, aiUtils } from '@activepieces/server-utils'
-import { AgentEvent, AgentEventType, AgentKnowledgeBaseTool, AgentMcpTool, AgentOutputField, AgentPhase, AgentPieceTool, AgentResult, AgentRunSource, AgentTool, AgentToolType, aiProviderUtils, EngineResponseStatus, ExecuteAgentRunJobData, MAX_AGENT_TURN_WALL_CLOCK_MS, PersistedAgentMessage, PersistedAgentPart, PersistedAgentPartType, PersistedAgentRole, ResolvedAgentFlowTool, WorkerJobType } from '@activepieces/shared'
+import { AgentEvent, AgentEventType, AgentKnowledgeBaseTool, AgentMcpTool, AgentOutputField, AgentPhase, AgentPieceTool, AgentResult, AgentRunSource, AgentTool, AgentToolType, EngineResponseStatus, ExecuteAgentRunJobData, MAX_AGENT_TURN_WALL_CLOCK_MS, PersistedAgentMessage, PersistedAgentPart, PersistedAgentRole, ResolvedAgentFlowTool, WorkerJobType } from '@activepieces/shared'
import { createUIMessageStream, generateText, ModelMessage, streamText, ToolSet, toUIMessageStream } from 'ai'
import { FireAndForgetJobResult, JobContext, JobHandler, JobResultKind } from '../../../types'
import { agentMcpClient, McpConnection } from './agent-mcp-client'
@@ -118,13 +118,24 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge
&& !tavilySearchActive
&& !untrackedSearchWouldBeat
&& aiUtils.supportsWebSearch(provider)
+ const billing: ActivepiecesAiBilling = {
+ scope: ActivepiecesAiBillingScope.CONVERSATION,
+ platformId,
+ projectId: projectId ?? null,
+ conversationId,
+ chat: { userId, turnIndex: config.previousUiMessages.length, tier: config.tier.id },
+ }
const model = aiUtils.createModel({
provider, auth: config.auth, config: config.providerConfig, modelId: config.modelId,
metadata: { platformId, conversationId, runId },
webSearchEnabled: webSearchActive,
+ billing,
+ ownKeyCredit: 'charged-with-the-turn',
})
const fastModel = aiUtils.createModel({
provider, auth: config.auth, config: config.providerConfig, modelId: config.fastModelId,
+ billing,
+ ownKeyCredit: 'charged-with-the-turn',
})
log.info({ provider, model: { id: config.modelId }, tier: { id: config.tier.id }, dryRun: dryRun ?? false, tavilySearchActive, webSearchActive }, '[executeAgentRun] Chat config loaded')
@@ -336,11 +347,6 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge
}, 'Chat message completed')
const thinkingDurationMs = Date.now() - thinkingStartTime
- const billedAsAiCredits = source === AgentRunSource.FLOW_STEP
- const toolCallsThisTurn = uiParts.filter((part) => part.type === PersistedAgentPartType.TOOL_CALL).length
- const aiCredits = billedAsAiCredits
- ? aiProviderUtils.resolveAiCreditWeight({ provider: config.provider, model: config.modelId }) + toolCallsThisTurn
- : undefined
const savePayload = {
conversationId,
runId,
@@ -351,7 +357,6 @@ export const executeAgentRunJob: JobHandler<ExecuteAgentRunJobData, FireAndForge
],
...spreadIfDefined('title', autoTitle),
...spreadIfDefined('modelName', isNil(data.modelName) ? config.tier.id : undefined),
- ...spreadIfDefined('aiCredits', aiCredits),
}
await retryWithBackoff({ fn: () => ctx.apiClient.saveAgentMessages(savePayload), description: 'Saving the transcript', throwOnExhausted: true, log })
diff --git a/packages/web/public/locales/ar/translation.json b/packages/web/public/locales/ar/translation.json
index 35ec18a19a3..2f29b83babe 100644
--- a/packages/web/public/locales/ar/translation.json
+++ b/packages/web/public/locales/ar/translation.json
@@ -1189,5 +1189,7 @@
"Select a project role": "اختر دور المشروع",
"Steps in this flow": "الخطوات في هذا التدفق",
"Invalid Access": "Invalid Access",
- "Either the project does not exist or you do not have access to it.": "Either the project does not exist or you do not have access to it."
+ "Either the project does not exist or you do not have access to it.": "Either the project does not exist or you do not have access to it.",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/de/translation.json b/packages/web/public/locales/de/translation.json
index e785d496d4f..1f4e34d6b25 100644
--- a/packages/web/public/locales/de/translation.json
+++ b/packages/web/public/locales/de/translation.json
@@ -711,8 +711,7 @@
"Voice input": "Spracheingabe",
"Drop files here": "Dateien hier ablegen",
"Release to add files to your message": "Loslassen, um Dateien zu Ihrer Nachricht hinzuzufügen",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "Abgelaufen",
"Using this {name} account": "Benutze dieses {name} Konto",
"Which {name} account should I use?": "Welcher {name} Account soll ich verwenden?",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "Vor 1 Minute",
"{count} mins ago": "{count} Minuten her",
"1 hour ago": "Vor 1 Stunde",
- "{count} hours ago": "{count} Stunden vor"
+ "{count} hours ago": "{count} Stunden vor",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/en/translation.json b/packages/web/public/locales/en/translation.json
index b8f1ea8adc1..4711ceb6271 100644
--- a/packages/web/public/locales/en/translation.json
+++ b/packages/web/public/locales/en/translation.json
@@ -867,8 +867,7 @@
"Voice input": "Voice input",
"Drop files here": "Drop files here",
"Release to add files to your message": "Release to add files to your message",
- "{count, plural, =1 {1 credit} other {# credits}}": "{count, plural, =1 {1 credit} other {# credits}}",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.",
"Expired": "Expired",
"Using this {name} account": "Using this {name} account",
"Which {name} account should I use?": "Which {name} account should I use?",
@@ -2140,14 +2139,14 @@
"see below": "see below",
"Agent/Chat": "Agent/Chat",
"Agent": "Agent",
- "sum of tools use + model cost per message": "sum of tools use + model cost per message",
+ "sum of tools use + what each message costs": "sum of tools use + what each message costs",
"AI Steps (per call)": "AI Steps (per call)",
"BYOK": "BYOK",
"Bring Your Own Key — flat execution cost, no model markup": "Bring Your Own Key — flat execution cost, no model markup",
"What are credits?": "What are credits?",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.",
"How are credits consumed?": "How are credits consumed?",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "Each action in Activepieces has a fixed credit cost. Here's a breakdown:",
+ "Here's what each action in Activepieces costs:": "Here's what each action in Activepieces costs:",
"What is BYOK?": "What is BYOK?",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.",
"Do unused credits carry over?": "Do unused credits carry over?",
@@ -2887,5 +2886,6 @@
"No runs match these filters": "No runs match these filters",
"The input and output were not kept for this call.": "The input and output were not kept for this call.",
"The input and output are no longer available.": "The input and output are no longer available.",
- "Too large to keep in full — some of it was dropped.": "Too large to keep in full — some of it was dropped."
+ "Too large to keep in full — some of it was dropped.": "Too large to keep in full — some of it was dropped.",
+ "what the call costs": "what the call costs"
}
diff --git a/packages/web/public/locales/es/translation.json b/packages/web/public/locales/es/translation.json
index 0f4b879f3f3..bea648afb37 100644
--- a/packages/web/public/locales/es/translation.json
+++ b/packages/web/public/locales/es/translation.json
@@ -711,8 +711,7 @@
"Voice input": "Entrada de voz",
"Drop files here": "Soltar archivos aquí",
"Release to add files to your message": "Suelta para añadir archivos a tu mensaje",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "Caducó",
"Using this {name} account": "Usando esta cuenta {name}",
"Which {name} account should I use?": "¿Qué cuenta de {name} debo usar?",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "Hace 1 min",
"{count} mins ago": "Hace {count} minuto(s)",
"1 hour ago": "Hace 1 hora",
- "{count} hours ago": "Hace {count} hora(s)"
+ "{count} hours ago": "Hace {count} hora(s)",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/fr/translation.json b/packages/web/public/locales/fr/translation.json
index 2a7ea013c09..e7084f5b758 100644
--- a/packages/web/public/locales/fr/translation.json
+++ b/packages/web/public/locales/fr/translation.json
@@ -711,8 +711,7 @@
"Voice input": "Saisie vocale",
"Drop files here": "Déposer des fichiers ici",
"Release to add files to your message": "Relâchez pour ajouter des fichiers à votre message",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "Expiré",
"Using this {name} account": "Utiliser ce compte {name}",
"Which {name} account should I use?": "Quel compte {name} dois-je utiliser ?",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "Il y a 1 min",
"{count} mins ago": "Il y a {count} mins",
"1 hour ago": "Il y a 1 heure",
- "{count} hours ago": "Il y a {count} heures"
+ "{count} hours ago": "Il y a {count} heures",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/ja/translation.json b/packages/web/public/locales/ja/translation.json
index ac8efa6530d..5599d578b76 100644
--- a/packages/web/public/locales/ja/translation.json
+++ b/packages/web/public/locales/ja/translation.json
@@ -711,8 +711,7 @@
"Voice input": "音声入力",
"Drop files here": "ここにファイルをドロップ",
"Release to add files to your message": "メッセージにファイルを追加するには、離してください",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "期限切れ",
"Using this {name} account": "この {name} アカウントを使用する",
"Which {name} account should I use?": "どの {name} アカウントを使用すればいいですか?",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "1分前",
"{count} mins ago": "{count} 分前",
"1 hour ago": "1時間前",
- "{count} hours ago": "{count} 時間前"
+ "{count} hours ago": "{count} 時間前",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/nl/translation.json b/packages/web/public/locales/nl/translation.json
index 83f483f32df..b2190630f5e 100644
--- a/packages/web/public/locales/nl/translation.json
+++ b/packages/web/public/locales/nl/translation.json
@@ -711,8 +711,7 @@
"Voice input": "Spraak-invoer",
"Drop files here": "Sleep bestanden hier naartoe",
"Release to add files to your message": "Loslaten om bestanden aan je bericht toe te voegen",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "Verlopen",
"Using this {name} account": "Dit {name} account wordt gebruikt",
"Which {name} account should I use?": "Welk {name} account moet ik gebruiken?",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "1 min geleden",
"{count} mins ago": "{count} min geleden",
"1 hour ago": "1 uur geleden",
- "{count} hours ago": "{count} uur geleden"
+ "{count} hours ago": "{count} uur geleden",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/pt/translation.json b/packages/web/public/locales/pt/translation.json
index 3fe1d91e3db..3aaca821da3 100644
--- a/packages/web/public/locales/pt/translation.json
+++ b/packages/web/public/locales/pt/translation.json
@@ -711,8 +711,7 @@
"Voice input": "Entrada por voz",
"Drop files here": "Solte os arquivos aqui",
"Release to add files to your message": "Solte para adicionar arquivos em sua mensagem",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "Expirado",
"Using this {name} account": "Usando esta conta {name}",
"Which {name} account should I use?": "Qual conta {name} devo usar?",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "1 min atrás",
"{count} mins ago": "{count} min atrás",
"1 hour ago": "1 hora atrás",
- "{count} hours ago": "{count} horas atrás"
+ "{count} hours ago": "{count} horas atrás",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/ru/translation.json b/packages/web/public/locales/ru/translation.json
index bf400221f76..2fcef610b76 100644
--- a/packages/web/public/locales/ru/translation.json
+++ b/packages/web/public/locales/ru/translation.json
@@ -1340,5 +1340,7 @@
"License activated successfully!": "Лицензия успешно активирована!",
"Activation failed, invalid license key": "Активация не удалась, неверный лицензионный ключ",
"Invalid Access": "Неправильный доступ",
- "Either the project does not exist or you do not have access to it.": "Проект не существует или у вас нет доступа к нему."
+ "Either the project does not exist or you do not have access to it.": "Проект не существует или у вас нет доступа к нему.",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/zh-TW/translation.json b/packages/web/public/locales/zh-TW/translation.json
index 1e98c108be6..85e896ec846 100644
--- a/packages/web/public/locales/zh-TW/translation.json
+++ b/packages/web/public/locales/zh-TW/translation.json
@@ -711,8 +711,7 @@
"Voice input": "",
"Drop files here": "",
"Release to add files to your message": "",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "",
"Using this {name} account": "",
"Which {name} account should I use?": "",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "",
"{count} mins ago": "",
"1 hour ago": "",
- "{count} hours ago": ""
+ "{count} hours ago": "",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/public/locales/zh/translation.json b/packages/web/public/locales/zh/translation.json
index 66b36bb894a..6db227343d9 100644
--- a/packages/web/public/locales/zh/translation.json
+++ b/packages/web/public/locales/zh/translation.json
@@ -711,8 +711,7 @@
"Voice input": "Voice input",
"Drop files here": "Drop files here",
"Release to add files to your message": "Release to add files to your message",
- "{count, plural, =1 {1 credit} other {# credits}}": "",
- "Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
+ "Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.": "",
"Expired": "已过期",
"Using this {name} account": "Using this {name} account",
"Which {name} account should I use?": "Which {name} account should I use?",
@@ -1882,14 +1881,14 @@
"see below": "",
"Agent/Chat": "",
"Agent": "",
- "sum of tools use + model cost per message": "",
+ "sum of tools use + what each message costs": "",
"AI Steps (per call)": "",
"BYOK": "",
"Bring Your Own Key — flat execution cost, no model markup": "",
"What are credits?": "",
"Credits are the unit used to measure your usage on Activepieces. Every action you run consumes a certain number of credits depending on what it does.": "",
"How are credits consumed?": "",
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:": "",
+ "Here's what each action in Activepieces costs:": "",
"What is BYOK?": "",
"BYOK stands for Bring Your Own Key. You connect your own AI provider API key, and Activepieces charges only 1 credit per call instead of the full model cost.": "",
"Do unused credits carry over?": "",
@@ -2295,5 +2294,7 @@
"1 min ago": "1 分鐘前",
"{count} mins ago": "{count} 分鐘前",
"1 hour ago": "1 小時前",
- "{count} hours ago": "{count} 小時前"
+ "{count} hours ago": "{count} 小時前",
+ "Activepieces AI": "",
+ "what the call costs": ""
}
diff --git a/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsx b/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsx
index 2b8cd08b74b..33cab94657e 100644
--- a/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsx
+++ b/packages/web/src/app/routes/chat-with-ai/components/chat-model-selector.tsx
@@ -58,7 +58,6 @@ function useModelOptions(): ModelOption[] {
id: tier.id,
...TIER_CONFIG[tier.id],
displayLabel: tier.label,
- creditWeight: tier.creditWeight,
}));
}
return curatedModels.map((model) => ({
@@ -66,7 +65,6 @@ function useModelOptions(): ModelOption[] {
icon: Sparkles,
displayLabel: model.label,
description: null,
- creditWeight: null,
}));
}
@@ -168,14 +166,6 @@ export function ChatModelSelector({
<span className="text-sm font-medium">
{t(option.displayLabel)}
</span>
- {showCredits && !isNil(option.creditWeight) && (
- <span className="text-xs text-muted-foreground">
- {t(
- '{count, plural, =1 {1 credit} other {# credits}}',
- { count: option.creditWeight },
- )}
- </span>
- )}
</div>
{option.description && (
<span className="text-xs text-muted-foreground">
@@ -196,7 +186,7 @@ export function ChatModelSelector({
{showCredits && (
<div className="border-t px-3 py-2 text-xs text-muted-foreground">
{t(
- 'Per message, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.',
+ 'Credits are charged on what each message actually costs, plus {count, plural, =1 {1 credit} other {# credits}} per tool call.',
{ count: CHAT_CREDITS_PER_TOOL_CALL },
)}
</div>
@@ -229,5 +219,4 @@ type ModelOption = {
icon: React.ComponentType<{ className?: string }>;
displayLabel: string;
description: string | null;
- creditWeight: number | null;
};
diff --git a/packages/web/src/features/billing/components/feature-usage/credits-info-dialog.tsx b/packages/web/src/features/billing/components/feature-usage/credits-info-dialog.tsx
index c4d0d1d7262..dd31c914edb 100644
--- a/packages/web/src/features/billing/components/feature-usage/credits-info-dialog.tsx
+++ b/packages/web/src/features/billing/components/feature-usage/credits-info-dialog.tsx
@@ -1,8 +1,4 @@
-import {
- ACTIVEPIECES_CHAT_TIERS,
- ApEdition,
- ApFlagId,
-} from '@activepieces/shared';
+import { ApEdition, ApFlagId } from '@activepieces/shared';
import { t } from 'i18next';
import { ArrowUpRight, Coins } from 'lucide-react';
import React from 'react';
@@ -165,17 +161,19 @@ function buildCostItems({
{
kind: 'row',
action: chatEnabled ? t('Agent/Chat') : t('Agent'),
- sub: t('sum of tools use + model cost per message'),
+ sub: t('sum of tools use + what each message costs'),
credits: t('see below'),
},
];
const activepiecesModels: CostItem[] = includeActivepiecesModels
- ? ACTIVEPIECES_CHAT_TIERS.map((tier) => ({
- kind: 'row',
- action: t(tier.label),
- sub: modelByActivepieces,
- credits: String(tier.creditWeight),
- }))
+ ? [
+ {
+ kind: 'row',
+ action: t('Activepieces AI'),
+ sub: modelByActivepieces,
+ credits: t('what the call costs'),
+ },
+ ]
: [];
const ai: CostItem[] = [
{ kind: 'section', label: t('AI Steps (per call)') },
@@ -208,11 +206,7 @@ function buildFaqs({
question: t('How are credits consumed?'),
answer: (
<div className="flex flex-col gap-2.5">
- <span>
- {t(
- "Each action in Activepieces has a fixed credit cost. Here's a breakdown:",
- )}
- </span>
+ <span>{t("Here's what each action in Activepieces costs:")}</span>
<CreditsCostTable
includeActivepiecesModels={isCloud}
chatEnabled={chatEnabled}