Skip to main content
LangChain is a framework for developing applications powered by language models. Braintrust integrates with LangChain using callback handlers to automatically trace chains, agents, and LLM calls.

Setup

Install the Braintrust LangChain integration alongside your LangChain packages:
# pnpm
pnpm add braintrust @braintrust/langchain-js @langchain/core @langchain/openai
# npm
npm install braintrust @braintrust/langchain-js @langchain/core @langchain/openai

Trace with LangChain

Braintrust provides callback handlers that automatically capture LangChain operations including chains, agents, retrievers, and individual LLM calls. To trace your LangChain application, configure the BraintrustCallbackHandler:
trace-langchain.ts
import { BraintrustCallbackHandler } from "@braintrust/langchain-js";
import { ChatOpenAI } from "@langchain/openai";
import { initLogger } from "braintrust";

initLogger({
  projectName: "My Project",
  apiKey: process.env.BRAINTRUST_API_KEY,
});

const handler = new BraintrustCallbackHandler();

async function main() {
  const model = new ChatOpenAI({ modelName: "gpt-4o-mini" });

  await model.invoke("What is the capital of France?", {
    callbacks: [handler],
  });
}

main();

Resources