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:

Capture controls

LangChain spans record chain inputs and outputs, model inputs and outputs, and token usage by default. Capture controls omit any of these from LangChain and LangGraph spans. Spans, timing (including time_to_first_token), errors, and metadata such as the model name are still recorded. Requires braintrust v3.44.0 or later.Each option defaults to true. Set an option to false to omit that data:With auto-instrumentation, pass the options as langchain to configureInstrumentation(). Import configureInstrumentation() from braintrust/instrumentation and call it in a separate file:
capture-config.mjs
Load the file before the hook:
With manual instrumentation, pass the same options to the handler:
Capture controls apply only to LangChain and LangGraph spans. If you also trace the model provider, such as with OpenAI or Anthropic auto-instrumentation, the provider’s spans nested under the LangChain spans still record model inputs, outputs, and token usage.

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
  • Service tiers on LLM spans, as requested_service_tier metadata when the invocation parameters set a tier and served_service_tier metadata when the model response reports one
  • Errors from any callback captured on the span

Tracing resources