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): report task usage before compute rates take effect (#87051)

Adam Bowker8dd04b7f6e75
diff --git a/products/tasks/backend/logic/services/task_usage.py b/products/tasks/backend/logic/services/task_usage.py
index 56d0de4b744c..a1c663d0dee5 100644
--- a/products/tasks/backend/logic/services/task_usage.py
+++ b/products/tasks/backend/logic/services/task_usage.py
@@ -158,6 +158,11 @@ def _get_task_compute_cost(*, team_id: int, task_id: UUID) -> Decimal:
rate_cards = validate_compute_rate_cards(COMPUTE_RATE_CARDS)
calculated_at = timezone.now()
+ pricing_start = rate_cards[0].effective_at
+ # Rates are published ahead of the date they take effect, so until then nothing is priced.
+ if calculated_at <= pricing_start:
+ return Decimal(0)
+
sessions = (
SandboxSession.objects.for_team(team_id)
.filter(
@@ -178,7 +183,7 @@ def _get_task_compute_cost(*, team_id: int, task_id: UUID) -> Decimal:
(
calculate_sandbox_compute_cost(
session,
- rate_cards[0].effective_at,
+ pricing_start,
calculated_at,
calculated_at=calculated_at,
rate_cards=rate_cards,
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 845a56b9b248..b2cc4a5e1c7d 100644
--- a/products/tasks/backend/logic/services/tests/test_task_usage.py
+++ b/products/tasks/backend/logic/services/tests/test_task_usage.py
@@ -126,6 +126,46 @@ class TestTaskUsage(ClickhouseTestMixin, APIBaseTest):
assert usage.compute_cost_usd == Decimal("0.20")
+ def test_usage_is_reported_before_the_first_rate_card_takes_effect(self) -> None:
+ rate_start = datetime(2026, 8, 1, tzinfo=UTC)
+ rate_card = ComputeRateCard(
+ version="test",
+ effective_at=rate_start,
+ expires_at=None,
+ cpu_core_second_usd=Decimal("0.01"),
+ memory_gib_second_usd=Decimal("0.01"),
+ )
+ run = TaskRun.objects.create(task=self.task, team=self.team)
+ SandboxSession.objects.unscoped().create(
+ team=self.team,
+ task_run=run,
+ sandbox_id="sandbox-before-rates",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ client_provenance=TaskClientProvenance.POSTHOG_DESKTOP,
+ cpu_cores=1,
+ memory_gb=1,
+ ttl_seconds=3600,
+ burstable=False,
+ created_at=rate_start - timedelta(hours=2),
+ ttl_expires_at=rate_start - timedelta(hours=1),
+ user_attributed_at=rate_start - timedelta(hours=2),
+ ended_at=rate_start - timedelta(hours=1, seconds=50),
+ )
+
+ with (
+ patch.object(task_usage, "COMPUTE_RATE_CARDS", (rate_card,)),
+ patch.object(task_usage, "_get_task_token_cost", return_value=Decimal("2.5")),
+ patch.object(task_usage.timezone, "now", return_value=rate_start - timedelta(days=1)),
+ ):
+ usage = task_usage.get_task_usage(
+ team_id=self.team.id,
+ task_id=self.task.id,
+ task_created_at=self.task.created_at,
+ )
+
+ assert usage.compute_cost_usd == Decimal(0)
+ assert usage.total_cost_usd == Decimal("2.5")
+
def test_eu_token_cost_uses_cross_region_source(self) -> None:
with (
self.settings(