Skip to content

Conversation

RulaKhaled
Copy link
Member

@RulaKhaled RulaKhaled commented Oct 16, 2025

This PR adds automatic instrumentation for LangChain chat clients in Node SDK, we cover most used providers mentioned in https://python.langchain.com/docs/integrations/chat/.

What's added?

TLDR; a LangChain Callback Handler that:

  • Creates a stateful callback handler that tracks LangChain lifecycle events
  • Handles LLM/Chat Model events (start, end, error, streaming)
  • Handles Chain events (start, end, error)
  • Handles Tool events (start, end, error)
  • Extracts and normalizes request/respo

How it works?

  1. Module Patching: When a LangChain provider package is loaded (e.g., @langchain/anthropic), the instrumentation:

    • Finds the chat model class (e.g., ChatAnthropic)
    • Wraps the invoke, stream, and batch methods on the prototype
    • Uses a Proxy to intercept method calls
  2. Callback Injection: When a LangChain method is called:

    • The wrapper intercepts the call
    • Augments the options.callbacks array with Sentry's callback handler
    • Calls the original method with the augmented callbacks

The integration is enabled by default when you initialize Sentry in Node.js:

import * as Sentry from '@sentry/node';
import { ChatAnthropic } from '@langchain/anthropic';

Sentry.init({
  dsn: 'your-dsn',
  tracesSampleRate: 1.0,
  sendDefaultPii: true, // Enable to record inputs/outputs
});

// LangChain calls are automatically instrumented
const model = new ChatAnthropic({
  model: 'claude-3-5-sonnet-20241022',
});

await model.invoke('What is the capital of France?');

You can configure what data is recorded:

Sentry.init({
  integrations: [
    Sentry.langChainIntegration({
      recordInputs: true,  // Record prompts/messages
      recordOutputs: true, // Record responses
    })
  ],
});

Note: We need to disable integrations for AI providers that LangChain use to avoid duplicate spans, this will be handled in a follow up PR.

Copy link
Contributor

github-actions bot commented Oct 17, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.64 kB - -
@sentry/browser - with treeshaking flags 23.14 kB - -
@sentry/browser (incl. Tracing) 40.99 kB - -
@sentry/browser (incl. Tracing, Replay) 79.29 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 68.98 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 84 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 96.16 kB - -
@sentry/browser (incl. Feedback) 41.33 kB - -
@sentry/browser (incl. sendFeedback) 29.3 kB - -
@sentry/browser (incl. FeedbackAsync) 34.26 kB - -
@sentry/react 26.35 kB - -
@sentry/react (incl. Tracing) 42.99 kB - -
@sentry/vue 29.13 kB - -
@sentry/vue (incl. Tracing) 42.79 kB - -
@sentry/svelte 24.66 kB - -
CDN Bundle 26.94 kB - -
CDN Bundle (incl. Tracing) 41.65 kB - -
CDN Bundle (incl. Tracing, Replay) 77.89 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 83.35 kB - -
CDN Bundle - uncompressed 78.95 kB - -
CDN Bundle (incl. Tracing) - uncompressed 123.53 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 238.54 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 251.31 kB - -
@sentry/nextjs (client) 45.13 kB - -
@sentry/sveltekit (client) 41.42 kB - -
@sentry/node-core 50.78 kB - -
@sentry/node 156.21 kB +1.17% +1.8 kB 🔺
@sentry/node - without tracing 92.66 kB - -
@sentry/aws-serverless 106.35 kB - -

View base workflow run

Copy link
Contributor

github-actions bot commented Oct 17, 2025

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 8,623 - 9,292 -7%
GET With Sentry 1,272 15% 1,442 -12%
GET With Sentry (error only) 5,947 69% 6,142 -3%
POST Baseline 1,161 - 1,195 -3%
POST With Sentry 528 45% 555 -5%
POST With Sentry (error only) 1,019 88% 1,065 -4%
MYSQL Baseline 3,212 - 3,309 -3%
MYSQL With Sentry 420 13% 528 -20%
MYSQL With Sentry (error only) 2,619 82% 2,773 -6%

View base workflow run

@RulaKhaled RulaKhaled marked this pull request as ready for review October 17, 2025 10:42
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

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.

1 participant