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, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To trace LangChain calls manually, attach BraintrustCallbackHandler 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)
  • 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