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(signals): skip the activity summary tick when no LLM gateway is configured (#121011)

posthog[bot]fd3fb52eb9fe
diff --git a/posthog/llm/gateway_client.py b/posthog/llm/gateway_client.py
index fa2c4ac023b9..a7747f9541bc 100644
--- a/posthog/llm/gateway_client.py
+++ b/posthog/llm/gateway_client.py
@@ -407,6 +407,11 @@ def anthropic_gateway_base_url(openai_base_url: str) -> str:
return trimmed
+def openai_gateway_configured() -> bool:
+ """Whether :func:`build_openai_client` can build a client, without building one."""
+ return resolve_ai_gateway_config() is not None or bool(settings.LLM_GATEWAY_URL and settings.LLM_GATEWAY_API_KEY)
+
+
def build_openai_client(
product: Product,
ai_product: str | None = None,
diff --git a/products/signals/backend/activity_summary/tick.py b/products/signals/backend/activity_summary/tick.py
index ebb15e34a8e0..e6e96826519f 100644
--- a/products/signals/backend/activity_summary/tick.py
+++ b/products/signals/backend/activity_summary/tick.py
@@ -15,6 +15,7 @@ from temporalio import activity, workflow
from temporalio.common import RetryPolicy
from posthog.dataclasses import frozen
+from posthog.llm.gateway_client import openai_gateway_configured
from posthog.models import Team
from posthog.ph_client import ph_background_capture
from posthog.schema_enums import EmbeddingModelName
@@ -47,6 +48,7 @@ EMBEDDING_RENDERING = "summary_text"
STOP_MAX_PAIRS_PER_TICK = "max_pairs_per_tick"
STOP_MAX_PAIRS_PER_DAY = "max_pairs_per_day"
STOP_MAX_COST_USD_PER_DAY = "max_cost_usd_per_day"
+STOP_GATEWAY_NOT_CONFIGURED = "gateway_not_configured"
_PLAN_TIMEOUT = datetime.timedelta(minutes=5)
# One model call has a 120 second request timeout and one retry on a malformed answer.
@@ -109,7 +111,12 @@ class ActivitySummaryTickResult:
def plan_tick(limit: int | None, now: datetime.datetime) -> ActivitySummaryTickPlan:
config = read_config()
- usage = daily_usage(now) if config.enabled else None
+ gateway_missing = config.enabled and not openai_gateway_configured()
+ if gateway_missing:
+ # Every pair would fail on the same missing gateway, so the tick runs none.
+ logger.warning("activity_summary_tick_gateway_not_configured")
+ enabled = config.enabled and not gateway_missing
+ usage = daily_usage(now) if enabled else None
updates_left = max(0, config.max_pairs_per_day - usage.updates) if usage else 0
cost_left = max(0.0, config.max_cost_usd_per_day - usage.cost_usd) if usage else 0.0
tick_limit = limit or config.max_pairs_per_tick
@@ -117,14 +124,16 @@ def plan_tick(limit: int | None, now: datetime.datetime) -> ActivitySummaryTickP
due = select_pairs(config, now, tick_limit + 1) if updates_left and cost_left else []
stop_reason: str | None = None
- if config.enabled and not updates_left:
+ if gateway_missing:
+ stop_reason = STOP_GATEWAY_NOT_CONFIGURED
+ elif enabled and not updates_left:
stop_reason = STOP_MAX_PAIRS_PER_DAY
- elif config.enabled and not cost_left:
+ elif enabled and not cost_left:
stop_reason = STOP_MAX_COST_USD_PER_DAY
elif len(due) > tick_limit:
stop_reason = STOP_MAX_PAIRS_PER_TICK
return ActivitySummaryTickPlan(
- enabled=config.enabled,
+ enabled=enabled,
pairs=due[:tick_limit],
candidates=len(due),
updates_left=updates_left,
@@ -267,7 +276,7 @@ class ActivitySummaryTickWorkflow:
retry_policy=RetryPolicy(maximum_attempts=2),
)
if not plan.enabled:
- return ActivitySummaryTickResult()
+ return ActivitySummaryTickResult(stop_reason=plan.stop_reason)
statuses: Counter[str] = Counter()
errored = 0
diff --git a/products/signals/backend/test/test_activity_summary_tick.py b/products/signals/backend/test/test_activity_summary_tick.py
index f59d59924d6d..ee9717728509 100644
--- a/products/signals/backend/test/test_activity_summary_tick.py
+++ b/products/signals/backend/test/test_activity_summary_tick.py
@@ -9,6 +9,8 @@ import time_machine
from posthog.test.base import BaseTest
from unittest.mock import AsyncMock, MagicMock, patch
+from django.test import override_settings
+
from parameterized import parameterized
from temporalio import activity
from temporalio.client import Client, ScheduleState
@@ -33,6 +35,7 @@ from products.signals.backend.activity_summary.schedule import (
create_activity_summary_tick_schedule,
)
from products.signals.backend.activity_summary.tick import (
+ STOP_GATEWAY_NOT_CONFIGURED,
STOP_MAX_COST_USD_PER_DAY,
STOP_MAX_PAIRS_PER_DAY,
STOP_MAX_PAIRS_PER_TICK,
@@ -207,26 +210,33 @@ class TestActivitySummaryQueue(BaseTest):
@parameterized.expand(
[
- ("the tick cap", {"max_pairs_per_tick": 1}, STOP_MAX_PAIRS_PER_TICK, 1),
- ("the daily pair cap", {"max_pairs_per_day": 1}, STOP_MAX_PAIRS_PER_DAY, 0),
- ("the daily cost cap", {"max_cost_usd_per_day": 0.1}, STOP_MAX_COST_USD_PER_DAY, 0),
- ("no cap", {}, None, 2),
+ ("the tick cap", {"max_pairs_per_tick": 1}, "phs_test", STOP_MAX_PAIRS_PER_TICK, 1),
+ ("the daily pair cap", {"max_pairs_per_day": 1}, "phs_test", STOP_MAX_PAIRS_PER_DAY, 0),
+ ("the daily cost cap", {"max_cost_usd_per_day": 0.1}, "phs_test", STOP_MAX_COST_USD_PER_DAY, 0),
+ ("no cap", {}, "phs_test", None, 2),
+ ("no gateway", {}, "", STOP_GATEWAY_NOT_CONFIGURED, 0),
]
)
def test_plan_names_the_cap_that_stops_it(
- self, _name: str, overrides: dict[str, Any], stop_reason: str | None, pairs: int
+ self, _name: str, overrides: dict[str, Any], gateway_key: str, stop_reason: str | None, pairs: int
) -> None:
self._changes(self._user("[email protected]"), 3)
self._changes(self._user("[email protected]"), 3)
self._summary(self.user, cost_usd=0.1)
with (
+ override_settings(
+ AI_GATEWAY_URL="https://ai-gateway.example/v1" if gateway_key else "",
+ AI_GATEWAY_API_KEY=gateway_key,
+ LLM_GATEWAY_URL="",
+ LLM_GATEWAY_API_KEY="",
+ ),
patch.object(tick, "read_config", return_value=self._config(**overrides)),
patch("products.signals.backend.activity_summary.queue.list_user_task_times", return_value=[]),
):
plan = plan_tick(None, NOW)
- assert (plan.stop_reason, len(plan.pairs)) == (stop_reason, pairs)
+ assert (plan.stop_reason, len(plan.pairs), plan.enabled) == (stop_reason, pairs, bool(gateway_key))
def test_a_skipped_pair_does_not_come_back_on_the_next_tick(self) -> None:
self._changes(self.user, 2)