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.

feat(tasks): mint Go ai-gateway tokens for reserved background_agents origins (#125732)

Jonathan Mieloo4ebf2fb80c16
diff --git a/posthog/settings/temporal.py b/posthog/settings/temporal.py
index c88132c6adcc..4efaf0ac9576 100644
--- a/posthog/settings/temporal.py
+++ b/posthog/settings/temporal.py
@@ -129,6 +129,7 @@ SANDBOX_AI_GATEWAY_TOKEN_CAP_USD_OVERRIDES: str = get_from_env("SANDBOX_AI_GATEW
# retries behind a cap that binds mid-run. Suggestion runs stay on the default. A PostHog
# Desktop cloud run has no duration cap, so its cap only bounds a runaway run.
SANDBOX_AI_GATEWAY_TOKEN_CAP_USD_PRODUCT_DEFAULTS: dict[str, str] = {
+ "background_agents": "50",
"signals_scout": "30",
"signals_implementation": "20",
"signals_inbox": "75",
diff --git a/products/tasks/backend/logic/services/gateway_model_pin.py b/products/tasks/backend/logic/services/gateway_model_pin.py
index 4ea852eb200d..15c5e08b7563 100644
--- a/products/tasks/backend/logic/services/gateway_model_pin.py
+++ b/products/tasks/backend/logic/services/gateway_model_pin.py
@@ -72,6 +72,7 @@ FREE_TIER_PIN_KEY = "posthog_code:free"
PRODUCT_ALLOWED_MODELS: dict[str, list[str]] = {
"posthog_ai": _first_party_pin("posthog_ai"),
"account_audit": _first_party_pin("account_audit"),
+ "background_agents": _first_party_pin("background_agents"),
"posthog_code": DESKTOP_AGENT_MODELS,
FREE_TIER_PIN_KEY: FREE_TIER_MODELS,
"review_hog": _first_party_pin("review_hog"),
diff --git a/products/tasks/backend/temporal/process_task/ai_gateway_token.py b/products/tasks/backend/temporal/process_task/ai_gateway_token.py
index 95b72557c8d5..8d50c726c3b1 100644
--- a/products/tasks/backend/temporal/process_task/ai_gateway_token.py
+++ b/products/tasks/backend/temporal/process_task/ai_gateway_token.py
@@ -79,7 +79,8 @@ _SCOUT_STAGE_PREFIX = "scout:"
# Products whose runs may mint an internally funded token. Mint scope needs
# server-side provenance: `internal` and some origin_product values are
# API-settable, so an unmapped origin marked internal resolves to
-# background_agents and must never mint. Signals products qualify because their
+# background_agents and mints only from an origin validate_origin_product reserves
+# (BACKGROUND_AGENTS_ORIGINS). Signals products qualify because their
# stages are set only by server flows: pipeline stages by the flows that start
# them, `inbox` / `chat` by `Task.create_run`. A caller owning a report can reach
# `signals_inbox`, so the per-run cap and the product's daily budget bound those two.
@@ -96,6 +97,7 @@ MINTABLE_PRODUCTS = frozenset(
{
"posthog_ai",
"account_audit",
+ "background_agents",
"posthog_code",
"review_hog",
"slack_app",
@@ -111,6 +113,8 @@ MINTABLE_PRODUCTS = frozenset(
}
)
+BACKGROUND_AGENTS_ORIGINS = frozenset({"autoresearch", "business_knowledge", "task_analysis"})
+
# Exempt from the background run-duration cap, so their tokens need the longer interactive
# ceiling. Mirrors the stages `Task.create_run` stamps.
INTERACTIVE_MINTABLE_PRODUCTS = frozenset({"signals_inbox", "signals_chat"})
@@ -256,6 +260,8 @@ def mint_refusal(
"""Why a routed run must not mint."""
if ai_product == "account_audit" and (origin_product != "account_audit" or not internal):
return "no_account_audit_provenance"
+ if ai_product == "background_agents" and (origin_product not in BACKGROUND_AGENTS_ORIGINS or not internal):
+ return "no_background_agents_provenance"
if ai_product == "slack_app" and not has_slack_provenance(
state, internal=internal, prior_slack_run=prior_slack_run
):
diff --git a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py
index 3b172236c2b1..a0089326789b 100644
--- a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py
+++ b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py
@@ -328,7 +328,9 @@ class TestMintScopedToken:
assert body["product"] == "review_hog"
assert body["allowed_models"] == _PRODUCT_ALLOWED_MODELS["review_hog"]
- @pytest.mark.parametrize("product", ["posthog_ai", "account_audit", "review_hog", "slack_app", "workflows"])
+ @pytest.mark.parametrize(
+ "product", ["posthog_ai", "account_audit", "background_agents", "review_hog", "slack_app", "workflows"]
+ )
def test_model_pin_follows_the_catalog(self, product):
from products.tasks.backend.facade.run_config import RuntimeAdapter, get_models_for_runtime_adapter
from products.tasks.backend.logic.services.gateway_model_pin import SDK_IMPLICIT_MODELS
@@ -580,13 +582,29 @@ class TestMintableGate:
"""Mint scope needs server-side provenance: `internal` and some origin_product
values are API-settable, so a routed-but-unmintable product must never mint."""
- @pytest.mark.parametrize("origin_product", ["image_builder", "business_knowledge"])
- def test_caller_internal_flag_cannot_mint_for_background_agents(self, mint_settings, origin_product):
+ @pytest.mark.parametrize(
+ "origin_product,mints",
+ [
+ ("autoresearch", True),
+ ("business_knowledge", True),
+ ("task_analysis", True),
+ ("image_builder", False),
+ ("user_created", False),
+ (None, False),
+ ],
+ )
+ def test_background_agents_mints_only_for_reserved_origins(self, mint_settings, origin_product, mints):
mint_settings.SANDBOX_AI_GATEWAY_PRODUCTS = "background_agents"
- with patch("products.tasks.backend.temporal.process_task.utils.mint_scoped_token") as mint:
+ with patch(
+ "products.tasks.backend.temporal.process_task.utils.mint_scoped_token", return_value="phe_abc"
+ ) as mint:
env = ai_gateway_env_vars(team_id=123, origin_product=origin_product, internal=True)
- assert "AI_GATEWAY_TOKEN" not in env
- mint.assert_not_called()
+ assert ("AI_GATEWAY_TOKEN" in env) is mints
+ if mints:
+ assert env["AI_GATEWAY_PRODUCT"] == "background_agents"
+ mint.assert_called_once_with(ai_product="background_agents", team_id=123, user=None)
+ else:
+ mint.assert_not_called()
def test_routed_review_hog_mints(self, mint_settings):
mint_settings.SANDBOX_AI_GATEWAY_PRODUCTS = "review_hog"