from langfuse import Langfuse, get_clientfrom langfuse.langchain import CallbackHandlerfrom langchain_openai import ChatOpenAI # Example LLMfrom langchain_core.prompts import ChatPromptTemplate# Initialize Langfuse client with constructor argumentsLangfuse( public_key="your-public-key", secret_key="your-secret-key", host="https://cloud.langfuse.com" # Optional: defaults to https://cloud.langfuse.com)# Get the configured client instancelangfuse = get_client()# Initialize the Langfuse handlerlangfuse_handler = CallbackHandler()# Create your LangChain componentsllm = ChatOpenAI(model_name="gpt-5.5")prompt = ChatPromptTemplate.from_template("Tell me a joke about {topic}")chain = prompt | llm# Run your chain with Langfuse tracingresponse = chain.invoke({"topic": "cats"}, config={"callbacks": [langfuse_handler]})print(response.content)# Flush events to Langfuse in short-lived applicationslangfuse.flush()
LANGFUSE_SECRET_KEY="sk-lf-..."LANGFUSE_PUBLIC_KEY="pk-lf-..."# 🇪🇺 EU regionLANGFUSE_HOST="https://cloud.langfuse.com"# 🇺🇸 US region# LANGFUSE_HOST="https://us.cloud.langfuse.com"
# Initialize Langfuse handlerfrom langfuse.langchain import CallbackHandlerlangfuse_handler = CallbackHandler()# Your LangChain code# Add Langfuse handler as callback (classic and LCEL)chain.invoke({"input": "<user_input>"}, config={"callbacks": [langfuse_handler]})
import os# get keys for your project from https://cloud.langfuse.comos.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-***"os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-***"os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # for EU data region# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # for US data region# your openai keyos.environ["OPENAI_API_KEY"] = "***"
from typing import Annotatedfrom langchain_openai import ChatOpenAIfrom langchain.messages import HumanMessagefrom typing_extensions import TypedDictfrom langgraph.graph import StateGraphfrom langgraph.graph.message import add_messagesclass State(TypedDict): # Messages have the type "list". The `add_messages` function in the annotation defines how this state key should be updated # (in this case, it appends messages to the list, rather than overwriting them) messages: Annotated[list, add_messages]graph_builder = StateGraph(State)llm = ChatOpenAI(model = "gpt-5.5", temperature = 0.2)# The chatbot node function takes the current State as input and returns an updated messages list. This is the basic pattern for all LangGraph node functions.def chatbot(state: State): return {"messages": [llm.invoke(state["messages"])]}# Add a "chatbot" node. Nodes represent units of work. They are typically regular python functions.graph_builder.add_node("chatbot", chatbot)# Add an entry point. This tells our graph where to start its work each time we run it.graph_builder.set_entry_point("chatbot")# Set a finish point. This instructs the graph "any time this node is run, you can exit."graph_builder.set_finish_point("chatbot")# To be able to run our graph, call "compile()" on the graph builder. This creates a "CompiledGraph" we can use invoke on our state.graph = graph_builder.compile()
from langfuse.langchain import CallbackHandler# Initialize Langfuse CallbackHandler for LangChain (tracing)langfuse_handler = CallbackHandler()stream = graph.stream_events( {"messages": [HumanMessage(content="What is Langfuse?")]}, config={"callbacks": [langfuse_handler]}, version="v3",)for snapshot in stream.values: print(snapshot)
{'chatbot': {'messages': [AIMessage(content='Langfuse is a tool designed to help developers monitor and observe the performance of their Large Language Model (LLM) applications. It provides detailed insights into how these applications are functioning, allowing for better debugging, optimization, and overall management. Langfuse offers features such as tracking key metrics, visualizing data, and identifying potential issues in real-time, making it easier for developers to maintain and improve their LLM-based solutions.', response_metadata={'token_usage': {'completion_tokens': 86, 'prompt_tokens': 13, 'total_tokens': 99}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_400f27fa1f', 'finish_reason': 'stop', 'logprobs': None}, id='run-9a0c97cb-ccfe-463e-902c-5a5900b796b4-0', usage_metadata={'input_tokens': 13, 'output_tokens': 86, 'total_tokens': 99})]}}