:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): bill and quota-limit push sends (#71464)
Daniel Marchuk6368299075a5
diff --git a/ee/billing/billing_manager.py b/ee/billing/billing_manager.pyindex 435135a2c130..b7d073a4cfa0 100644--- a/ee/billing/billing_manager.py+++ b/ee/billing/billing_manager.py@@ -456,6 +456,7 @@ class BillingManager:signals_credits=usage_summary.get("signals_credits", {}),posthog_code_credits=usage_summary.get("posthog_code_credits", {}),workflow_emails=usage_summary.get("workflow_emails", {}),+ workflow_push=usage_summary.get("workflow_push", {}),workflow_destinations_dispatched=usage_summary.get("workflow_destinations_dispatched", {}),logs_mb_ingested=usage_summary.get("logs_mb_ingested", {}),replay_vision_credits=usage_summary.get("replay_vision_credits", {}),diff --git a/ee/billing/quota_limiting.py b/ee/billing/quota_limiting.pyindex 34614f5e778e..5bac20480482 100644--- a/ee/billing/quota_limiting.py+++ b/ee/billing/quota_limiting.py@@ -44,6 +44,7 @@ from posthog.tasks.usage_report import (get_teams_with_survey_responses_count_in_period,get_teams_with_workflow_billable_invocations_in_period,get_teams_with_workflow_emails_sent_in_period,+ get_teams_with_workflow_push_sent_in_period,)from posthog.utils import get_current_day@@ -89,6 +90,7 @@ class QuotaResource(Enum):SIGNALS_CREDITS = "signals_credits"POSTHOG_CODE_CREDITS = "posthog_code_credits"WORKFLOW_EMAILS = "workflow_emails"+ WORKFLOW_PUSH = "workflow_push"WORKFLOW_DESTINATIONS = "workflow_destinations_dispatched"LOGS_MB_INGESTED = "logs_mb_ingested"REPLAY_VISION_CREDITS = "replay_vision_credits"@@ -114,6 +116,7 @@ OVERAGE_BUFFER = {QuotaResource.SIGNALS_CREDITS: 0,QuotaResource.POSTHOG_CODE_CREDITS: 0,QuotaResource.WORKFLOW_EMAILS: 0,+ QuotaResource.WORKFLOW_PUSH: 0,QuotaResource.WORKFLOW_DESTINATIONS: 0,QuotaResource.LOGS_MB_INGESTED: 0,QuotaResource.REPLAY_VISION_CREDITS: 0,@@ -144,6 +147,7 @@ class UsageCounters(TypedDict):signals_credits: intposthog_code_credits: intworkflow_emails: int+ workflow_push: intworkflow_destinations_dispatched: intlogs_mb_ingested: intreplay_vision_credits: int@@ -972,6 +976,9 @@ def update_all_orgs_billing_quotas("teams_with_workflow_emails_sent_in_period": convert_team_usage_rows_to_dict(_timed_query("workflow_emails", get_teams_with_workflow_emails_sent_in_period, period_start, period_end)),+ "teams_with_workflow_push_sent_in_period": convert_team_usage_rows_to_dict(+ _timed_query("workflow_push", get_teams_with_workflow_push_sent_in_period, period_start, period_end)+ ),"teams_with_workflow_destinations_in_period": convert_team_usage_rows_to_dict(_timed_query("workflow_invocations", get_teams_with_workflow_billable_invocations_in_period, period_start, period_end@@ -1044,6 +1051,7 @@ def update_all_orgs_billing_quotas(cdp_trigger_events=all_data["teams_with_cdp_trigger_events_metrics"].get(team.id, 0),rows_exported=all_data["teams_with_rows_exported_in_period"].get(team.id, 0),workflow_emails=all_data["teams_with_workflow_emails_sent_in_period"].get(team.id, 0),+ workflow_push=all_data["teams_with_workflow_push_sent_in_period"].get(team.id, 0),workflow_destinations_dispatched=all_data["teams_with_workflow_destinations_in_period"].get(team.id, 0),logs_mb_ingested=all_data["teams_with_logs_mb_in_period"].get(team.id, 0),replay_vision_credits=all_data["teams_with_replay_vision_credits_used_in_period"].get(team.id, 0),diff --git a/ee/billing/test/test_billing_manager.py b/ee/billing/test/test_billing_manager.pyindex b02dc3532de0..b4060f296883 100644--- a/ee/billing/test/test_billing_manager.py+++ b/ee/billing/test/test_billing_manager.py@@ -249,6 +249,7 @@ class TestBillingManager(BaseTest):"replay_vision_credits": {},"posthog_code_credits": {},"workflow_emails": {"usage": 100, "limit": 10000, "todays_usage": 10},+ "workflow_push": {},"workflow_destinations_dispatched": {"usage": 50, "limit": 10000, "todays_usage": 5},"logs_mb_ingested": {"usage": 5500, "limit": 50000, "todays_usage": 500},"period": ["2024-01-01T00:00:00Z", "2024-01-31T23:59:59Z"],diff --git a/ee/billing/test/test_quota_limiting.py b/ee/billing/test/test_quota_limiting.pyindex d1bcb7a97b06..eb533075a998 100644--- a/ee/billing/test/test_quota_limiting.py+++ b/ee/billing/test/test_quota_limiting.py@@ -2313,6 +2313,7 @@ def _full_usage_counters(**overrides: int) -> UsageCounters:cdp_trigger_events=0,rows_exported=0,workflow_emails=0,+ workflow_push=0,workflow_destinations_dispatched=0,logs_mb_ingested=0,replay_vision_credits=0,diff --git a/ee/hogai/tools/read_billing_tool/tool.py b/ee/hogai/tools/read_billing_tool/tool.pyindex 35fceb11ca1d..4cfd144d4091 100644--- a/ee/hogai/tools/read_billing_tool/tool.py+++ b/ee/hogai/tools/read_billing_tool/tool.py@@ -36,6 +36,7 @@ USAGE_TYPES = [{"label": "PostHog AI", "value": "ai_credits_used_in_period"},{"label": "Inbox credits", "value": "signals_credits_used_in_period"},{"label": "Workflow emails", "value": "workflow_emails_sent_in_period"},+ {"label": "Workflow push notifications", "value": "workflow_push_sent_in_period"},{"label": "Workflow destinations", "value": "workflow_billable_invocations_in_period"},{"label": "Logs ingested (MB)", "value": "logs_mb_in_period"},]diff --git a/frontend/src/scenes/billing/constants.ts b/frontend/src/scenes/billing/constants.tsindex 174b1fbc6b9e..58ac312d2b16 100644--- a/frontend/src/scenes/billing/constants.ts+++ b/frontend/src/scenes/billing/constants.ts@@ -18,6 +18,7 @@ export const USAGE_TYPES = [{ label: 'PostHog AI', value: 'ai_credits_used_in_period' },{ label: 'Inbox credits', value: 'signals_credits_used_in_period' },{ label: 'Workflow emails', value: 'workflow_emails_sent_in_period' },+ { label: 'Workflow push notifications', value: 'workflow_push_sent_in_period' },{ label: 'Workflow destinations', value: 'workflow_billable_invocations_in_period' },{ label: 'Logs ingested (MB)', value: 'logs_mb_in_period' },] as constdiff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.tsindex 78fbeef671aa..5015f0ab7bc2 100644--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts@@ -419,60 +419,42 @@ describe('HogFunctionHandler', () => {])})- it('should emit a single billable_invocation metric upon function completion', async () => {- const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {- queue: 'hog',- queuePriority: 0,- })-- await hogFunctionHandler.execute({ invocation, action, result: invocationResult })-- const billableMetrics = invocationResult.metrics.filter(- (metric) => metric.metric_name === 'billable_invocation' && metric.metric_kind === 'fetch'- )-- expect(billableMetrics).toHaveLength(1)-- expect(billableMetrics[0]).toMatchObject({- team_id: team.id,- app_source_id: invocation.functionId,- instance_id: action.id,- metric_kind: 'fetch',- metric_name: 'billable_invocation',- count: 1,- })- })-- it('should emit a billable_invocation metric with email kind when billingMetricType is email', async () => {- hogFunctionHandler = new HogFunctionHandler(- mockHogFlowFunctionsService,- mockRecipientPreferencesService,- mockEmailValidationService,- 'email'- )+ // The billing kind is the whole point of the per-channel handlers: push bills at its own rate+ // (roughly half of email), so a completed invocation must emit exactly one billable_invocation+ // carrying the handler's billing type — never fall back to another channel's kind.+ it.each(['fetch', 'email', 'push'] as const)(+ 'emits a single billable_invocation with %s kind matching the handler billing type',+ async (billingType) => {+ const handler = new HogFunctionHandler(+ mockHogFlowFunctionsService,+ mockRecipientPreferencesService,+ mockEmailValidationService,+ billingType+ )- const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {- queue: 'hog',- queuePriority: 0,- })+ const invocationResult = createInvocationResult<CyclotronJobInvocationHogFlow>(invocation, {+ queue: 'hog',+ queuePriority: 0,+ })- await hogFunctionHandler.execute({ invocation, action, result: invocationResult })+ await handler.execute({ invocation, action, result: invocationResult })- const billableMetrics = invocationResult.metrics.filter(- (metric) => metric.metric_name === 'billable_invocation' && metric.metric_kind === 'email'- )+ const billableMetrics = invocationResult.metrics.filter(+ (metric) => metric.metric_name === 'billable_invocation'+ )- expect(billableMetrics).toHaveLength(1)+ expect(billableMetrics).toHaveLength(1)- expect(billableMetrics[0]).toMatchObject({- team_id: team.id,- app_source_id: invocation.functionId,- instance_id: action.id,- metric_kind: 'email',- metric_name: 'billable_invocation',- count: 1,- })- })+ expect(billableMetrics[0]).toMatchObject({+ team_id: team.id,+ app_source_id: invocation.functionId,+ instance_id: action.id,+ metric_kind: billingType,+ metric_name: 'billable_invocation',+ count: 1,+ })+ }+ )it('should not emit a billable_invocation metric if function is not finished', async () => {// Mock the executeWithAsyncFunctions to return a non-finished resultdiff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.tsindex d2195e90063e..64fa03d952fe 100644--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts@@ -27,7 +27,7 @@ export class HogFunctionHandler implements ActionHandler {private hogFlowFunctionsService: HogFlowFunctionsService,private recipientPreferencesService: RecipientPreferencesService,private emailValidationService: EmailValidationService,- private hogFlowActionBillingType: 'fetch' | 'email'+ private hogFlowActionBillingType: 'fetch' | 'email' | 'push') {}async execute({diff --git a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.ts b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.tsindex 0bc9267aec41..51744807ff3d 100644--- a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.ts+++ b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.ts@@ -105,6 +105,12 @@ export class HogFlowExecutorService {emailValidationService,'email')+ const hogFunctionPushHandler = new HogFunctionHandler(+ hogFlowFunctionsService,+ recipientPreferencesService,+ emailValidationService,+ 'push'+ )this.actionHandlers = {trigger: new TriggerHandler(),@@ -115,7 +121,7 @@ export class HogFlowExecutorService {random_cohort_branch: new RandomCohortBranchHandler(),function: hogFunctionHandler,function_sms: hogFunctionHandler,- function_push: hogFunctionHandler,+ function_push: hogFunctionPushHandler,function_email: hogFunctionEmailHandler,exit: new ExitHandler(),}diff --git a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.ts b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.tsindex 35a458be5c6d..5f71ee6309a0 100644--- a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.ts+++ b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.ts@@ -43,8 +43,9 @@ describe('HogFlow Quota Limiting', () => {const result = await checkHogFlowQuotaLimits(hogFlow, teamId, mockQuotaLimiting)expect(result.isLimited).toBe(false)- expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledTimes(2)+ expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledTimes(3)expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledWith(teamId, 'workflow_emails')+ expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledWith(teamId, 'workflow_push')expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledWith(teamId,'workflow_destinations_dispatched'@@ -67,6 +68,22 @@ describe('HogFlow Quota Limiting', () => {expect(result.isLimited).toBe(true)})+ it('should limit workflow with push action when team has push quota limit', async () => {+ mockQuotaLimiting.isTeamQuotaLimited.mockImplementation((_teamId, resource) => {+ return Promise.resolve(resource === 'workflow_push')+ })++ const hogFlow: HogFlow = {+ ...baseHogFlow,+ actions: [{ type: 'function_push' } as any, { type: 'function' } as any],+ billable_action_types: ['function_push', 'function'],+ }++ const result = await checkHogFlowQuotaLimits(hogFlow, teamId, mockQuotaLimiting)++ expect(result.isLimited).toBe(true)+ })+it('should limit workflow with destination action when team has destination quota limit', async () => {mockQuotaLimiting.isTeamQuotaLimited.mockImplementation((_teamId, resource) => {return Promise.resolve(resource === 'workflow_destinations_dispatched')@@ -140,8 +157,9 @@ describe('HogFlow Quota Limiting', () => {const result = await checkHogFlowQuotaLimits(hogFlow, teamId, mockQuotaLimiting)expect(result.isLimited).toBe(false)- expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledTimes(2)+ expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledTimes(3)expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledWith(teamId, 'workflow_emails')+ expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledWith(teamId, 'workflow_push')expect(mockQuotaLimiting.isTeamQuotaLimited).toHaveBeenCalledWith(teamId,'workflow_destinations_dispatched'diff --git a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.ts b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.tsindex 93d7c0ebfecd..a0a00f0e6af9 100644--- a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.ts+++ b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.ts@@ -34,8 +34,9 @@ export async function checkHogFlowQuotaLimits(}// Check which quotas the team is limited on- const [isEmailQuotaLimited, isDestinationQuotaLimited] = await Promise.all([+ const [isEmailQuotaLimited, isPushQuotaLimited, isDestinationQuotaLimited] = await Promise.all([quotaLimiting.isTeamQuotaLimited(teamId, 'workflow_emails'),+ quotaLimiting.isTeamQuotaLimited(teamId, 'workflow_push'),quotaLimiting.isTeamQuotaLimited(teamId, 'workflow_destinations_dispatched'),])@@ -44,6 +45,10 @@ export async function checkHogFlowQuotaLimits(return { isLimited: true }}+ if (isPushQuotaLimited && billableActionTypes.includes('function_push')) {+ return { isLimited: true }+ }+if (isDestinationQuotaLimited && billableActionTypes.includes('function')) {return { isLimited: true }}diff --git a/nodejs/src/common/services/quota-limiting.service.ts b/nodejs/src/common/services/quota-limiting.service.tsindex 485cec9c995d..80e341993ae8 100644--- a/nodejs/src/common/services/quota-limiting.service.ts+++ b/nodejs/src/common/services/quota-limiting.service.ts@@ -9,6 +9,7 @@ export type QuotaResource =| 'events'| 'cdp_trigger_events'| 'workflow_emails'+ | 'workflow_push'| 'workflow_destinations_dispatched'| 'logs_mb_ingested'| 'metrics_mb_ingested'diff --git a/posthog/models/organization.py b/posthog/models/organization.pyindex 00eef0a88273..c87037113d5f 100644--- a/posthog/models/organization.py+++ b/posthog/models/organization.py@@ -57,6 +57,7 @@ class OrganizationUsageInfo(TypedDict):signals_credits: OrganizationUsageResource | Noneposthog_code_credits: OrganizationUsageResource | Noneworkflow_emails: OrganizationUsageResource | None+ workflow_push: OrganizationUsageResource | Noneworkflow_destinations_dispatched: OrganizationUsageResource | Nonelogs_mb_ingested: OrganizationUsageResource | Nonereplay_vision_credits: OrganizationUsageResource | None