: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(slack-app): show the thread cost in the reply footer (#128993)
Vojta Bartošca5639e3ed0e
diff --git a/products/slack_app/backend/feature_flags.py b/products/slack_app/backend/feature_flags.pyindex d2033fe9ac8c..6035c3abd2bb 100644--- a/products/slack_app/backend/feature_flags.py+++ b/products/slack_app/backend/feature_flags.py@@ -49,6 +49,7 @@ logger = structlog.get_logger(__name__)SLACK_APP_AGENT_DESIGN_FLAG = "slack-app-agent-design"SLACK_APP_FORKING_FLAG = "slack-app-forking"SLACK_APP_BYOS_FLAG = "slack-app-byos"+SLACK_APP_FOOTER_COST_FLAG = "slack-app-footer-cost"# Linking a Slack identity to a PostHog user resolves the Slack profile and its email.@@ -154,6 +155,16 @@ def is_slack_app_byos_enabled()+def is_slack_app_footer_cost_enabled(integration: IntegrationRecord, distinct_id: str | None) -> bool:+ """Gate for the thread cost in the footer under an answer, keyed on the run's PostHog user."""+ return _workspace_flag_enabled(+ SLACK_APP_FOOTER_COST_FLAG,+ integration,+ failure_log_key="slack_app_footer_cost_feature_flag_check_failed",+ distinct_id=distinct_id,+ )++def is_slack_app_assistant_enabled(integration: IntegrationRecord) -> bool:"""Gate for the DM assistant: the install must hold the scopes the DM surfacecalls — ``im:history`` in particular, without which the assistant would answerdiff --git a/products/slack_app/backend/services/slack_messages.py b/products/slack_app/backend/services/slack_messages.pyindex 22b0ee2cbfd0..65f48ffbeb8e 100644--- a/products/slack_app/backend/services/slack_messages.py+++ b/products/slack_app/backend/services/slack_messages.py@@ -23,7 +23,7 @@ import reimport jsonfrom dataclasses import asdict, dataclassfrom decimal import Decimal, InvalidOperation-from typing import Any+from typing import TYPE_CHECKING, Anyfrom uuid import UUIDfrom django.conf import settings@@ -45,6 +45,9 @@ from products.slack_app.backend.services.model_catalogue import describe_run_modfrom products.slack_app.backend.services.slack_user_info import get_cached_bot_user_id, get_slack_user_infofrom products.slack_app.backend.services.workspace_integrations import WorkspaceIntegration+if TYPE_CHECKING:+ from products.tasks.backend.facade.contracts import TaskRunDTO+logger = structlog.get_logger(__name__)# Short TTL keeps a burst of follow-ups (chatty thread, fast classifier-then-forwarder@@ -715,6 +718,8 @@ class RunFooter:# Whether the run shares charts as insight links instead of images, so reply analytics can# compare the two delivery modes. Not rendered.chart_links: bool = False+ # Model spend of the thread's task so far. Sandbox compute is not billed, so it is not included.+ thread_cost_usd: Decimal | None = Nonedef has_content(self) -> bool:"""Whether this would render as anything.@@ -724,7 +729,7 @@ class RunFooter:keeps meaning "None-coalesce" and cannot silently discard a partial instance.The ids are not part of the answer — they say nothing on their own."""- return any((self.task_url, self.model, self.project, self.plan))+ return any((self.task_url, self.model, self.project, self.plan, self.thread_cost_usd is not None))def _project_name(team_id: int, *, integration_id: int | None, created_by_id: int | None) -> str | None:@@ -800,7 +805,9 @@ def _plan_label(state: dict[str, Any] | None, *, task_id: Any, integration_id: ireturn f"<@{owner_slack_user_id}>'s {plan.plan_name}"-def load_run_footer(run_id: str | UUID | None, *, integration_id: int | None) -> RunFooter:+def load_run_footer(+ run_id: str | UUID | None, *, integration_id: int | None, include_thread_cost: bool = False+) -> RunFooter:"""Describe a run for the footer.Never raises: the footer is the last thing added to an answer that is already@@ -812,6 +819,8 @@ def load_run_footer(run_id: str | UUID | None, *, integration_id: int | None) ->``integration_id`` is the install the reply is posted through, and the workspacebehind it is what the project segment is judged against. Required rather thandefaulted, so a new caller cannot drop the segment without saying so.++ ``include_thread_cost`` is for the reply that closes a turn."""# Deferred so the tasks product stays off this module's import path, matching# `model_catalogue`.@@ -825,6 +834,7 @@ def load_run_footer(run_id: str | UUID | None, *, integration_id: int | None) ->if run is None:return RunFooter()state = parse_run_state(run.state)+ plan = _plan_label(run.state, task_id=run.task_id, integration_id=integration_id)return RunFooter(run_id=str(run.id),task_id=str(run.task_id),@@ -836,17 +846,53 @@ def load_run_footer(run_id: str | UUID | None, *, integration_id: int | None) ->integration_id=integration_id,created_by_id=run.created_by_id,),- plan=_plan_label(run.state, task_id=run.task_id, integration_id=integration_id),+ plan=plan,chart_links=bool((run.state or {}).get("slack_chart_links")),+ # A run on someone's own plan is not paid with PostHog credits, so its cost says nothing.+ thread_cost_usd=_thread_cost(run, integration_id=integration_id)+ if include_thread_cost and plan is None+ else None,)except Exception:logger.exception("slack_app_run_footer_load_failed", run_id=str(run_id))return RunFooter()+def _thread_cost(run: "TaskRunDTO", *, integration_id: int | None) -> Decimal | None:+ """What the thread's task spent on models so far, or `None` outside the rollout or when unknown.++ Reads the same task usage that PostHog Desktop shows, which sums the task's `$ai_generation`+ events. Those events arrive with a short delay and the usage is cached for a minute, so the+ newest requests can be missing here. A later reply's total then includes them.+ """+ from products.slack_app.backend.feature_flags import is_slack_app_footer_cost_enabled # noqa: PLC0415+ from products.tasks.backend.facade.billing import get_task_usage # noqa: PLC0415++ if integration_id is None or run.task_created_at is None:+ return None+ try:+ integration = IntegrationManager.slack.oauth.get(run.team_id, integration_id)+ if not is_slack_app_footer_cost_enabled(integration, run.created_by_distinct_id):+ return None+ return get_task_usage(+ team_id=run.team_id, task_id=run.task_id, task_created_at=run.task_created_at+ ).token_cost_usd+ except Exception:+ # Its own guard, like `_project_name`: a failed lookup costs the reader one segment.+ logger.warning("slack_app_thread_cost_load_failed", task_id=str(run.task_id), exc_info=True)+ return None+++def describe_thread_cost(cost_usd: Decimal) -> str:+ """A cost under one cent shows as `<$0.01`, so that it does not read as free."""+ if cost_usd < Decimal("0.01"):+ return "Thread cost: <$0.01"+ return f"Thread cost: ${cost_usd:.2f}"++def _run_context_segments(footer: RunFooter) -> list[str]:- """What the run is, as footer segments: the model, the plan that pays for it, then the- project it answered from.+ """What the run is, as footer segments: the model, the thread cost, the plan that pays for+ it, then the project it answered from.Shared by the reply footer and the progress message so the two cannot describe onerun differently, and so the escaping below has a single home.@@ -854,6 +900,8 @@ def _run_context_segments(footer: RunFooter) -> list[str]:segments: list[str] = []if footer.model:segments.append(describe_run_model(footer.model, footer.reasoning_effort))+ if footer.thread_cost_usd is not None:+ segments.append(describe_thread_cost(footer.thread_cost_usd))if footer.plan:segments.append(footer.plan)if footer.project:diff --git a/products/slack_app/backend/slack_thread.py b/products/slack_app/backend/slack_thread.pyindex 64df3b26a475..29bafbc46c86 100644--- a/products/slack_app/backend/slack_thread.py+++ b/products/slack_app/backend/slack_thread.py@@ -288,6 +288,7 @@ class SlackThreadHandler:actor_slack_user_id: str | None = None,turn_trace_id: str | None = None,turn_person_message: str | None = None,+ include_thread_cost: bool = False,) -> "SlackThreadHandler":"""A handler whose footer describes ``run_id``.@@ -296,7 +297,7 @@ class SlackThreadHandler:"""handler = cls(context,- load_run_footer(run_id, integration_id=context.integration_id),+ load_run_footer(run_id, integration_id=context.integration_id, include_thread_cost=include_thread_cost),actor_slack_user_id=actor_slack_user_id,turn_trace_id=turn_trace_id,turn_person_message=turn_person_message,@@ -1182,6 +1183,9 @@ class SlackThreadHandler:task_id=self.run_footer.task_id,run_id=self.run_footer.run_id,chart_links=self.run_footer.chart_links,+ thread_cost_usd=(+ float(self.run_footer.thread_cost_usd) if self.run_footer.thread_cost_usd is not None else None+ ),)except Exception:logger.warning("slack_app_reply_posted_capture_failed", reply_kind=str(reply_kind), exc_info=True)diff --git a/products/slack_app/backend/tests/services/slack_messages/test_run_footer.py b/products/slack_app/backend/tests/services/slack_messages/test_run_footer.pyindex 2afe7ed332e3..cbc02def0985 100644--- a/products/slack_app/backend/tests/services/slack_messages/test_run_footer.py+++ b/products/slack_app/backend/tests/services/slack_messages/test_run_footer.py@@ -1,3 +1,4 @@+from decimal import Decimalfrom types import SimpleNamespacefrom typing import castfrom uuid import uuid4@@ -64,6 +65,23 @@ class TestRunFooter(SimpleTestCase):None,"*Claude Opus 5* · Your ChatGPT plan · Project: *Fernwood staging*",),+ (+ "cost_follows_the_model",+ RunFooter(+ TASK_URL,+ "claude-opus-5",+ project="Fernwood staging",+ thread_cost_usd=Decimal("0.123456"),+ ),+ None,+ f"<{TASK_URL}|View session> · *Claude Opus 5* · Thread cost: $0.12 · Project: *Fernwood staging*",+ ),+ (+ "a_cost_under_a_cent_does_not_read_as_free",+ RunFooter(thread_cost_usd=Decimal("0.004")),+ None,+ "Thread cost: <$0.01",+ ),("configure_only",RunFooter(),diff --git a/products/slack_app/backend/tests/test_slack_thread.py b/products/slack_app/backend/tests/test_slack_thread.pyindex 0e5e96cd117e..753dab433ac3 100644--- a/products/slack_app/backend/tests/test_slack_thread.py+++ b/products/slack_app/backend/tests/test_slack_thread.py@@ -694,6 +694,7 @@ class TestReplyPostedCapture(SimpleTestCase):"task_id": "task-1","run_id": "run-1","chart_links": True,+ "thread_cost_usd": None,}diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex 3494e4d49816..722f4fac0f2e 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -504,6 +504,7 @@ def _task_run_to_dto(run: TaskRun, *, task: Task | None = None) -> contracts.Tasworkflow_id=run.workflow_id,mode=run.mode,task_origin_product=parent.origin_product if parent is not None else None,+ task_created_at=parent.created_at if parent is not None else None,created_by_id=parent.created_by_id if parent is not None else None,created_by_distinct_id=str(created_by.distinct_id) if created_by is not None else None,pr_url=(run.output or {}).get("pr_url"),diff --git a/products/tasks/backend/facade/contracts.py b/products/tasks/backend/facade/contracts.pyindex 4fa4ac64e6a3..158dfd3f65a9 100644--- a/products/tasks/backend/facade/contracts.py+++ b/products/tasks/backend/facade/contracts.py@@ -219,6 +219,7 @@ class TaskRunDTO:workflow_id: str = ""mode: str = "background"task_origin_product: str | None = None+ task_created_at: datetime | None = Nonecreated_by_id: int | None = Nonecreated_by_distinct_id: str | None = Nonepr_url: str | None = Nonediff --git a/products/tasks/backend/temporal/slack_relay/activities.py b/products/tasks/backend/temporal/slack_relay/activities.pyindex 50a03fceb956..ac43ee502732 100644--- a/products/tasks/backend/temporal/slack_relay/activities.py+++ b/products/tasks/backend/temporal/slack_relay/activities.py@@ -266,6 +266,7 @@ def relay_slack_message(input: RelaySlackMessageInput) -> None:actor_slack_user_id=target,turn_trace_id=input.trace_id,turn_person_message=person_message,+ include_thread_cost=True,)if handler.proactive_notifications_muted() and not is_explicit_slack_reply(task_run, input.message_id):