Skip to content

feat: dedicated AI endpoint routing#656

Merged
carlos-marchal-ph merged 3 commits into
mainfrom
feat(aiobs)/dedicated-ai-endpoint-routing
Jun 10, 2026
Merged

feat: dedicated AI endpoint routing#656
carlos-marchal-ph merged 3 commits into
mainfrom
feat(aiobs)/dedicated-ai-endpoint-routing

Conversation

@carlos-marchal-ph

Copy link
Copy Markdown
Contributor

Problem

AI Observability wants $ai_* events isolated into their own batch to a dedicated capture endpoint so AI and analytics workloads stop affecting each other, and different settings can be applied to each.

Changes

Sends $ai_ prefixed events to a dedicated endpoint.

Gated behind _internal_dedicated_ai_endpoint. When off, behaviour is byte-identical to today.

💚 How did you test it?

I'm an agent (Claude Code), paired with Carlos. Added posthog/test/test_dedicated_ai_endpoint.py (6 cases): consumer routing of mixed batches to the two endpoints, AI-only batches, the disabled no-op, and the sync_mode path for both AI and non-AI events. New tests pass; the touched suites (test_consumer, test_request, test_client_fork) also pass. No manual testing claimed.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog. (additive; off by default behind an internal flag)

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Authored with Claude Code (agent), paired with Carlos, as the python port of the posthog-js change. Design note: python's consumer materializes each batch in memory before POSTing (no persisted contiguous-removal invariant like posthog-node), so the clean approach here is to partition the batch at flush in the consumer rather than maintain separate queues. We target the existing analytics /batch/ handler via a dedicated path (/i/v0/ai/batch/) — not the dead multipart /i/v0/ai endpoint. Still pending before this is live: the backend route alias + charts Contour ingress entry, and confirmation the AI pipeline's per-message size limit is raised (WarpStream, per RFC #1111) so oversized events stop being dropped. Requires human review.

@carlos-marchal-ph carlos-marchal-ph requested a review from a team as a code owner June 9, 2026 09:52
@greptile-apps

greptile-apps Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 3
posthog/consumer.py:147-152
**AI events silently dropped when analytics send fails**

If `_send(analytics_events, EVENTS_ENDPOINT)` exhausts its retries and raises, the exception propagates immediately — the `_send(ai_events, AI_EVENTS_ENDPOINT)` call is never reached. Both sub-lists were dequeued from the shared queue and `task_done()` will be called for every item in `upload()`, so the AI events are permanently lost without any send being attempted. The docstring says "a failure on one does not re-send the other" which implies symmetric independence, but the ordering creates a dependency: analytics failure silently kills AI delivery too.

### Issue 2 of 3
posthog/consumer.py:147-148
`is_ai_event` is evaluated for every item in `batch` twice — once per list comprehension. A single partitioning loop halves the predicate calls and says the intent once.

```suggestion
        ai_events, analytics_events = [], []
        for item in batch:
            (ai_events if is_ai_event(item.get("event")) else analytics_events).append(item)
```

### Issue 3 of 3
posthog/test/test_dedicated_ai_endpoint.py:46-74
**Sync-mode tests could be parameterised**

The three `TestDedicatedAiEndpointSyncMode` methods differ only in `(flag_enabled, event_name, expected_path)` — a table-driven `subTest` or `parameterized.expand` would express all three cases in one method, which aligns with the team's preference for parameterised tests.

Reviews (1): Last reviewed commit: "feat: dedicated ai endpoint routing" | Re-trigger Greptile

Comment thread posthog/consumer.py Outdated
Comment thread posthog/consumer.py Outdated
Comment thread posthog/test/test_dedicated_ai_endpoint.py
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-10 11:12:15 UTC
Duration: 176093ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 515ms
Format Validation.Event Has Uuid 1506ms
Format Validation.Event Has Lib Properties 1506ms
Format Validation.Distinct Id Is String 1507ms
Format Validation.Token Is Present 1506ms
Format Validation.Custom Properties Preserved 1507ms
Format Validation.Event Has Timestamp 1507ms
Retry Behavior.Retries On 503 9517ms
Retry Behavior.Does Not Retry On 400 3505ms
Retry Behavior.Does Not Retry On 401 3509ms
Retry Behavior.Respects Retry After Header 9513ms
Retry Behavior.Implements Backoff 23516ms
Retry Behavior.Retries On 500 7515ms
Retry Behavior.Retries On 502 7510ms
Retry Behavior.Retries On 504 7509ms
Retry Behavior.Max Retries Respected 23525ms
Deduplication.Generates Unique Uuids 1503ms
Deduplication.Preserves Uuid On Retry 7513ms
Deduplication.Preserves Uuid And Timestamp On Retry 14520ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7505ms
Deduplication.No Duplicate Events In Batch 1508ms
Deduplication.Different Events Have Different Uuids 1506ms
Compression.Sends Gzip When Enabled 1507ms
Batch Format.Uses Proper Batch Structure 1507ms
Batch Format.Flush With No Events Sends Nothing 1005ms
Batch Format.Multiple Events Batched Together 1505ms
Error Handling.Does Not Retry On 403 3507ms
Error Handling.Does Not Retry On 413 3509ms
Error Handling.Retries On 408 7514ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1002ms
Request Payload.Flags Request Uses V2 Query Param 1007ms
Request Payload.Flags Request Hits Flags Path Not Decide 1006ms
Request Payload.Flags Request Omits Authorization Header 1007ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1006ms
Request Payload.Groups Default To Empty Object 1006ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 1007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1006ms
Request Payload.Disable Geoip Omitted Defaults To False 1007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1007ms
Request Lifecycle.No Flags Request On Init Alone 502ms
Request Lifecycle.No Flags Request On Normal Capture 1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1011ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1002ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1509ms

Comment thread posthog/client.py Outdated
Comment thread posthog/request.py
Comment on lines +344 to +345
EVENTS_ENDPOINT = "/batch/"
AI_EVENTS_ENDPOINT = "/i/v0/ai/batch/"

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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.

@marandaneto marandaneto requested a review from dustinbyrne June 9, 2026 12:19
…dicated_ai_endpoint

Generated-By: PostHog Code
Task-Id: 142d3917-41e8-4bb5-ad6c-5778e13041c2

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving to unblock, but still this comment left #656 (comment)

@carlos-marchal-ph carlos-marchal-ph merged commit 00b2091 into main Jun 10, 2026
29 checks passed
@carlos-marchal-ph carlos-marchal-ph deleted the feat(aiobs)/dedicated-ai-endpoint-routing branch June 10, 2026 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants