Skip to content

feat(llma): use PostHog SDK integration for stamphog LLM analytics#53008

Merged
andrewm4894 merged 2 commits into
masterfrom
feat/llma-stamphog-use-sdk-integration
Apr 7, 2026
Merged

feat(llma): use PostHog SDK integration for stamphog LLM analytics#53008
andrewm4894 merged 2 commits into
masterfrom
feat/llma-stamphog-use-sdk-integration

Conversation

@andrewm4894
Copy link
Copy Markdown
Member

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: Import query from posthoganalytics.ai.claude_agent_sdk instead of claude_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: Add posthoganalytics to 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:

  • 16 unit tests passing in posthog-python
  • Live tested against EU PostHog project with multi-turn tool-calling queries
  • Verified generation, span, and trace events appear correctly in LLM Analytics dashboard

No changes to stamphog's review behavior — the query() wrapper is a transparent pass-through.

Publish to changelog?

No

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.
@andrewm4894 andrewm4894 requested a review from a team as a code owner April 1, 2026 11:19
@andrewm4894 andrewm4894 self-assigned this Apr 1, 2026
@assign-reviewers-posthog assign-reviewers-posthog Bot requested a review from a team April 1, 2026 11:19
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 1, 2026

Prompt To Fix All With AI
This 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

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 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".

Comment thread tools/pr-approval-agent/reviewer.py Outdated
Comment thread tools/pr-approval-agent/reviewer.py Outdated
Comment thread tools/pr-approval-agent/reviewer.py Outdated
The fallback claude_agent_sdk.query() does not accept posthog_*
kwargs, so passing them unconditionally would raise TypeError when
posthoganalytics is not installed.
Copy link
Copy Markdown
Contributor

@webjunkie webjunkie left a comment

Choose a reason for hiding this comment

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

🎉

Copy link
Copy Markdown
Member

@Piccirello Piccirello left a comment

Choose a reason for hiding this comment

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

Siiiiick

@andrewm4894 andrewm4894 merged commit 5e042b4 into master Apr 7, 2026
155 checks passed
@andrewm4894 andrewm4894 deleted the feat/llma-stamphog-use-sdk-integration branch April 7, 2026 17:31
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.

4 participants