LangChain and LangGraph

Send OpenTelemetry spans from a LangGraph agent with OpenLLMetry's LangChain instrumentation, then add an owner and approvals with telumin-otel.

Python

Package: opentelemetry-instrumentation-langchain (tested with 0.63.1)

bash
pip install opentelemetry-sdk==1.45.2 opentelemetry-instrumentation-langchain==0.63.1 langgraph==1.2.23
Setup (from examples/instrumentation/python-langgraph)
import os
from typing import Annotated

from langchain_core.tools import InjectedToolCallId, tool
from langgraph.graph import START, MessagesState, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition
from opentelemetry import trace
from opentelemetry.instrumentation.langchain import LangchainInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from telumin_otel import owner_resource, record_approval

# 1. Tracer provider: the owner goes on the resource (C2), spans go to a file.
provider = TracerProvider(
    resource=Resource.create({"service.name": "refund-service", **owner_resource("payments-team")})
)
# This example writes a file; in production use BatchSpanProcessor(OTLPSpanExporter()).
provider.add_span_processor(SimpleSpanProcessor(OtlpJsonFileExporter(sys.argv[1])))

# 2. Record no prompt, response or tool argument content (read when spans are created),
#    then instrument LangChain/LangGraph. The compiled graph's name becomes the agent name (C1).
os.environ["TRACELOOP_TRACE_CONTENT"] = "false"
LangchainInstrumentor().instrument(tracer_provider=provider)
Record the approval in the tool (from examples/instrumentation/python-langgraph)
def ask_human(amount: int) -> tuple[str, str]:
    """Stand-in for your approval step (a review queue, a chat button, ...)."""
    return ("approved", "refund under the team limit") if amount <= 100 else ("rejected", "over the limit")


@tool
def issue_refund(order_id: str, amount: int, tool_call_id: Annotated[str, InjectedToolCallId]) -> str:
    """Refund an order. A person must approve every refund."""
    decision, reason = ask_human(amount)
    # The tool runs inside its execute_tool span, so the decision lands on that span (C4).
    record_approval(trace.get_current_span(), decision, "alice@example.com", reason=reason, tool_call_id=tool_call_id)
    return f"refund {decision} for {order_id}"
Run (from examples/instrumentation/python-langgraph)
graph = StateGraph(MessagesState)
graph.add_node("model", call_model)
graph.add_node("tools", ToolNode([issue_refund]))
graph.add_edge(START, "model")
graph.add_conditional_edges("model", tools_condition)
graph.add_edge("tools", "model")
app = graph.compile(name="refund-agent")  # the graph name becomes the agent name
  • The compiled graph's name becomes gen_ai.agent.name on the run's invoke_agent span.
  • The agent id this package sets changes on every run and there is no agent version, so C1 shows amber.
  • Set TRACELOOP_TRACE_CONTENT=false so prompts, responses and tool arguments are not recorded.

Runnable example, tested in CI

examples/instrumentation/python-langgraph runs offline with a mocked model, writes OTLP JSON, and goes through the check on every change. Result: C1 amber, C2 green, C3 amber, C4 green (one approval recorded with record_approval).

TypeScript

Package: @traceloop/instrumentation-langchain (version at time of writing: 0.27.0, not yet tested by us)

Not yet tested by us: the install line and setup below have not been run by us.

Not yet tested by us
npm install --save-exact @traceloop/instrumentation-langchain@0.27.0
Setup (not yet tested by us)
import * as CallbackManagerModule from "@langchain/core/callbacks/manager"
import { LangChainInstrumentation } from "@traceloop/instrumentation-langchain"

// After registering your tracer provider (resource with "telumin.agent.owner"):
const instrumentation = new LangChainInstrumentation({ traceContent: false })
instrumentation.manuallyInstrument({ callbackManagerModule: CallbackManagerModule })

Example pending

No example yet. Each example must run in CI with a mocked model, without an API key and without network access, and that has not been verified for this package.

Owner and approvals

No instrumentation package records who owns an agent or who approved a tool call. Add both with telumin-otel, as the examples do.

Check the result

To check the result, export the spans as OTLP JSON (or send them through the collector tap) and drop the file on the free check. The check runs in your browser tab.

Guides