Introducing Confident Monitor

LangChain

Use Confident AI for LLM observability and evals for LangChain

Overview

LangChain is a framework for building LLM applications. Confident AI traces and evaluates your LangChain applications automatically through confident-trace, Confident AI's OpenTelemetry-native tracing SDK for Python and TypeScript — call init() once and your chain code stays exactly as it is.

The integration captures the following spans from your LangChain application:

  • Chain and runnable spans — one span per chain and each intermediate runnable inside it, with their inputs and outputs
  • LLM spans — model name, token usage, finish reasons, and input/output messages (including tool calls made by the model)
  • Tool spans — tool name, input parameters, and output, nested under the chain that ran them
  • Retriever spans — query input and retrieved document text
RuntimeRequirementsSetup
PythonPython 3.10+, LangChain 1.xCall init() before running the chain
TypeScriptNode.js 22+, @langchain/core >=1.2.9 <2Call init() and launch your entry point with the preload

Auto-Instrument

  1. Install Dependencies

    Run the following command to install confident-trace alongside LangChain:

    pip install confident-trace 'langchain>=1,<2' 'langchain-openai>=1,<2'
  2. Set Your API Keys

    Get your Confident AI Project API key and set it as an environment variable, along with your model provider's key:

    export CONFIDENT_API_KEY="<your-confident-project-key>"
    export OPENAI_API_KEY="<your-openai-key>"
  3. Instrument LangChain

    Call init() once before running your chain. It detects LangChain automatically and attaches its callback handler for you — there's no handler to pass to invoke, and no tracing extra to install.

    main.py
    from confident_trace import init, shutdown
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.output_parsers import StrOutputParser
    from langchain_openai import ChatOpenAI
    
    init()
    prompt = ChatPromptTemplate.from_template("Explain {topic} in one sentence.")
    chain = prompt | ChatOpenAI(model="gpt-4.1-mini") | StrOutputParser()
    
    try:
        print(chain.invoke({"topic": "OpenTelemetry"}))
    finally:
        shutdown()
  4. Run LangChain

    Run your script to send the trace to Confident AI:

    python main.py

    Done ✅. Open the Observatory in your Confident AI project to inspect the trace and its chain, model, and parser spans.

What Gets Captured

The integration mirrors the hierarchy LangChain reports through its callbacks:

  • Chains and runnables — one span per chain and each intermediate runnable inside it. These are plain spans; a chain's name alone doesn't mark it as an agent (wrap the invocation in an agent-typed span if you want an agent-typed root — see custom application spans).
  • Model calls — LLM spans with model information, token usage, finish reasons, and normalized input/output messages.
  • Tool executions — tool spans that follow their framework parent. The model's tool request appears on the model span's output.
  • Retrievers — retriever spans with retrieved document text.

Chain state, tool values, and document text follow the content policy. Size limits are disabled by default, but you can configure a limit or redact content before export.

Streaming

Streaming is traced the same way as invoke; the model span finishes when the stream does. These snippets replace the chain.invoke call inside the quickstart's try block.

stream = chain.stream({"topic": "OpenTelemetry"})
try:
    for chunk in stream:
        print(chunk, end="", flush=True)
finally:
    stream.close()

Also supported: ainvoke, astream, batch operations, and astream_events v2.

Set Trace Span Properties

Use a trace context to add properties you know before the call starts. It creates no extra span; the trace started by chain.invoke() inherits the tags, metadata, user ID, and customer ID.

main.py
from confident_trace import init, trace_context
from langchain_openai import ChatOpenAI

init()

model = ChatOpenAI(model="gpt-4.1-mini")
chain = model

with trace_context(
    tags=["support"],
    metadata={"release": "2026-09"},
    user_id="user-42",
    customer_id="customer-7",
):
    result = chain.invoke("Explain OpenTelemetry in one sentence.")

See users and customers to set an optional display name alongside each ID, and trace context for every supported trace property and update behavior.

Instrumenting Multi-Turn

You do not need turn() when one LangChain entry-point call is already one conversational turn—the integration creates that turn's trace automatically. Use turn() when you want to define the boundary yourself, such as grouping two sequential LangChain calls into one turn. Reuse the same thread ID on later turns to group them into one conversation.

main.py
from confident_trace import init, turn

init()

with turn("support-turn", thread_id="chat-42"):
    context = chain.invoke("Find the relevant account details.")
    answer = chain.invoke(f"Answer the user using this context: {context.content}")

See threads for thread I/O, turn IDs, and user IDs.

Troubleshooting

  • No trace: make sure init() runs before the chain executes, and that the process reaches shutdown() so buffered spans are flushed.
  • Incomplete streams: consume or close streams before shutdown().

For general setup issues, see troubleshooting.

Disable LangChain Instrumentation

Pass init() a list of integration identifiers to opt in to only those integrations. The identifier for LangChain is "langchain" in Python and TypeScript; omit it to disable this integration. An empty list disables all automatic instrumentation:

main.py
from confident_trace import init
init(instrumentations=())
# Use ("langchain",) to opt in; omit "langchain" to disable it.

This turns off Confident AI's automatic instrumentation; calls made after initialization are not instrumented by this integration.

Next Steps

Need help instrumenting your application?Connect your model calls and agent workflows to Confident AITalk to an expert

Last updated on

Built byConfident AI