Skip to content

LangChain and LangGraph Monitoring

Monitor LangChain agents and LangGraph graphs using the elsai ARMS callback handler. Add arms.langchain_callback to your graph or agent invocation to automatically track execution, tools, LLM calls, and costs.

Prerequisites

  • elsai ARMS installed and configured
  • LangChain and LangGraph installed
  • Database connection configured (MongoDB, DynamoDB, or ClickHouse)

Basic usage

Initialize elsai ARMS

python
from elsai_arms.elsai_arms import ElsaiARMS

arms = ElsaiARMS("Agent_Project")

Create your LangGraph

python
from langchain_core.messages import HumanMessage
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator

class State(TypedDict):
    messages: Annotated[list, operator.add]

def agent_node(state: State):
    return {"messages": [HumanMessage(content="Agent response")]}

graph = StateGraph(State)
graph.add_node("agent", agent_node)
graph.set_entry_point("agent")
graph.add_edge("agent", END)
graph = graph.compile()

Invoke with the ARMS callback

python
messages = [HumanMessage(content="What is the weather today?")]

result = graph.invoke(
    {"messages": messages},
    config={"callbacks": [arms.langchain_callback]}
)

Complete example

python
from elsai_arms.elsai_arms import ElsaiARMS
from langchain_core.messages import HumanMessage, AIMessage
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator

arms = ElsaiARMS("Weather_Agent_Project")

class State(TypedDict):
    messages: Annotated[list, operator.add]

def weather_agent(state: State):
    user_message = state["messages"][-1].content
    response = f"Processing weather query: {user_message}"
    return {"messages": [AIMessage(content=response)]}

graph = StateGraph(State)
graph.add_node("agent", weather_agent)
graph.set_entry_point("agent")
graph.add_edge("agent", END)
graph = graph.compile()

try:
    arms.info("Starting weather agent")

    messages = [HumanMessage(content="What's the weather in New York?")]
    result = graph.invoke(
        {"messages": messages},
        config={"callbacks": [arms.langchain_callback]}
    )

    arms.info("Agent execution completed")
    arms.end_run()
    arms.flush()

    print(arms.export())

except Exception as e:
    arms.error(f"Agent error: {e}")

What gets monitored

When you use the LangChain callback, elsai ARMS automatically tracks:

  • Agent execution — start time, end time, duration
  • LLM calls — all LLM interactions within the agent
  • Token usage — input and output tokens for each LLM call
  • Tool calls — tools invoked by the agent
  • Cost — estimated cost for all LLM operations
  • Performance — latency and throughput metrics
  • Errors — any errors during agent execution

Traces are tagged with framework: "langchain" or framework: "langgraph" depending on the execution context.

Advanced usage

Combining with other callbacks

python
from langchain.callbacks import StdOutCallbackHandler

result = graph.invoke(
    {"messages": messages},
    config={
        "callbacks": [
            arms.langchain_callback,
            StdOutCallbackHandler()
        ]
    }
)

Multiple invocations

Each invoke call creates a separate trace when using the callback:

python
step1_result = graph.invoke(
    {"messages": messages1},
    config={"callbacks": [arms.langchain_callback]}
)

step2_result = graph.invoke(
    {"messages": messages2},
    config={"callbacks": [arms.langchain_callback]}
)

Model override

Pass an explicit model name in config metadata:

python
config = {
    "callbacks": [arms.langchain_callback],
    "metadata": {"arms_override_model": "gpt-4o"}
}
result = graph.invoke({"messages": messages}, config=config)

Viewing results

After running your agent:

  1. Export data — use arms.export() to get JSON
  2. Check your database — view stored traces in MongoDB, DynamoDB, or ClickHouse
  3. Review metrics — analyze agent performance, token usage, and costs in the ARMS dashboard

Next steps

Copyright © 2026 elsai foundry.