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(phai): gateway config (#65474)

Georgiy Tarasov13086c13de90
diff --git a/products/posthog_ai/backend/message_routing.py b/products/posthog_ai/backend/message_routing.py
index 6a64dd596632..3c370a9b8a91 100644
--- a/products/posthog_ai/backend/message_routing.py
+++ b/products/posthog_ai/backend/message_routing.py
@@ -48,6 +48,7 @@ if TYPE_CHECKING:
logger = structlog.get_logger(__name__)
DEFAULT_INITIAL_PERMISSION_MODE: InitialPermissionMode = "auto"
+POSTHOG_AI_INTERACTION_ORIGIN = tasks_facade.TaskOriginProduct.POSTHOG_AI.value
# A PostHog AI sandbox session is a live chat: if the user goes quiet for this long
# they've moved on, so the Run's workflow times out and reclaims the sandbox. Passed
@@ -228,6 +229,7 @@ class SandboxSession(BaseSandboxService):
self.team.pk,
self.user.pk,
extra_state={
+ "interaction_origin": POSTHOG_AI_INTERACTION_ORIGIN,
"systemPrompt": system_prompt,
"initial_permission_mode": initial_permission_mode,
"inactivity_timeout_seconds": SANDBOX_INACTIVITY_TIMEOUT_SECONDS,
@@ -281,6 +283,7 @@ class SandboxSession(BaseSandboxService):
repository=repository,
create_pr=False,
mode="interactive",
+ interaction_origin=POSTHOG_AI_INTERACTION_ORIGIN,
inactivity_timeout_seconds=SANDBOX_INACTIVITY_TIMEOUT_SECONDS,
# Defer the workflow so the initial run state can carry the PostHog AI keys.
start_workflow=False,
@@ -298,6 +301,7 @@ class SandboxSession(BaseSandboxService):
system_prompt=system_prompt,
attached_context=attached_context,
initial_permission_mode=initial_permission_mode,
+ interaction_origin=POSTHOG_AI_INTERACTION_ORIGIN,
pending_user_message=wrapped,
)
state_updates = ph_state.model_dump(mode="json", by_alias=True, exclude_unset=True)
@@ -449,6 +453,7 @@ class SandboxSession(BaseSandboxService):
run = current
extra_state: dict[str, Any] = {
+ "interaction_origin": POSTHOG_AI_INTERACTION_ORIGIN,
"resume_from_run_id": str(run.id),
"pending_user_message": wrapped,
"systemPrompt": system_prompt,
diff --git a/products/posthog_ai/backend/tests/test_message_routing.py b/products/posthog_ai/backend/tests/test_message_routing.py
index 523c78f9e414..78f5b068c89d 100644
--- a/products/posthog_ai/backend/tests/test_message_routing.py
+++ b/products/posthog_ai/backend/tests/test_message_routing.py
@@ -9,6 +9,7 @@ from posthog.exceptions import Conflict, QuotaLimitExceeded
from products.posthog_ai.backend.context_wrapper import MAX_ATTACHED_ITEMS, MAX_TEXT_LENGTH
from products.posthog_ai.backend.message_routing import (
+ POSTHOG_AI_INTERACTION_ORIGIN,
SANDBOX_INACTIVITY_TIMEOUT_SECONDS,
SandboxSession,
lock_conversation_for_followup,
@@ -79,11 +80,13 @@ class TestOpenSandboxMessage(APIBaseTest):
assert kwargs["create_pr"] is False
assert kwargs["mode"] == "interactive"
assert kwargs["start_workflow"] is False
+ assert kwargs["interaction_origin"] == POSTHOG_AI_INTERACTION_ORIGIN
# The sandbox session pins its short interactivity window as a per-task override.
assert kwargs["inactivity_timeout_seconds"] == SANDBOX_INACTIVITY_TIMEOUT_SECONDS
# Run state enriched with the PostHog AI per-Run keys; full undeduped context.
run.refresh_from_db()
+ assert run.state["interaction_origin"] == POSTHOG_AI_INTERACTION_ORIGIN
assert run.state["systemPrompt"] == SYS_PROMPT
assert run.state["initial_permission_mode"] == "auto"
assert run.state["attached_context"] == [{"type": "dashboard", "id": 123, "name": "Funnel"}]
@@ -270,6 +273,7 @@ class TestOpenSandboxMessage(APIBaseTest):
assert str(new_run.id) != str(run.id)
assert new_run.state["resume_from_run_id"] == str(run.id)
assert new_run.state["snapshot_external_id"] == "snap-9"
+ assert new_run.state["interaction_origin"] == POSTHOG_AI_INTERACTION_ORIGIN
assert new_run.state["systemPrompt"] == SYS_PROMPT
assert new_run.state["initial_permission_mode"] == "auto"
assert new_run.state["inactivity_timeout_seconds"] == SANDBOX_INACTIVITY_TIMEOUT_SECONDS
@@ -444,6 +448,7 @@ class TestSandboxWarmViaOpen(APIBaseTest):
assert run is not None
assert str(run.id) == result.run_id
assert result.just_created_run is True
+ assert run.state["interaction_origin"] == POSTHOG_AI_INTERACTION_ORIGIN
assert run.state["systemPrompt"] == SYS_PROMPT
assert run.state["await_user_message"] is True
assert run.state["initial_permission_mode"] == "auto"
diff --git a/services/llm-gateway/src/llm_gateway/config.py b/services/llm-gateway/src/llm_gateway/config.py
index 68a53dc1cd30..a55e1e659404 100644
--- a/services/llm-gateway/src/llm_gateway/config.py
+++ b/services/llm-gateway/src/llm_gateway/config.py
@@ -31,6 +31,7 @@ DEFAULT_PRODUCT_COST_LIMITS: dict[str, "ProductCostLimit"] = {
"background_agents": ProductCostLimit(limit_usd=1000.0, window_seconds=3600),
"django": ProductCostLimit(limit_usd=5000.0, window_seconds=86400),
"signals": ProductCostLimit(limit_usd=5000.0, window_seconds=86400),
+ "posthog_ai": ProductCostLimit(limit_usd=5000.0, window_seconds=86400),
}
DEFAULT_USER_COST_LIMITS: dict[str, "UserCostLimit"] = {
diff --git a/services/llm-gateway/tests/test_cost_throttles.py b/services/llm-gateway/tests/test_cost_throttles.py
index 08cdd05350d0..d66c9c100f77 100644
--- a/services/llm-gateway/tests/test_cost_throttles.py
+++ b/services/llm-gateway/tests/test_cost_throttles.py
@@ -46,6 +46,8 @@ class TestProductCostLimitConfig:
assert "llm_gateway" in settings.product_cost_limits
assert settings.product_cost_limits["llm_gateway"].limit_usd == 1000.0
assert settings.product_cost_limits["llm_gateway"].window_seconds == 86400
+ assert settings.product_cost_limits["posthog_ai"].limit_usd == 5000.0
+ assert settings.product_cost_limits["posthog_ai"].window_seconds == 86400
def test_parses_json_string(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(