Trace LangChain and LangGraph agents with OpenTelemetry

OpenInference's LangChain instrumentation turns LangChain and LangGraph runs into OpenTelemetry spans. Export them to Maple, pass the conversation's thread_id on every call, and each chat becomes one agent session. Python is shown here; the full guide covers TypeScript too.

invoke_agent assistant 5 spans · 2.84s
DurationStatus
invoke_agent assistantsupport-agent
2.84sOk
chat gpt-4o-mini
820msOk
invoke_workflow tools
310msOk
execute_tool get_weather
295msOk
chat gpt-4o-mini
1.65sOk

Example: a LangGraph turn with a tool node

Quick start

Set up LangChain

1

Install the instrumentor

bash
pip install "langchain>=1.4" "langgraph>=1.2" "langchain-openai>=1.6" \
  "openinference-instrumentation-langchain>=0.1.76" "openinference-instrumentation>=0.1.66" \
  "opentelemetry-sdk>=1.45" "opentelemetry-exporter-otlp-proto-http>=1.45"
2

Point the exporter at Maple

bash
export OTEL_SERVICE_NAME=support-agent
export OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=production
export OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.maple.dev
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer YOUR_INGEST_KEY"
3

Initialize tracing before the first invoke()

python
# tracing.py
from openinference.instrumentation import TraceConfig
from openinference.instrumentation.langchain import LangChainInstrumentor
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import SpanProcessor, TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# Marks agent and tool-node spans, which Maple draws as agent lanes and steps.
# The name= you gave create_agent(), and graph nodes that act as agents
AGENT_NAMES = {"assistant"}
# Your tool node
STEP_NAMES = {"tools"}


class AgentSpans(SpanProcessor):
    def on_start(self, span, parent_context=None):
        if span.instrumentation_scope.name != "openinference.instrumentation.langchain":
            return
        if span.name in AGENT_NAMES:
            span.set_attribute("gen_ai.operation.name", "invoke_agent")
            span.set_attribute("gen_ai.agent.name", span.name)
        elif span.name in STEP_NAMES:
            span.set_attribute("gen_ai.operation.name", "invoke_workflow")


provider = TracerProvider()
provider.add_span_processor(AgentSpans())
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)

LangChainInstrumentor().instrument(
    tracer_provider=provider,
    config=TraceConfig(enable_genai_semconv=True),
)
4

Pass thread_id on every call

python
import tracing  # first, before the first invoke()

result = agent.invoke(
    {"messages": [{"role": "user", "content": text}]},
    {"configurable": {"thread_id": conversation_id}},
)

This is the short path. The full LangChain guide covers the rest.

Organization in the EU region? Send to https://ingest.eu.maple.dev instead of https://ingest.maple.dev. An ingest key only works in its own region.

What you get

What Maple shows for a LangGraph agent

One session per thread

Every invoke() with the same thread_id is a turn in one agent session. LangGraph Server threads group without extra code.

Agent lanes

Each agent named in AGENT_NAMES, sub-agents included, gets its own lane in the session.

Model calls with tokens

Each chat model call is a span with the model, the messages and the input and output token counts.

Tool calls

Tool spans are named after your tools and carry the arguments and the result.

Graph steps

Nodes such as your tool node show as workflow steps, so a turn reads in the order the graph ran.

Readable transcript

With enable_genai_semconv, each turn shows the messages that went in and the reply that came out, so a session reads like the chat did.

What is different in LangChain

Plain chains ignore configurable

A chain with no graph, such as prompt | model, drops the configurable thread_id. Pass it as metadata instead, or every message becomes its own session.

Streamed tokens need stream_usage

With a custom base_url, vLLM or a gateway, set stream_usage=True on ChatOpenAI. Without it, streamed replies carry no token counts.

One instrumentor only

If every model call appears twice, remove LANGSMITH_OTEL_ENABLED and any provider instrumentor such as openinference-instrumentation-openai.

Integrations

Other integrations

See your first trace today.

Add the SDK, point your OTLP exporter at Maple, and traces arrive.

maple.dev: observability on OpenTelemetry