Register a callback configure hook to automatically add callback handlers to all runs.

There are two ways to use this:

  1. Using a context variable:

    • Set contextVar to specify the variable name
    • Use setContextVariable() to store your handler instance
  2. Using an environment variable:

    • Set both envVar and handlerClass
    • The handler will be instantiated when the env var is set to "true".
// Method 1: Using context variable
import {
registerConfigureHook,
setContextVariable
} from "@langchain/core/context";

const tracer = new MyCallbackHandler();
registerConfigureHook({
contextVar: "my_tracer",
});
setContextVariable("my_tracer", tracer);

// ...run code here

// Method 2: Using environment variable
registerConfigureHook({
handlerClass: MyCallbackHandler,
envVar: "MY_TRACER_ENABLED",
});
process.env.MY_TRACER_ENABLED = "true";

// ...run code here