Skip to content

Conversation

@daibhin
Copy link
Contributor

@daibhin daibhin commented Nov 28, 2025

Related to PostHog/posthog-js#2593

  • Capture exception events (if enabled) when an error is thrown in LLMA
  • Add exception_id to LLMA traces / spans so we can better integrate the two in the app

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile

andrewm4894 and others added 6 commits January 6, 2026 13:58
Resolved conflicts:
- CHANGELOG.md: Added 7.5.0 entry for error tracking integration
- posthog/ai/utils.py: Kept refactored tag-based approach with sanitize_messages
- posthog/version.py: Bumped to 7.5.0

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add 6 tests covering the new LLMA + error tracking integration:
- capture_exception called on span/generation errors
- $exception_event_id added to AI events
- No capture when autocapture disabled
- AI properties passed to exception event
- Handles None return from capture_exception

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Export get_tags() from posthog module
- Explicitly pass context tags to capture() in AI utils
- Fix $ai_model fallback to extract from response.model
- Fix ruff formatting in langchain test_callbacks.py

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
The new_context() defaults to capture_exceptions=True which would
auto-capture any exception regardless of enable_exception_autocapture
setting. This was inconsistent with LangChain callbacks which
explicitly check the setting.

Pass capture_exceptions=False to let exception handling be controlled
explicitly by the enable_exception_autocapture setting.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Use fresh=True to start with a clean context for each LLM call.
This avoids inheriting $ai_* tags from parent contexts which could
cause mismatched AI metadata due to the tag merge order bug in
contexts.py (parent tags incorrectly override child tags).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
The collect_tags() method had a bug where parent tags would overwrite
child tags, despite the comment saying the opposite. This fix ensures
child context tags properly override parent tags.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
andrewm4894 and others added 2 commits January 7, 2026 12:43
With the collect_tags() bug fixed, child tags properly override parent
tags. LLM events can now inherit useful parent context tags (request_id,
user info, etc.) while still having their $ai_* tags take precedence.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Verifies that in non-fresh contexts, child tags properly override
parent tags with the same key while still inheriting other parent tags.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@andrewm4894
Copy link
Member

Summary of current state

This PR adds error tracking integration for LLM observability, primarily for LangChain.

What it does

LangChain integration (complete)

  • When an error occurs during LangChain LLM calls, capture_exception() is called if enable_exception_autocapture is enabled on the client
  • The $exception_event_id is added to the $ai_generation event properties, linking the two events
  • Exception events include all relevant AI properties ($ai_trace_id, $ai_provider, etc.)

OpenAI/Anthropic/Gemini wrappers (partial)

  • Wrapped in new_context() for future error tracking support
  • Exception capture not yet implemented for these - planned for a follow-up PR

Additional fixes included

  • Fixed bug in contexts.py where parent tags incorrectly overwrote child tags (merge order was backwards)
  • Exported get_tags() from posthog module to support passing context tags to capture calls
  • Added capture_exceptions=False to LLM contexts to prevent automatic exception capture (should be controlled by enable_exception_autocapture setting instead)

Test coverage

  • 7 new tests for LangChain exception autocapture (enabled/disabled, exception IDs, property passing)
  • 1 new test for tag merge order fix

Document that exception capture needs to be added for the direct SDK
wrappers, similar to how it's implemented in LangChain callbacks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@andrewm4894 andrewm4894 merged commit 4e4cd18 into master Jan 7, 2026
15 checks passed
@andrewm4894 andrewm4894 deleted the dn-feat/llma-meets-error-tracking branch January 7, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants