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(llma): truncate gateway capture payloads below kafka limit (#66092)

Joshua Snyder74665307ced7
diff --git a/services/llm-gateway/src/llm_gateway/callbacks/posthog.py b/services/llm-gateway/src/llm_gateway/callbacks/posthog.py
index b66a986f821f..95134e437c7c 100644
--- a/services/llm-gateway/src/llm_gateway/callbacks/posthog.py
+++ b/services/llm-gateway/src/llm_gateway/callbacks/posthog.py
@@ -50,7 +50,13 @@ def _replace_binary_content(data: Any) -> Any:
return data
-_MAX_CAPTURE_SIZE = 15 * 1024 * 1024
+# Capture rejects events whose Kafka message exceeds message.max.bytes (~1 MB,
+# the librdkafka default) with a 413, and the posthoganalytics client drops
+# events over its own ~900 KB ceiling. $ai_generation events carry the full
+# prompt/completion via $ai_input / $ai_output_choices, so they routinely cross
+# that line. Truncate well below 1 MB to leave headroom for the event envelope
+# (distinct_id, event name, other properties) and JSON re-escaping on the wire.
+_MAX_CAPTURE_SIZE = 800 * 1024
_MIN_FIELD_SIZE_TO_TRUNCATE = 10 * 1024
_TRUNCATION_MARKER = "[truncated: content too large for capture]"
_TRUNCATABLE_FIELDS = ("$ai_output_choices", "$ai_input")
diff --git a/services/llm-gateway/tests/callbacks/test_posthog.py b/services/llm-gateway/tests/callbacks/test_posthog.py
index 8525f1191f0f..1af441df12fb 100644
--- a/services/llm-gateway/tests/callbacks/test_posthog.py
+++ b/services/llm-gateway/tests/callbacks/test_posthog.py
@@ -7,6 +7,7 @@ import pytest
from llm_gateway.auth.models import AuthenticatedUser
from llm_gateway.callbacks.posthog import (
+ _MAX_CAPTURE_SIZE,
PostHogCallback,
_normalize_trace_id,
_replace_binary_content,
@@ -943,7 +944,7 @@ class TestReplaceBinaryContent:
assert _replace_binary_content(input_data) == expected
-_MAX_SIZE = 15 * 1024 * 1024
+_MAX_SIZE = _MAX_CAPTURE_SIZE
_TRUNCATION_MARKER = "[truncated: content too large for capture]"
@@ -974,6 +975,11 @@ class TestTruncateForCapture:
result = _truncate_for_capture(properties)
assert result == properties
+ def test_threshold_below_kafka_message_limit(self) -> None:
+ # Capture rejects events over Kafka's message.max.bytes (~1 MB) with a 413,
+ # so the truncation threshold must stay under it with headroom for the envelope.
+ assert _MAX_CAPTURE_SIZE < 1_000_000
+
def test_large_output_truncated(self) -> None:
large_output = "x" * (_MAX_SIZE + 1)
properties = {