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_nameandgen_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(notERROR) and is marked withlanggraph.interrupt
LangGraphInstrumentoris a deprecated no-op kept for backwards compatibility. You can remove anyLangGraphInstrumentor().instrument()call — tracing happens throughLangChainInstrumentor.
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 metadatasession.id— when set viausing_session(...)orconfig={"metadata": {"session_id": ...}}langgraph.interrupt(attribute + event) on a HITL pause; alanggraph.resumeevent on resume
Examples
LangChain Examples
examples/chat_prompt_template.py- Basic chat prompt usageexamples/rag.py- Retrieval-augmented generationexamples/tool_calling_agent.py- Agent with toolsexamples/openai_chat_stream.py- Streaming responses
LangGraph Examples
examples/langgraph_simple_workflow.py- Simple state machine workflowexamples/langgraph_agent_supervisor.py- Multi-agent supervisor patternexamples/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
- Make sure you instrumented
LangChainInstrumentor— LangGraph is traced through it (LangGraphInstrumentoris a no-op). - Install the langgraph extra if you use LangGraph:
pip install traceAI-langchain[langgraph]. - Check that
langgraphis 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15045681dc9a14f446a5bcf908904e7834b48f6f9765181f5055877b8552f9d8
|
|
| MD5 |
89cfef5c0135d196d508071e23e15860
|
|
| BLAKE2b-256 |
f89b4870d7cf29507cdb138d1bbcd363340ad2e82f1e822b738cf6118890e46b
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83066fdd52a1ec8ad397bff0c79c3efa964d3687d39abdaa73ebf3ebf2a7ad17
|
|
| MD5 |
45e8ac7b606cd785aca3d038d35c06ef
|
|
| BLAKE2b-256 |
7ce142b870ebb962ab099edc68830de97e32bb95953bacc4af017b5c662a924e
|