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.

pip install traceAI-langchain
pip install langgraph
pip install langchain-anthropic
pip install ipython

2. Set Environment Variables

Set up your environment variables to authenticate with both FutureAGI and Anthropic.

import os

os.environ["ANTHROPIC_API_KEY"] = "your-anthropic-api-key"
os.environ["FI_API_KEY"] = "your-futureagi-api-key"
os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"

3. Initialize Trace Provider

Set up the trace provider to create a new project in FutureAGI, establish telemetry data pipelines .

from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

trace_provider = register(
    project_type=ProjectType.OBSERVE,
    project_name="langgraph_project",
)

4. Instrument your Project

Initialize the LangChain Instrumentor to enable automatic tracing. Our LangChainInstrumentor automatically captures traces for both LangGraph and LangChain.

from traceai_langchain import LangChainInstrumentor

LangChainInstrumentor().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 Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_anthropic import ChatAnthropic
from IPython.display import Image, display


class 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:
    pass

def 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)

Was this page helpful?