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(tasks): count slack_app generations in a task's token cost (#129612)

Vojta Bartoš48a1a9c6f02c
diff --git a/products/tasks/backend/logic/services/task_usage.py b/products/tasks/backend/logic/services/task_usage.py
index 1d2aca3bdbe6..5ef4d85d8c3d 100644
--- a/products/tasks/backend/logic/services/task_usage.py
+++ b/products/tasks/backend/logic/services/task_usage.py
@@ -152,6 +152,12 @@ def _internal_llm_analytics_team() -> Team:
raise TaskTokenUsageUnavailable("The internal AI observability project is not readable here") from error
+# The products a task's own agent bills its generations to. A Slack task's agent bills `slack_app`,
+# so a `posthog_code`-only filter prices every Slack task at zero. Other products can tag the same
+# task id, such as `background_agents`, and stay out.
+TASK_AGENT_AI_PRODUCTS = ("posthog_code", "slack_app")
+
+
def get_local_task_token_cost(*, team_id: int, task_id: UUID, task_created_at: datetime) -> Decimal:
query = parse_select(
"""
@@ -159,7 +165,7 @@ def get_local_task_token_cost(*, team_id: int, task_id: UUID, task_created_at: d
FROM events
WHERE equals(event, '$ai_generation')
AND greaterOrEquals(timestamp, {task_created_at})
- AND equals(properties.ai_product, 'posthog_code')
+ AND in(properties.ai_product, {ai_products})
AND equals(toString(properties.team_id), {team_id})
AND (
equals(properties.task_id, {task_id})
@@ -174,6 +180,7 @@ def get_local_task_token_cost(*, team_id: int, task_id: UUID, task_created_at: d
"task_created_at": ast.Constant(value=task_created_at),
"team_id": ast.Constant(value=str(team_id)),
"task_id": ast.Constant(value=str(task_id)),
+ "ai_products": ast.Constant(value=list(TASK_AGENT_AI_PRODUCTS)),
},
team=_internal_llm_analytics_team(),
query_type="TaskUsageTokenCost",
diff --git a/products/tasks/backend/logic/services/tests/test_task_usage.py b/products/tasks/backend/logic/services/tests/test_task_usage.py
index 7a3c84b297ab..780239d6ce6e 100644
--- a/products/tasks/backend/logic/services/tests/test_task_usage.py
+++ b/products/tasks/backend/logic/services/tests/test_task_usage.py
@@ -112,6 +112,7 @@ class TestTaskUsage(ClickhouseTestMixin, APIBaseTest):
def test_token_cost_only_includes_generations_for_the_task_and_product(self) -> None:
for team_id, task_id, product, cost in (
(self.team.id, str(self.task.id), "posthog_code", 2.5),
+ (self.team.id, str(self.task.id), "slack_app", 1.25),
(self.team.id, str(self.task.id), "background_agents", 10),
(self.team.id, "00000000-0000-0000-0000-000000000001", "posthog_code", 20),
(self.team.id + 1, str(self.task.id), "posthog_code", 40),
@@ -137,9 +138,9 @@ class TestTaskUsage(ClickhouseTestMixin, APIBaseTest):
task_created_at=self.task.created_at,
)
- assert usage.token_cost_usd == Decimal("2.5")
+ assert usage.token_cost_usd == Decimal("3.75")
assert usage.compute_cost_usd == 0
- assert usage.total_cost_usd == Decimal("2.5")
+ assert usage.total_cost_usd == Decimal("3.75")
def test_run_token_costs_group_by_run_and_key_on_the_origin_product(self) -> None:
# `ai_product` can't stand in for `task_origin_product`: a scout run reports