Skip to content

: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.

fix(workflows): bill AI tasks as PostHog AI (#94180)

Ben White8118bf3bc98b
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py
index ead6f69a0c44..0d5af2df3a01 100644
--- a/posthog/tasks/test/test_usage_report.py
+++ b/posthog/tasks/test/test_usage_report.py
@@ -3918,8 +3918,9 @@ class TestAIEventsUsageReport(ClickhouseDestroyTablesMixin, TestCase, Clickhouse
assert org_1_report["ai_event_count_in_period"] == 7
assert org_1_report["teams"]["3"]["ai_event_count_in_period"] == 7
+ @parameterized.expand([("posthog_ai",), ("workflows",)])
@patch("posthog.tasks.usage_report.get_instance_region")
- def test_ai_credits_with_billable_tools(self, mock_region: MagicMock) -> None:
+ def test_ai_credits_with_billable_tools(self, ai_product: str, mock_region: MagicMock) -> None:
"""Test that generations with non-search tools are billed correctly."""
from posthog.tasks.usage_report import get_teams_with_ai_credits_used_in_period
@@ -3965,7 +3966,7 @@ class TestAIEventsUsageReport(ClickhouseDestroyTablesMixin, TestCase, Clickhouse
"$ai_trace_id": "trace_billable",
"$ai_total_cost_usd": 1.0,
"$ai_billable": True,
- "ai_product": "posthog_ai",
+ "ai_product": ai_product,
"$group_1": "https://us.posthog.com",
},
)
diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.py
index 5c0b6b526f49..77334b21d09e 100644
--- a/posthog/tasks/usage_report.py
+++ b/posthog/tasks/usage_report.py
@@ -1748,6 +1748,7 @@ CLOUD_REGION_TO_URL = {
POSTHOG_AI_PRODUCTS = [
"posthog_ai",
"slack_app",
+ "workflows",
"subscriptions",
"alert_investigation_agent",
"product_analytics",
diff --git a/products/desktop/packages/agent/src/utils/gateway-routing-cases.json b/products/desktop/packages/agent/src/utils/gateway-routing-cases.json
index 5c0dce71c5b5..c41619bf069c 100644
--- a/products/desktop/packages/agent/src/utils/gateway-routing-cases.json
+++ b/products/desktop/packages/agent/src/utils/gateway-routing-cases.json
@@ -91,6 +91,12 @@
"internal": false,
"expected": "slack_app"
},
+ {
+ "origin_product": "workflow",
+ "ai_stage": null,
+ "internal": false,
+ "expected": "workflows"
+ },
{
"origin_product": "support_reply",
"ai_stage": null,
@@ -196,6 +202,13 @@
"allowlist": "signals_scout",
"expected": false
},
+ {
+ "origin_product": "workflow",
+ "ai_stage": null,
+ "internal": false,
+ "allowlist": "workflows",
+ "expected": true
+ },
{
"origin_product": "signal_report",
"ai_stage": null,
diff --git a/products/desktop/packages/agent/src/utils/gateway.test.ts b/products/desktop/packages/agent/src/utils/gateway.test.ts
index 5d7ddd03d098..fc4601ac47a0 100644
--- a/products/desktop/packages/agent/src/utils/gateway.test.ts
+++ b/products/desktop/packages/agent/src/utils/gateway.test.ts
@@ -73,6 +73,11 @@ describe("resolveGatewayProduct", () => {
originProduct: "support_reply",
expected: "conversations",
},
+ {
+ isInternal: false,
+ originProduct: "workflow",
+ expected: "workflows",
+ },
{
isInternal: true,
originProduct: "support_reply",
diff --git a/products/desktop/packages/agent/src/utils/gateway.ts b/products/desktop/packages/agent/src/utils/gateway.ts
index 83ba5389fb90..d1429224d132 100644
--- a/products/desktop/packages/agent/src/utils/gateway.ts
+++ b/products/desktop/packages/agent/src/utils/gateway.ts
@@ -5,6 +5,7 @@ export type GatewayProduct =
| "background_agents"
| "signals"
| "slack_app"
+ | "workflows"
| "posthog_ai"
| "conversations"
| "onboarding"
@@ -31,6 +32,7 @@ export function resolveGatewayProduct({
signals_scout: "signals",
slack: "slack_app",
support_reply: "conversations",
+ workflow: "workflows",
};
if (originProduct && originProduct in originProductToGatewayProductMap) {
diff --git a/products/tasks/backend/temporal/process_task/ai_gateway_token.py b/products/tasks/backend/temporal/process_task/ai_gateway_token.py
index 490de069d21a..a9474ded948e 100644
--- a/products/tasks/backend/temporal/process_task/ai_gateway_token.py
+++ b/products/tasks/backend/temporal/process_task/ai_gateway_token.py
@@ -40,6 +40,7 @@ _ORIGIN_TO_GATEWAY_PRODUCT: dict[str, str] = {
"signals_scout": "signals",
"slack": "slack_app",
"support_reply": "conversations",
+ "workflow": "workflows",
}
# Mirrors SIGNALS_STAGE_PRODUCTS + SCOUT_STAGE_PREFIX in gateway.ts.
@@ -69,6 +70,7 @@ MINTABLE_PRODUCTS = frozenset(
"signals_implementation",
"signals_repo_selection",
"signals_custom_agent",
+ "workflows",
"signals_inbox",
"signals_chat",
"signals_scout_suggestions",
diff --git a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py
index 28ef2b5183db..d5447c459803 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py
@@ -40,6 +40,7 @@ class TestResolveSandboxAiProduct:
("signal_report", "match", "signals"),
("loop", None, "posthog_code"),
("slack", None, "slack_app"),
+ ("workflow", None, "workflows"),
("support_reply", None, "conversations"),
("onboarding", None, "onboarding"),
("posthog_ai", None, "posthog_ai"),
@@ -310,6 +311,16 @@ class TestAiGatewayEnvVars:
assert "AI_GATEWAY_AI_STAGE" not in env
mint.assert_not_called()
+ def test_workflow_run_gets_a_pinned_token_when_routed(self, mint_settings):
+ mint_settings.SANDBOX_AI_GATEWAY_PRODUCTS = "workflows"
+ with patch(
+ "products.tasks.backend.temporal.process_task.utils.mint_scoped_token",
+ return_value="phe_abc",
+ ) as mint:
+ env = ai_gateway_env_vars(team_id=123, origin_product="workflow")
+ assert env["AI_GATEWAY_TOKEN"] == "phe_abc"
+ mint.assert_called_once_with(ai_product="workflows", team_id=123, user=None)
+
# The agent trusts these as the worker's word, so the API must refuse a run-supplied value.
def test_reserved_keys_cover_the_pinned_product_env(self):
assert "AI_GATEWAY_PRODUCT" in RESERVED_SANDBOX_ENVIRONMENT_VARIABLE_KEYS
diff --git a/services/llm-gateway/src/llm_gateway/products/config.py b/services/llm-gateway/src/llm_gateway/products/config.py
index a977c04de9e5..3140f1a0f51b 100644
--- a/services/llm-gateway/src/llm_gateway/products/config.py
+++ b/services/llm-gateway/src/llm_gateway/products/config.py
@@ -193,6 +193,15 @@ PRODUCTS: Final[dict[str, ProductConfig]] = {
credit_bucket=CreditBucket.AI_CREDITS,
requires_server_credential=True,
),
+ # Workflow task runs authenticate with a server-minted credential, share the Slack app's
+ # model policy, and bill into PostHog AI credits rather than PostHog Code credits.
+ "workflows": ProductConfig(
+ allowed_application_ids=frozenset({POSTHOG_CODE_US_APP_ID, POSTHOG_CODE_EU_APP_ID, POSTHOG_CODE_DEV_APP_ID}),
+ allowed_models=_POSTHOG_CODE_AGENT_MODELS | BEDROCK_MODELS,
+ allow_api_keys=False,
+ credit_bucket=CreditBucket.AI_CREDITS,
+ requires_server_credential=True,
+ ),
# SherlockHog (https://github.com/PostHog/SherlockHog) — the internal SRE
# bot. Authenticates with a personal API key (not OAuth), so no application
# IDs are needed. It pins claude-opus-4-8 but can be repointed via
diff --git a/services/llm-gateway/tests/test_dependencies.py b/services/llm-gateway/tests/test_dependencies.py
index c9ebdf63ea97..527d0403a6c7 100644
--- a/services/llm-gateway/tests/test_dependencies.py
+++ b/services/llm-gateway/tests/test_dependencies.py
@@ -299,7 +299,7 @@ class TestResolveQuota:
@pytest.mark.asyncio
@pytest.mark.parametrize(
("product", "expected_resource"),
- [("slack_app", "ai_credits"), ("posthog_code", "posthog_code_credits")],
+ [("slack_app", "ai_credits"), ("workflows", "ai_credits"), ("posthog_code", "posthog_code_credits")],
)
async def test_bucket_billed_product_resolves_its_own_bucket(self, product: str, expected_resource: str) -> None:
quota_status, quota_mock = await self._run(product)
diff --git a/services/llm-gateway/tests/test_product_config.py b/services/llm-gateway/tests/test_product_config.py
index ea1b960e722a..58734555ee60 100644
--- a/services/llm-gateway/tests/test_product_config.py
+++ b/services/llm-gateway/tests/test_product_config.py
@@ -487,20 +487,23 @@ class TestCheckProductAccess:
"gpt-5.3-codex",
],
)
- def test_slack_app_allows_agent_models(self, model: str):
- allowed, error = check_product_access("slack_app", "oauth_access_token", POSTHOG_CODE_US_APP_ID, model)
+ @pytest.mark.parametrize("product", ["slack_app", "workflows"])
+ def test_billed_agent_products_allow_agent_models(self, product: str, model: str):
+ allowed, error = check_product_access(product, "oauth_access_token", POSTHOG_CODE_US_APP_ID, model)
assert allowed is True
assert error is None
- def test_slack_app_rejects_api_keys(self):
- allowed, error = check_product_access("slack_app", "personal_api_key", None, "claude-sonnet-4-6")
+ @pytest.mark.parametrize("product", ["slack_app", "workflows"])
+ def test_billed_agent_products_reject_api_keys(self, product: str):
+ allowed, error = check_product_access(product, "personal_api_key", None, "claude-sonnet-4-6")
assert allowed is False
assert error is not None
assert "requires OAuth" in error
- def test_slack_app_rejects_unauthorized_oauth_app(self):
+ @pytest.mark.parametrize("product", ["slack_app", "workflows"])
+ def test_billed_agent_products_reject_unauthorized_oauth_apps(self, product: str):
allowed, error = check_product_access(
- "slack_app", "oauth_access_token", "00000000-0000-0000-0000-000000000000", "claude-sonnet-4-6"
+ product, "oauth_access_token", "00000000-0000-0000-0000-000000000000", "claude-sonnet-4-6"
)
assert allowed is False
assert error is not None
@@ -661,7 +664,7 @@ class TestCheckFreeTierModelAccess:
class TestServerCredentialRequirement:
"""Internal products driven by server-minted sandbox tokens (background_agents, signals,
- slack_app, conversations, onboarding) must accept only tokens carrying the internal
+ slack_app, workflows, conversations, onboarding) must accept only tokens carrying the internal
`internal_run:read` marker. Otherwise a user's own OAuth token minted under the same app could
route around the posthog_code free-tier gate through these products to premium models."""
@@ -672,6 +675,7 @@ class TestServerCredentialRequirement:
("background_agents", POSTHOG_CODE_US_APP_ID),
("signals", SIGNALS_DEV_APP_ID),
("slack_app", POSTHOG_CODE_US_APP_ID),
+ ("workflows", POSTHOG_CODE_US_APP_ID),
("conversations", POSTHOG_CODE_US_APP_ID),
("onboarding", POSTHOG_CODE_US_APP_ID),
]