Skip to main content

LangChain & LangGraph OpenTelemetry Integration

Overview

This integration provides comprehensive OpenTelemetry instrumentation for both LangChain and LangGraph frameworks. It enables detailed tracing and monitoring of applications built with these frameworks.

Installation

Install traceAI LangChain

pip install traceAI-langchain

For LangGraph support (optional)

pip install traceAI-langchain[langgraph]

Install LangChain OpenAI

pip install langchain-openai

Environment Variables

Set up your environment variables to authenticate with FutureAGI.

import os

os.environ["FI_API_KEY"] = FI_API_KEY
os.environ["FI_SECRET_KEY"] = FI_SECRET_KEY
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY

LangChain Quickstart

Register Tracer Provider

Set up the trace provider to establish the observability pipeline:

from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

trace_provider = register(
    project_type=ProjectType.OBSERVE,
    project_name="langchain_app",
    session_name="chat-bot"
)

Configure LangChain Instrumentation

Instrument the LangChain client to enable telemetry collection:

from traceai_langchain import LangChainInstrumentor

LangChainInstrumentor().instrument(tracer_provider=trace_provider)

Create LangChain Components

Set up your LangChain client with built-in observability:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("{x} {y} {z}?").partial(x="why is", z="blue")
chain = prompt | ChatOpenAI(model_name="gpt-3.5-turbo")

def run_chain():
    try:
        result = chain.invoke({"y": "sky"})
        print(f"Response: {result}")
    except Exception as e:
        print(f"Error executing chain: {e}")

if __name__ == "__main__":
    run_chain()

LangGraph Instrumentation

LangGraph tracing is automatic: LangGraph runs each node, tool and LLM through LangChain's callback system, so LangChainInstrumentor captures them with no LangGraph-specific setup. You get:

  • Per-node spans named after the graph node (agent, tools, ask_human, …)
  • Tool and LLM spans nested under their node
  • Graph-node enrichment — the node's own span carries gen_ai.agent.graph.node_name and gen_ai.agent.graph.node_id
  • Session grouping — bind session.id = thread_id (see the quickstart)
  • HITL interrupts traced correctly — an interrupted node/tool span stays OK (not ERROR) and is marked with langgraph.interrupt

LangGraphInstrumentor is a deprecated no-op kept for backwards compatibility. You can remove any LangGraphInstrumentor().instrument() call — tracing happens through LangChainInstrumentor.

LangGraph Quickstart

from typing import Annotated, TypedDict
from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType
from fi_instrumentation.instrumentation.context_attributes import using_session
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from traceai_langchain import LangChainInstrumentor

class MyState(TypedDict):
    messages: Annotated[list, add_messages]

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

# Instrument LangChain — LangGraph is traced through it automatically.
LangChainInstrumentor().instrument(tracer_provider=trace_provider)

workflow = StateGraph(MyState)
workflow.add_node("agent", agent_node)   # sync or async nodes both work
workflow.add_edge(START, "agent")
workflow.add_edge("agent", END)
app = workflow.compile()

# Group every span of a conversation under one session:
with using_session("thread-123"):
    result = app.invoke(
        {"messages": []}, {"configurable": {"thread_id": "thread-123"}}
    )

LangGraph Span Attributes

Node, tool and LLM runs are standard LangChain spans (gen_ai.span.kind = CHAIN / TOOL / LLM / AGENT) with these LangGraph additions:

  • gen_ai.agent.graph.node_name, gen_ai.agent.graph.node_id — the graph node (on the node's own span)
  • langgraph_node, langgraph_step, langgraph_triggers, langgraph_path, langgraph_checkpoint_ns — LangGraph's raw callback metadata
  • session.id — when set via using_session(...) or config={"metadata": {"session_id": ...}}
  • langgraph.interrupt (attribute + event) on a HITL pause; a langgraph.resume event on resume

Examples

LangChain Examples

  • examples/chat_prompt_template.py - Basic chat prompt usage
  • examples/rag.py - Retrieval-augmented generation
  • examples/tool_calling_agent.py - Agent with tools
  • examples/openai_chat_stream.py - Streaming responses

LangGraph Examples

  • examples/langgraph_simple_workflow.py - Simple state machine workflow
  • examples/langgraph_agent_supervisor.py - Multi-agent supervisor pattern
  • examples/langgraph_human_in_the_loop.py - Human-in-the-loop interrupt workflow

API Reference

LangChainInstrumentor

from traceai_langchain import LangChainInstrumentor

# Initialize and instrument
instrumentor = LangChainInstrumentor()
instrumentor.instrument(tracer_provider=trace_provider)

# Get current span
span = instrumentor.get_span(run_id)

# Get ancestor spans
ancestors = instrumentor.get_ancestors(run_id)

LangGraphInstrumentor

from traceai_langchain import LangGraphInstrumentor

# Deprecated no-op — LangGraph is traced automatically by LangChainInstrumentor.
# instrument() only logs a one-time deprecation notice; you can remove this call.
LangGraphInstrumentor().instrument(tracer_provider=trace_provider)

Troubleshooting

LangGraph not being traced

  1. Make sure you instrumented LangChainInstrumentor — LangGraph is traced through it (LangGraphInstrumentor is a no-op).
  2. Install the langgraph extra if you use LangGraph: pip install traceAI-langchain[langgraph].
  3. Check that langgraph is installed: pip show langgraph.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

traceai_langchain-0.2.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

traceai_langchain-0.2.0-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file traceai_langchain-0.2.0.tar.gz.

File metadata

  • Download URL: traceai_langchain-0.2.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.0 Darwin/25.1.0

File hashes

Hashes for traceai_langchain-0.2.0.tar.gz
Algorithm Hash digest
SHA256 15045681dc9a14f446a5bcf908904e7834b48f6f9765181f5055877b8552f9d8
MD5 89cfef5c0135d196d508071e23e15860
BLAKE2b-256 f89b4870d7cf29507cdb138d1bbcd363340ad2e82f1e822b738cf6118890e46b

See more details on using hashes here.

File details

Details for the file traceai_langchain-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: traceai_langchain-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.0 Darwin/25.1.0

File hashes

Hashes for traceai_langchain-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83066fdd52a1ec8ad397bff0c79c3efa964d3687d39abdaa73ebf3ebf2a7ad17
MD5 45e8ac7b606cd785aca3d038d35c06ef
BLAKE2b-256 7ce142b870ebb962ab099edc68830de97e32bb95953bacc4af017b5c662a924e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page