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.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 17, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.63 kB - -
@sentry/browser - with treeshaking flags 23.11 kB - -
@sentry/browser (incl. Tracing) 40.97 kB - -
@sentry/browser (incl. Tracing, Profiling) 45.26 kB - -
@sentry/browser (incl. Tracing, Replay) 79.29 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 68.97 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 83.99 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 96.16 kB - -
@sentry/browser (incl. Feedback) 41.3 kB - -
@sentry/browser (incl. sendFeedback) 29.29 kB - -
@sentry/browser (incl. FeedbackAsync) 34.22 kB - -
@sentry/react 26.31 kB - -
@sentry/react (incl. Tracing) 42.97 kB - -
@sentry/vue 29.11 kB - -
@sentry/vue (incl. Tracing) 42.75 kB - -
@sentry/svelte 24.64 kB - -
CDN Bundle 26.9 kB - -
CDN Bundle (incl. Tracing) 41.62 kB - -
CDN Bundle (incl. Tracing, Replay) 77.87 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 83.35 kB - -
CDN Bundle - uncompressed 78.86 kB - -
CDN Bundle (incl. Tracing) - uncompressed 123.44 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 238.48 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 251.24 kB - -
@sentry/nextjs (client) 45.11 kB - -
@sentry/sveltekit (client) 41.4 kB - -
@sentry/node-core 50.75 kB - -
@sentry/node 156.91 kB +1.28% +1.98 kB 🔺
@sentry/node - without tracing 92.61 kB +0.01% +1 B 🔺
@sentry/aws-serverless 106.33 kB +0.01% +1 B 🔺

View base workflow run

@github-actions
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 9,190 - 11,704 -21%
GET With Sentry 1,354 15% 1,613 -16%
GET With Sentry (error only) 6,016 65% 7,913 -24%
POST Baseline 1,187 - 1,216 -2%
POST With Sentry 508 43% 564 -10%
POST With Sentry (error only) 1,050 88% 1,068 -2%
MYSQL Baseline 3,248 - 4,148 -22%
MYSQL With Sentry 352 11% 593 -41%
MYSQL With Sentry (error only) 2,662 82% 3,405 -22%

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.

Copy link
Member

@andreiborza andreiborza left a comment

Choose a reason for hiding this comment

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

LGTM, nice work!

@RulaKhaled RulaKhaled merged commit 7333e18 into develop Oct 22, 2025
371 of 379 checks passed
@RulaKhaled RulaKhaled deleted the instrument-langchain branch October 22, 2025 13:11
@RulaKhaled RulaKhaled restored the instrument-langchain branch October 22, 2025 13:13
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.

3 participants