Musterby Elitery
Integrations

LangChain

Trace LangChain and LangGraph applications in Muster with the CallbackHandler that automatically captures LLM calls, tools, and retrievers.

Muster provides observability for LangChain and LangGraph applications through a CallbackHandler that automatically captures traces of LLM calls, tools, and retrievers. The handler hooks into the standard mechanism LangChain exposes for instrumenting component execution, so you do not need to modify your chains or graphs to get full traces.

Key Integration Points

The integration captures:

  • LLM interactions and outputs (including streaming)
  • Tool usage and execution
  • Retriever operations
  • Performance metrics and latencies
  • Errors raised by chains, tools, or models

Getting Started

Python

pip install langfuse langchain langchain_openai langgraph
import os
from langfuse.langchain import CallbackHandler
from langchain_openai import ChatOpenAI

os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_BASE_URL"] = "https://app.getmuster.io"  # or your self-hosted Muster URL

handler = CallbackHandler()

llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])
print(llm.invoke("Hello"))

JS/TS

npm install @langfuse/core @langfuse/langchain
import { CallbackHandler } from "@langfuse/langchain";
import { ChatOpenAI } from "@langchain/openai";

const handler = new CallbackHandler({
  publicKey: process.env.LANGFUSE_PUBLIC_KEY,
  secretKey: process.env.LANGFUSE_SECRET_KEY,
  baseUrl: process.env.LANGFUSE_BASE_URL, // e.g. https://app.getmuster.io
});

const llm = new ChatOpenAI({
  model: "gpt-4o",
  callbacks: [handler],
});

await llm.invoke("Hello");

Both flavours read credentials from the standard LANGFUSE_* environment variables (LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_BASE_URL).

Interoperability Features

The integration supports:

  • Combining with the Muster SDK — add non-LangChain observations to a trace using @observe() decorators or context managers.
  • Dynamic trace attributes — set user_id, session_id, and tags per invocation.
  • Distributed tracing — pass custom trace IDs to correlate with traces produced by external systems.
  • Scoring — multiple options to attach evaluation scores to traces after execution.

Important Upgrade Notes

Recent versions introduced breaking changes:

  • Python v2 → v3 — shifted to a singleton pattern via get_client() and removed several constructor arguments. The CallbackHandler no longer accepts trace metadata in its constructor; use propagate_attributes(...) instead.
  • JS/TS v1 → v2 — callbacks now run in the background by default. Set LANGCHAIN_CALLBACKS_BACKGROUND=false for serverless environments where the runtime exits before the queue is flushed.

Both versions now create separate traces per invocation rather than grouping multiple calls into one trace.

See also