-
Notifications
You must be signed in to change notification settings - Fork 72
feat: dedicated AI endpoint routing #656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
carlos-marchal-ph
merged 3 commits into
main
from
feat(aiobs)/dedicated-ai-endpoint-routing
Jun 10, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| pypi/posthog: patch | ||
| --- | ||
|
|
||
| Add internal-only routing of `$ai_*` events to a dedicated capture endpoint in their own batch, gated behind the unstable `_dedicated_ai_endpoint` client option (off by default, not for general use). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import unittest | ||
| from queue import Queue | ||
| from unittest import mock | ||
|
|
||
| from parameterized import parameterized | ||
|
|
||
| from posthog.client import Client | ||
| from posthog.consumer import AI_MAX_MSG_SIZE, Consumer | ||
| from posthog.request import AI_EVENTS_ENDPOINT, EVENTS_ENDPOINT | ||
| from posthog.test.test_utils import TEST_API_KEY | ||
|
|
||
|
|
||
| def _event(name: str) -> dict: | ||
| return {"type": "capture", "event": name, "distinct_id": "distinct_id"} | ||
|
|
||
|
|
||
| class TestDedicatedAiEndpointConsumer(unittest.TestCase): | ||
| def test_routes_ai_and_analytics_to_separate_endpoints(self) -> None: | ||
| consumer = Consumer(None, TEST_API_KEY, dedicated_ai_endpoint=True) | ||
| with mock.patch("posthog.consumer.batch_post") as mock_post: | ||
| consumer.request([_event("$ai_generation"), _event("button_clicked")]) | ||
|
|
||
| by_path = { | ||
| c.kwargs["path"]: c.kwargs["batch"] for c in mock_post.call_args_list | ||
| } | ||
| self.assertEqual(set(by_path), {EVENTS_ENDPOINT, AI_EVENTS_ENDPOINT}) | ||
| self.assertEqual( | ||
| [e["event"] for e in by_path[AI_EVENTS_ENDPOINT]], ["$ai_generation"] | ||
| ) | ||
| self.assertEqual( | ||
| [e["event"] for e in by_path[EVENTS_ENDPOINT]], ["button_clicked"] | ||
| ) | ||
|
|
||
| def test_only_ai_events_single_call_to_ai_endpoint(self) -> None: | ||
| consumer = Consumer(None, TEST_API_KEY, dedicated_ai_endpoint=True) | ||
| with mock.patch("posthog.consumer.batch_post") as mock_post: | ||
| consumer.request([_event("$ai_generation"), _event("$ai_embedding")]) | ||
|
|
||
| self.assertEqual(mock_post.call_count, 1) | ||
| self.assertEqual(mock_post.call_args.kwargs["path"], AI_EVENTS_ENDPOINT) | ||
|
|
||
| def test_disabled_routes_everything_to_batch(self) -> None: | ||
| consumer = Consumer(None, TEST_API_KEY, dedicated_ai_endpoint=False) | ||
| with mock.patch("posthog.consumer.batch_post") as mock_post: | ||
| consumer.request([_event("$ai_generation"), _event("button_clicked")]) | ||
|
|
||
| self.assertEqual(mock_post.call_count, 1) | ||
| self.assertEqual(mock_post.call_args.kwargs["path"], EVENTS_ENDPOINT) | ||
|
|
||
|
|
||
| class TestDedicatedAiEndpointSyncMode(unittest.TestCase): | ||
| @parameterized.expand( | ||
| [ | ||
| ("enabled_ai_event", True, "$ai_generation", AI_EVENTS_ENDPOINT), | ||
| ("enabled_normal_event", True, "button_clicked", EVENTS_ENDPOINT), | ||
| ("disabled_ai_event", False, "$ai_generation", EVENTS_ENDPOINT), | ||
| ] | ||
| ) | ||
| def test_routes_event_to_expected_endpoint( | ||
| self, _name, enabled, event, expected_path | ||
| ) -> None: | ||
| client = Client(TEST_API_KEY, sync_mode=True, _dedicated_ai_endpoint=enabled) | ||
| with mock.patch("posthog.client.batch_post") as mock_post: | ||
| client.capture(event, distinct_id="distinct_id") | ||
|
|
||
| self.assertEqual(mock_post.call_args.kwargs["path"], expected_path) | ||
|
|
||
|
|
||
| class TestDedicatedAiEndpointSizeLimit(unittest.TestCase): | ||
| def _sized_event(self, name: str, payload_bytes: int) -> dict: | ||
| event = _event(name) | ||
| event["properties"] = {"p": "x" * payload_bytes} | ||
| return event | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ( | ||
| "ai_event_under_ai_limit_kept", | ||
| True, | ||
| "$ai_generation", | ||
| 2 * 1024 * 1024, | ||
| True, | ||
| ), | ||
| ( | ||
| "ai_event_over_ai_limit_dropped", | ||
| True, | ||
| "$ai_generation", | ||
| AI_MAX_MSG_SIZE, | ||
| False, | ||
| ), | ||
| ( | ||
| "analytics_event_over_analytics_limit_dropped", | ||
| True, | ||
| "button_clicked", | ||
| 2 * 1024 * 1024, | ||
| False, | ||
| ), | ||
| ( | ||
| "ai_event_uses_analytics_limit_when_disabled", | ||
| False, | ||
| "$ai_generation", | ||
| 2 * 1024 * 1024, | ||
| False, | ||
| ), | ||
| ] | ||
| ) | ||
| def test_per_event_size_limit( | ||
| self, _name, enabled, event, payload_bytes, expect_kept | ||
| ) -> None: | ||
| q = Queue() | ||
| consumer = Consumer( | ||
| q, TEST_API_KEY, dedicated_ai_endpoint=enabled, flush_interval=0.01 | ||
| ) | ||
| q.put(self._sized_event(event, payload_bytes)) | ||
|
|
||
| batch = consumer.next() | ||
|
|
||
| if expect_kept: | ||
| self.assertEqual([e["event"] for e in batch], [event]) | ||
| else: | ||
| self.assertEqual(batch, []) | ||
| self.assertTrue(q.empty()) | ||
| self.assertEqual(q.unfinished_tasks, 0) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
|
carlos-marchal-ph marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the whole HTTP contract the very same? as in retry logic, request and response payload, etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I just noticed that beyond capping batch sizes on the backend, we also pre-emptively do it on the SDK. Just bumped the AI event size to the 8MB we're gonna be trialing.
Didn't touch batching size for the moment because it was a more comprehensive change, affecting non-AI events too. This means that for events larger than the batch size, we'll just send them as a 1 event batch, this seems reasonable for now.