Skip to main content
LangChain is a framework for developing applications powered by language models. Braintrust traces LangChain applications from your code.

Setup

Install Braintrust alongside the LangChain JS packages.

Auto-instrumentation

To trace LangChain calls without modifying your application code, initialize Braintrust normally, then run your app with Braintrust’s import hook to patch @langchain/core at runtime. Requires @langchain/core v0.3.42 or later. The same hook traces LangGraph graphs, which run through the LangChain callback path.
1

Initialize Braintrust and call LangChain

2

Run with the import hook

The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects. Compile your TypeScript to JavaScript and run the compiled file with the import hook, or run TypeScript directly with a TypeScript loader.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To trace LangChain calls manually, attach BraintrustLangChainCallbackHandler to each call’s callbacks option yourself.
Run it:

What Braintrust traces

Braintrust captures:
  • LLM and chat model calls, chains, agents, tools, and retrievers as separate spans, with inputs and outputs preserved
  • Token usage on LLM spans (prompt_tokens, completion_tokens, total_tokens, prompt_cached_tokens, prompt_cache_creation_tokens, completion_reasoning_tokens)
  • time_to_first_token on streaming responses, captured on the first new-token callback
  • Model name and serialized invocation parameters in span metadata
  • Errors from any callback captured on the span

Tracing resources