Python SDK

LangChain & LangGraph

Trace LangChain chains and LangGraph agents using the Infralo callback handler.

Infralo integrates with both LangChain and LangGraph via the LangChain Callbacks framework. This enables seamless tracing of chains, agents, and state graphs.


Setup

First, install the LangGraph extra dependencies:

pip install infralo[langgraph]

To configure tracing, instantiate the InfraloCallbackHandler and pass it to your chain or graph at invocation time.


Usage

LangGraph Example

Create a callback handler instance and pass it inside the invocation config dictionary:

from infralo import Infralo
from infralo.integrations.langgraph import InfraloCallbackHandler
from langgraph.prebuilt import create_react_agent

infralo = Infralo()
agent = create_react_agent(model, tools)

# Invoke within an active Infralo trace
with infralo.start_trace(session_id="langgraph-demo"):
    handler = InfraloCallbackHandler()
    
    result = await agent.ainvoke(
        {"messages": [("user", "Calculate 42 * 7")]},
        config={"callbacks": [handler]}
    )

Plain LangChain Example

The exact same handler works with any standard LangChain LCEL chain:

from infralo import Infralo
from infralo.integrations.langgraph import InfraloCallbackHandler

infralo = Infralo()
chain = prompt | model | parser

# Invoke within an active Infralo trace
with infralo.start_trace(session_id="langchain-demo"):
    handler = InfraloCallbackHandler()
    
    result = chain.invoke(
        {"topic": "observability"},
        config={"callbacks": [handler]}
    )

[!TIP] Handler Lifecycle: It is highly recommended to instantiate a new InfraloCallbackHandler per run (invoke / ainvoke) rather than sharing a single handler globally. This prevents trace leaks and ensures clean execution contexts.


What is Captured

The LangChain/LangGraph integration automatically captures:

  • Tool Spans: Executions of functions wrapped with LangChain @tool or subclassing BaseTool.
  • Inputs & Outputs: Serialized inputs and output strings/dictionaries.
  • Concurrency Safety: Auto-handles parallel executions (e.g. parallel ToolNode structures inside graphs) across different threads and coroutines safely.

Complete Examples

You can view or download the complete, ready-to-run examples:

On this page