: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(aio): track missing evaluation executions (#109095)
Bernat Torresb2d0d35fca86
diff --git a/posthog/temporal/ai_observability/evaluation_workflow_activities.py b/posthog/temporal/ai_observability/evaluation_workflow_activities.pyindex 143eeccb7b4e..20b53e3274e7 100644--- a/posthog/temporal/ai_observability/evaluation_workflow_activities.py+++ b/posthog/temporal/ai_observability/evaluation_workflow_activities.py@@ -1,6 +1,8 @@+import osimport jsonimport uuidimport hashlib+from collections.abc import Mappingfrom dataclasses import dataclassfrom datetime import datetime, timedeltafrom typing import Any@@ -353,6 +355,41 @@ def _evaluation_event_uuid() -> str | None:return str(uuid.uuid5(uuid.NAMESPACE_URL, f"posthog://ai-evaluation/{workflow_id}"))+def capture_evaluation_run_usage(+ evaluation: Mapping[str, object],+ result: EvaluationActivityResult,+ *,+ team_id: int,+) -> None:+ properties: dict[str, object] = {+ "evaluation_id": str(evaluation["id"]),+ "team_id": team_id,+ "result_type": result["result_type"],+ "status": "skipped" if result.get("skipped") else "completed",+ }+ if evaluation.get("evaluation_type", "llm_judge") == "llm_judge" and result.get("model"):+ properties.update(+ {+ key: value+ for key, value in result.items()+ if key in ("model", "provider", "input_tokens", "output_tokens", "total_tokens")+ }+ )+ if "verdict" in result and not result.get("skipped"):+ properties["verdict"] = result["verdict"]+ from posthog.tasks.usage_report import get_ph_client # noqa: PLC0415 - keeps billing imports off worker startup++ organization_id = str(Team.objects.filter(id=team_id).values_list("organization_id", flat=True).get())+ ph_client = get_ph_client(sync_mode=True, disabled=bool(settings.TEST or os.environ.get("OPT_OUT_CAPTURE", False)))+ ph_client.capture(+ distinct_id=f"org-{organization_id}",+ event="llm analytics evaluation executed",+ properties=properties,+ groups={"organization": organization_id, "instance": settings.SITE_URL},+ )+ ph_client.flush()++async def emit_generation_evaluation_event(inputs: EmitEvaluationEventInputs) -> None:"""Emit the $ai_evaluation event via capture_internal so it routes through the ingestionpipeline for cost calculation. A billing-limited capture drops the event without failing@@ -410,6 +447,13 @@ async def emit_generation_evaluation_event(inputs: EmitEvaluationEventInputs) ->properties=properties,event_uuid=_evaluation_event_uuid(),)+ # Completed LLM judge runs emit telemetry in the workflow's separate activity.+ if evaluation.get("evaluation_type", "llm_judge") != "llm_judge" or result.get("skipped"):+ try:+ capture_evaluation_run_usage(evaluation, result, team_id=event_data["team_id"])+ except Exception:+ # Telemetry failures must not retry an already emitted evaluation.+ logger.warning("evaluation_usage_capture_failed", team_id=event_data["team_id"], exc_info=True)try:await database_sync_to_async(_emit, thread_sensitive=False)()@@ -449,35 +493,11 @@ class EmitInternalTelemetryInputs:@temporalio.activity.defnasync def emit_internal_telemetry_activity(inputs: EmitInternalTelemetryInputs) -> None:"""Emit telemetry event to PostHog org for internal tracking."""- from posthog.tasks.usage_report import get_ph_client-- evaluation = inputs.evaluation- team_id = inputs.team_id- result = inputs.result-- def _emit_telemetry() -> None:- organization_id = str(Team.objects.filter(id=team_id).values_list("organization_id", flat=True).get())-- ph_client = get_ph_client(sync_mode=True)- ph_client.capture(- distinct_id=f"org-{organization_id}",- event="llm analytics evaluation executed",- properties={- "evaluation_id": evaluation["id"],- "team_id": team_id,- "model": result.get("model", DEFAULT_JUDGE_MODEL),- "provider": result.get("provider", "openai"),- "input_tokens": result.get("input_tokens", 0),- "output_tokens": result.get("output_tokens", 0),- "total_tokens": result.get("total_tokens", 0),- **({"verdict": result["verdict"]} if "verdict" in result else {}),- "result_type": result["result_type"],- },- groups={"organization": organization_id, "instance": settings.SITE_URL},- )- ph_client.flush()-- await database_sync_to_async(_emit_telemetry, thread_sensitive=False)()+ await database_sync_to_async(capture_evaluation_run_usage, thread_sensitive=False)(+ inputs.evaluation,+ inputs.result,+ team_id=inputs.team_id,+ )@frozendiff --git a/posthog/temporal/ai_observability/run_trace_evaluation.py b/posthog/temporal/ai_observability/run_trace_evaluation.pyindex b4251d198c4c..2eb5b1d0fe95 100644--- a/posthog/temporal/ai_observability/run_trace_evaluation.py+++ b/posthog/temporal/ai_observability/run_trace_evaluation.py@@ -59,6 +59,7 @@ from posthog.temporal.ai_observability.evaluation_workflow_activities import (RunEvaluationInputs,backfill_verdict_timestamp,build_evaluation_event_properties,+ capture_evaluation_run_usage,emit_internal_telemetry_activity,fetch_evaluation_activity,)@@ -818,6 +819,13 @@ async def emit_trace_evaluation_event_activity(inputs: EmitTraceEvaluationEventItimestamp=timestamp,properties=properties,)+ # Completed runs emit telemetry in the workflow's separate activity.+ if inputs.result.get("skipped"):+ try:+ capture_evaluation_run_usage(inputs.evaluation, inputs.result, team_id=inputs.team_id)+ except Exception:+ # Telemetry failures must not retry an already emitted evaluation.+ logger.warning("evaluation_usage_capture_failed", team_id=inputs.team_id, exc_info=True)try:await database_sync_to_async(_emit, thread_sensitive=False)()diff --git a/posthog/temporal/ai_observability/test_run_evaluation.py b/posthog/temporal/ai_observability/test_run_evaluation.pyindex 7b9f0ba8f923..116f4f207ba1 100644--- a/posthog/temporal/ai_observability/test_run_evaluation.py+++ b/posthog/temporal/ai_observability/test_run_evaluation.py@@ -58,14 +58,18 @@ from .evaluation_llm_judge import (JUDGE_EVENT_MAX_CHARS,NumericWithNAEvalResult,TransientJudgeError,+ _build_errored_trace_result,+ _build_output_limit_skip_result,_execute_llm_judge_activity,call_llm_judge,get_output_type_config,)+from .evaluation_types import build_skipped_evaluation_resultfrom .evaluation_workflow_activities import (LocalEvaluationOutcome,backfill_verdict_timestamp,build_evaluation_event_properties,+ capture_evaluation_run_usage,emit_internal_telemetry_activity,)from .run_evaluation import (@@ -91,6 +95,201 @@ from .run_evaluation import (run_local_evaluation_activity,send_evaluation_disabled_email_activity,)+from .run_trace_evaluation import EmitTraceEvaluationEventInputs, emit_trace_evaluation_event_activity++[email protected]("skipped", [False, True])+ "target,evaluation_type,result",+ [+ (target, evaluation_type, result)+ for target in ["generation", "trace", "session"]+ for evaluation_type in ["hog", "llm_judge"]+ for result in [+ {"result_type": "boolean", "verdict": True},+ {"result_type": "numeric", "score": 123.45},+ {"result_type": "categorical", "categories": ["private_key"]},+ ]+ ]+ + [+ ("generation", "sentiment", {"result_type": "sentiment", "sentiment_score": 0.9, "sentiment_label": "positive"})+ ],+)+async def test_execution_telemetry_covers_output_types(+ target: str, evaluation_type: str, result: EvaluationActivityResult, skipped: bool+) -> None:+ evaluation = {"id": "test-evaluation", "name": "Example evaluation", "evaluation_type": evaluation_type}+ result = {**result, "skipped": skipped, "reasoning": "Private content"}+ model_usage = {+ "model": "example-model",+ "provider": "example-provider",+ "input_tokens": 42,+ "output_tokens": 8,+ "total_tokens": 50,+ }+ if skipped:+ if evaluation_type == "sentiment":+ result = {"result_type": "sentiment", "reasoning": "No user messages", "skipped": True}+ else:+ result = build_skipped_evaluation_result(+ output_type=result["result_type"], allows_na=False, reasoning="Private content", skip_reason="example"+ )+ elif evaluation_type == "llm_judge":+ result.update(+ {+ "model": "example-model",+ "provider": "example-provider",+ "input_tokens": 42,+ "output_tokens": 8,+ "total_tokens": 50,+ }+ )+ module = "posthog.temporal.ai_observability.evaluation_workflow_activities"+ with (+ override_settings(SITE_URL="https://example.com"),+ patch(f"{module}.Team.objects.filter") as teams,+ patch("posthog.tasks.usage_report.get_ph_client") as capture,+ patch(f"{module}.capture_ai_internal_for_team"),+ patch("posthog.temporal.ai_observability.run_trace_evaluation.capture_ai_internal_for_team"),+ ):+ teams.return_value.values_list.return_value.get.return_value = "test-org"+ if target == "generation":+ await emit_evaluation_event_activity(+ EmitEvaluationEventInputs(+ evaluation=evaluation,+ event_data=create_mock_event_data(1),+ result=result,+ start_time=datetime(2026, 9, 1, tzinfo=UTC),+ )+ )+ else:+ await emit_trace_evaluation_event_activity(+ EmitTraceEvaluationEventInputs(+ evaluation=evaluation,+ team_id=1,+ trace_id="example-trace",+ distinct_id="example-user",+ session_id=None,+ result=result,+ start_time=datetime(2026, 9, 1, tzinfo=UTC),+ target=target,+ ai_session_id="example-session",+ )+ )+ if not skipped and (target != "generation" or evaluation_type == "llm_judge"):+ capture.assert_not_called()+ await emit_internal_telemetry_activity(+ EmitInternalTelemetryInputs(evaluation=evaluation, team_id=1, result=result)+ )++ capture.assert_called_once_with(sync_mode=True, disabled=True)+ capture.return_value.flush.assert_called_once_with()+ capture.return_value.capture.assert_called_once_with(+ distinct_id="org-test-org",+ event="llm analytics evaluation executed",+ properties={+ "evaluation_id": "test-evaluation",+ "team_id": 1,+ **(model_usage if evaluation_type == "llm_judge" and not skipped else {}),+ **({"verdict": result["verdict"]} if "verdict" in result and not skipped else {}),+ "result_type": result["result_type"],+ "status": "skipped" if skipped else "completed",+ },+ groups={"organization": "test-org", "instance": "https://example.com"},+ )++[email protected]("model_called", [False, True])+def test_skipped_usage_only_includes_actual_model_metadata(model_called: bool) -> None:+ result = (+ _build_output_limit_skip_result(+ False, is_byok=False, key_id=None, provider="example-provider", model="example-model"+ )+ if model_called+ else _build_errored_trace_result(False)+ )+ module = "posthog.temporal.ai_observability.evaluation_workflow_activities"+ with patch(f"{module}.Team.objects.filter"), patch("posthog.tasks.usage_report.get_ph_client") as capture:+ capture_evaluation_run_usage({"id": "test-evaluation", "evaluation_type": "llm_judge"}, result, team_id=1)++ assert capture.return_value.capture.call_args.kwargs["properties"] == {+ "evaluation_id": "test-evaluation",+ "team_id": 1,+ "result_type": "boolean",+ "status": "skipped",+ **({"model": "example-model", "provider": "example-provider"} if model_called else {}),+ }++[email protected]("target", ["telemetry", "generation", "trace", "session"])[email protected]("failure", ["lookup", "capture"])+async def test_usage_failure_preserves_activity_behavior(target: str, failure: str) -> None:+ module = "posthog.temporal.ai_observability.evaluation_workflow_activities"+ with (+ patch(f"{module}.Team.objects.filter") as teams,+ patch("posthog.tasks.usage_report.get_ph_client") as capture,+ patch(f"{module}.capture_ai_internal_for_team") as generation_capture,+ patch("posthog.temporal.ai_observability.run_trace_evaluation.capture_ai_internal_for_team") as trace_capture,+ ):+ failing_call = teams if failure == "lookup" else capture.return_value.capture+ failing_call.side_effect = RuntimeError("telemetry unavailable")+ evaluation = {"id": "test-evaluation", "name": "Example evaluation", "evaluation_type": "hog"}+ result: EvaluationActivityResult = {"result_type": "numeric", "skipped": True, "reasoning": "Example"}+ if target == "telemetry":+ with pytest.raises(RuntimeError, match="telemetry unavailable"):+ await emit_internal_telemetry_activity(+ EmitInternalTelemetryInputs(evaluation=evaluation, team_id=1, result=result)+ )+ elif target == "generation":+ await emit_evaluation_event_activity(+ EmitEvaluationEventInputs(+ evaluation=evaluation,+ event_data=create_mock_event_data(1),+ result=result,+ start_time=datetime(2026, 9, 1, tzinfo=UTC),+ )+ )+ generation_capture.assert_called_once()+ else:+ await emit_trace_evaluation_event_activity(+ EmitTraceEvaluationEventInputs(+ evaluation=evaluation,+ team_id=1,+ trace_id="example-trace",+ distinct_id="example-user",+ session_id=None,+ result=result,+ start_time=datetime(2026, 9, 1, tzinfo=UTC),+ target=target,+ ai_session_id="example-session",+ )+ )+ trace_capture.assert_called_once()+++def test_execution_telemetry_does_not_add_destination_region() -> None:+ events: list[dict[str, Any]] = []++ def record_event(event: dict[str, Any]) -> dict[str, Any]:+ events.append(event)+ return event++ client = posthoganalytics.Client("example-key", send=False, before_send=record_event)+ try:+ with (+ override_settings(CLOUD_DEPLOYMENT="EU"),+ patch("posthog.temporal.ai_observability.evaluation_workflow_activities.Team.objects.filter"),+ patch("posthog.tasks.usage_report.get_ph_client", return_value=client),+ ):+ capture_evaluation_run_usage(+ {"id": "test-evaluation"}, {"result_type": "numeric", "score": 1, "reasoning": "Example"}, team_id=1+ )+ assert len(events) == 1+ assert "region" not in events[0]["properties"]+ finally:+ client.shutdown()def _mock_config_with_active_key(provider: str = "openai") -> MagicMock:@@ -830,13 +1029,13 @@ class TestRunEvaluationWorkflow:"reasoning": "Customer content","allows_na": False,}- with patch("posthog.tasks.usage_report.get_ph_client") as get_client:+ with patch("posthog.tasks.usage_report.get_ph_client") as capture:await emit_internal_telemetry_activity(EmitInternalTelemetryInputs(evaluation=evaluation, team_id=setup_data["team"].id, result=result))- get_client.return_value.capture.assert_called_once()- properties = get_client.return_value.capture.call_args.kwargs["properties"]+ capture.return_value.capture.assert_called_once()+ properties = capture.return_value.capture.call_args.kwargs["properties"]assert properties["result_type"] == "numeric"assert "score" not in propertiesassert "reasoning" not in properties