Notebooks
L
Langfuse
Js Integration Langchain

Js Integration Langchain

observabilityllmsgenaicookbookprompt-managementhacktoberfestlarge-language-modelsnextraLangfuselangfuse-docs

Cookbook: Langchain Integration (JS/TS)

Python JS/TS

This is a cookbook with examples of the Langfuse integration for Langchain (JS/TS).

Follow the integration guide to add this integration to your Langchain project.

Set Up Environment

Get your Langfuse API keys by signing up for Langfuse Cloud or self-hosting Langfuse. You’ll also need your OpenAI API key.

Note: This cookbook uses Deno.js for execution, which requires different syntax for importing packages and setting environment variables. For Node.js applications, the setup process is similar but uses standard npm packages and process.env.

[ ]

With the environment variables set, we can now initialize the langfuseSpanProcessor which is passed to the main OpenTelemetry SDK that orchestrates tracing.

[ ]

Step 2: Instantiate the CallbackHandler

Instantiate the CallbackHandler and pass it to your chain’s .invoke() or .stream() method in the callbacks array. All operations within the chain will be traced as nested observations.

[ ]

Step 3: Langchain Example

We create a simple agent that can answer questions and call tools. The agent will use OpenAI GPT-5-mini as its language model, a basic weather function as a tool, and a simple prompt to guide its behavior. All tools and model calls are traced in Langfuse.

[ ]
[ ]

Step 4: Explore the trace in Langfuse

In the Langfuse interface, you can see a detailed trace of all steps in the Langchain application.

Langfuse Trace

Public trace in the Langfuse UI