feat(llma): use PostHog SDK integration for stamphog LLM analytics#53008
Merged
Conversation
Replace direct claude_agent_sdk.query() with posthog.ai.claude_agent_sdk.query() to automatically capture $ai_generation, $ai_span, and $ai_trace events. Falls back to plain claude_agent_sdk.query() if posthoganalytics is not installed, so stamphog continues to work without analytics as before.
Contributor
Prompt To Fix All With AIThis is a comment left during a code review.
Path: tools/pr-approval-agent/reviewer.py
Line: 19-22
Comment:
**Fallback `query` call will always raise `TypeError`**
When `posthoganalytics` is not installed, `from claude_agent_sdk import query` is used as the fallback. However, the call site unconditionally passes `posthog_distinct_id="stamphog"` and `posthog_properties=posthog_props` to whichever `query` was imported. The base `claude_agent_sdk.query` does not accept those keyword arguments, so every invocation via the fallback path will raise:
```
TypeError: query() got an unexpected keyword argument 'posthog_distinct_id'
```
Since `posthog-python#477` (the dependency that adds `posthoganalytics.ai.claude_agent_sdk`) may not yet be released, the fallback is likely to be the common path in the near term, making this a reliable failure.
A minimal fix is to track availability and only pass the extra kwargs when the posthog wrapper is in use:
```python
try:
from posthoganalytics.ai.claude_agent_sdk import query
_POSTHOG_AI_AVAILABLE = True
except ImportError:
from claude_agent_sdk import query # type: ignore[no-redef]
_POSTHOG_AI_AVAILABLE = False
```
Then at the call site (lines 229–234):
```python
posthog_kwargs = (
{"posthog_distinct_id": "stamphog", "posthog_properties": posthog_props}
if _POSTHOG_AI_AVAILABLE
else {}
)
async for message in query(prompt=prompt, options=options, **posthog_kwargs):
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "feat(llma): use PostHog SDK integration ..." | Re-trigger Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 65538b7377
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The fallback claude_agent_sdk.query() does not accept posthog_* kwargs, so passing them unconditionally would raise TypeError when posthoganalytics is not installed.
This was referenced Apr 1, 2026
rafaeelaudibert
approved these changes
Apr 1, 2026
MattBro
pushed a commit
that referenced
this pull request
Apr 8, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The stamphog PR approval agent has no observability into LLM performance, costs, and behavior. PR #52984 added a hand-rolled 202-line analytics module, but PostHog's Python SDK now has a proper Claude Agent SDK integration (
posthog.ai.claude_agent_sdk) that handles this automatically.Changes
Minimal change (21 lines added) to wire stamphog into the SDK integration:
reviewer.py: Importqueryfromposthoganalytics.ai.claude_agent_sdkinstead ofclaude_agent_sdk. Falls back to the original if posthoganalytics is not installed. Passes PR metadata (number, repo, author, tier, gate verdict) as custom properties.review_pr.py: Addposthoganalyticsto PEP 723 script dependencies.The SDK integration automatically emits
$ai_generation(per LLM turn),$ai_span(per tool use), and$ai_trace(per query) events with token counts, costs, latency, input/output, and cache metrics.Depends on PostHog/posthog-python#477.
How did you test this code?
This is an agent-authored change. The PostHog SDK integration was tested separately:
No changes to stamphog's review behavior — the
query()wrapper is a transparent pass-through.Publish to changelog?
No