Skip to content

feat(core): Instrument LangChain AI#17955

Merged
RulaKhaled merged 13 commits intodevelopfrom
instrument-langchain
Oct 22, 2025
Merged

feat(core): Instrument LangChain AI#17955
RulaKhaled merged 13 commits intodevelopfrom
instrument-langchain

Conversation

@RulaKhaled
Copy link
Copy Markdown
Contributor

@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.

Loading
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.

2 participants