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(llma): alert on unhandled Max agent run errors (#62611)

Georgiy Tarasov3ab7d935bc18
diff --git a/ee/hogai/core/agent_modes/test/test_toolkit_imports.py b/ee/hogai/core/agent_modes/test/test_toolkit_imports.py
new file mode 100644
index 000000000000..a0211435ab23
--- /dev/null
+++ b/ee/hogai/core/agent_modes/test/test_toolkit_imports.py
@@ -0,0 +1,45 @@
+import pkgutil
+import importlib
+
+from posthog.test.base import BaseTest
+
+from langchain_core.runnables import RunnableConfig
+from parameterized import parameterized
+
+import ee.hogai.core.agent_modes.presets as presets_package
+from ee.hogai.context import AssistantContextManager
+from ee.hogai.core.agent_modes.factory import AgentModeDefinition
+from ee.hogai.core.agent_modes.toolkit import AgentToolkit
+
+
+def _discover_toolkit_classes() -> list[tuple[str, type[AgentToolkit]]]:
+ """Every toolkit class reachable from a preset AgentModeDefinition.
+
+ Toolkits load their tools via lazy `from products... import` inside the
+ `tools` property, so a renamed or moved module is invisible until that
+ property runs at runtime. Discovering the classes here lets the test below
+ force those imports — new presets are covered automatically.
+ """
+ toolkit_classes: dict[str, type[AgentToolkit]] = {}
+ for module_info in pkgutil.iter_modules(presets_package.__path__):
+ if module_info.name == "test":
+ continue
+ module = importlib.import_module(f"{presets_package.__name__}.{module_info.name}")
+ for definition in vars(module).values():
+ if isinstance(definition, AgentModeDefinition):
+ toolkit_classes.setdefault(definition.toolkit_class.__name__, definition.toolkit_class)
+ return sorted(toolkit_classes.items())
+
+
+class TestToolkitImports(BaseTest):
+ @parameterized.expand(_discover_toolkit_classes())
+ def test_toolkit_tools_property_imports_resolve(self, _name: str, toolkit_class: type[AgentToolkit]) -> None:
+ context_manager = AssistantContextManager(
+ team=self.team, user=self.user, config=RunnableConfig(configurable={})
+ )
+ toolkit = toolkit_class(team=self.team, user=self.user, context_manager=context_manager)
+ # Accessing `.tools` runs the lazy imports; a stale import raises here.
+ assert isinstance(toolkit.tools, list)
+
+ def test_discovers_toolkits(self) -> None:
+ assert len(_discover_toolkit_classes()) > 0
diff --git a/ee/hogai/core/runner.py b/ee/hogai/core/runner.py
index 9be0dbc98729..488601fb3601 100644
--- a/ee/hogai/core/runner.py
+++ b/ee/hogai/core/runner.py
@@ -47,6 +47,7 @@ from ee.hogai.core.base import BaseAssistantGraph
from ee.hogai.core.stream_processor import AssistantStreamProcessorProtocol
from ee.hogai.tool import ApprovalRequest
from ee.hogai.utils.exceptions import (
+ AGENT_RUN_UNHANDLED_ERROR_COUNTER,
HTTPX_TRANSPORT_EXCEPTIONS,
LLM_API_EXCEPTIONS,
LLM_CLIENT_ERROR_COUNTER,
@@ -417,6 +418,7 @@ class BaseAgentRunner(ABC):
await self._graph.aupdate_state(config, self._partial_state_type.get_reset_state())
if not isinstance(e, GenerationCanceled):
+ AGENT_RUN_UNHANDLED_ERROR_COUNTER.labels(error_type=type(e).__name__).inc()
logger.exception("Error in assistant stream", error=e)
self._capture_exception(e)
diff --git a/ee/hogai/utils/exceptions.py b/ee/hogai/utils/exceptions.py
index c71486c347f2..7ae773189bfa 100644
--- a/ee/hogai/utils/exceptions.py
+++ b/ee/hogai/utils/exceptions.py
@@ -53,6 +53,17 @@ LLM_TRANSPORT_ERROR_COUNTER = Counter(
["error_type"],
)
+# Unexpected exceptions that crash an agent run (e.g. a code bug or a stale import),
+# as opposed to the known LLM provider/client/transport errors counted above. These
+# bubble to the catch-all in the runner and return a FailureMessage to the user — an
+# agent generation that never finished. Sits at ~0 in steady state, so a sustained
+# rate signals that a recent change broke an agent path in production.
+AGENT_RUN_UNHANDLED_ERROR_COUNTER = Counter(
+ "posthog_ai_agent_run_unhandled_errors_total",
+ "Total number of agent runs that failed with an unexpected (non-LLM) exception",
+ ["error_type"],
+)
+
def resolve_llm_provider(exc: Exception) -> str:
"""Extract the LLM provider name from an exception.