Our LangChainInstrumentor automatically captures traces for both LangGraph and LangChain. If you’ve already enabled that instrumentor, you do not need to complete the steps below.
1. Installation
First install the traceAI package and necessary LangChain packages.
Initialize the LangChain Instrumentor to enable automatic tracing. Our LangChainInstrumentor automatically captures traces for both LangGraph and LangChain.
from traceai_langchain import LangChainInstrumentorLangChainInstrumentor().instrument(tracer_provider=trace_provider)
5. Create LangGraph Agents
Set up your LangGraph agents as you normally would. Our Instrumentor will automatically trace and send the telemetry data to our platform.
from typing import Annotatedfrom typing_extensions import TypedDictfrom langgraph.graph import StateGraph, START, ENDfrom langgraph.graph.message import add_messagesfrom langchain_anthropic import ChatAnthropicfrom IPython.display import Image, displayclass State(TypedDict): messages: Annotated[list, add_messages]graph_builder = StateGraph(State)llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")def chatbot(state: State): return {"messages": [llm.invoke(state["messages"])]}graph_builder.add_node("chatbot", chatbot)graph_builder.add_edge(START, "chatbot")graph_builder.add_edge("chatbot", END)graph = graph_builder.compile()try: display(Image(graph.get_graph().draw_mermaid_png()))except Exception: passdef stream_graph_updates(user_input: str): for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}): for value in event.values(): print("Assistant:", value["messages"][-1].content)user_input = "What do you know about LangGraph?"stream_graph_updates(user_input)