Observing agents built with LangGraph

LangGraph is a framework for building stateful, multi-agent workflows with LangChain. It enables the creation of complex agent systems with cyclic execution flows, state management, and conditional routing between agents.

Instana provides observability for LangGraph applications, enabling real-time monitoring of multi-agent workflows, state transitions, and LLM interactions.

Prerequisites

Make sure that the following prerequisites are met.

  • Python 3.8 or later is installed.

  • An LLM API key such as Anthropic, OpenAI, or any LLM provider supported by LangGraph.

  • A search API key (optional, for search tool functionality - Tavily or DuckDuckGo).

  • Instana is configured for your application, see Getting started.

Instrumenting LangGraph applications

To instrument your LangGraph application with Instana, complete the following steps:

  1. Install the required packages for LangGraph and OpenLLMetry.

    pip install langgraph langchain-anthropic duckduckgo-search traceloop-sdk
  2. Set your LLM provider and search tool credentials.

    export ANTHROPIC_API_KEY="<your-api-key>"
    export TAVILY_API_KEY="<your-tavily-api-key>"
    Note:

    Replace with your LLM provider's environment variable if using a different provider (e.g., OPENAI_API_KEY for OpenAI). The Tavily API key is optional if you're using a different search tool instead.

  3. Create your LangGraph application.

    A multi-agent workflow with a researcher and a writer is shown in the following example:

    import os
    from typing import Literal
    from langchain_anthropic import ChatAnthropic
    from langchain_community.tools import DuckDuckGoSearchRun
    from langchain_core.messages import BaseMessage, HumanMessage
    from langgraph.prebuilt import create_react_agent
    from langgraph.graph import MessagesState, StateGraph, START, END
    from langgraph.types import Command
    from traceloop.sdk import Traceloop
    from traceloop.sdk.decorators import workflow
    
    # Initialize OpenLLMetry
    Traceloop.init(app_name="langgraph_demo")
    
    # Define search tool
    search_tool = DuckDuckGoSearchRun()
    
    # Define LLM
    llm = ChatAnthropic(
        model="claude-3-5-sonnet-20241022",
        temperature=0.7,
        max_tokens=600
    )
    
    def get_next_node(last_message: BaseMessage, goto: str):
        if not last_message.content:
            return END
        return goto
    
    # Research agent and node
    research_agent = create_react_agent(
        llm,
        tools=[search_tool],
        state_modifier="You are a research assistant. Your job is to conduct thorough research."
    )
    
    def research_node(state: MessagesState) -> Command[Literal["writer", END]]:
        result = research_agent.invoke(state)
        goto = get_next_node(result["messages"][-1], "writer")
        result["messages"][-1] = HumanMessage(
            content=result["messages"][-1].content,
            name="researcher"
        )
        return Command(
            update={"messages": result["messages"]},
            goto=goto
        )
    
    # Writer agent and node
    writer_agent = create_react_agent(
        llm,
        tools=[search_tool],
        state_modifier="You are a blog writer. Your task is to create a well-structured blog post."
    )
    
    def writer_node(state: MessagesState) -> Command[Literal[END]]:
        result = writer_agent.invoke(state)
        result["messages"][-1] = HumanMessage(
            content=result["messages"][-1].content,
            name="writer"
        )
        return Command(
            update={"messages": result["messages"]},
            goto=END
        )
    
    # Define workflow graph
    workflow = StateGraph(MessagesState)
    workflow.add_node("researcher", research_node)
    workflow.add_node("writer", writer_node)
    workflow.add_edge(START, "researcher")
    graph = workflow.compile()
    
    # Execute workflow
    @workflow(name="langgraph_multi_agent_workflow")
    def run_workflow():
        events = graph.stream(
            {
                "messages": [
                    (
                        "user",
                        "First, research AI trends in 2025. "
                        "Then, based on that research, create a well-structured blog post."
                    )
                ]
            },
            {"recursion_limit": 150}
        )
    
        # Print workflow execution steps
        for event in events:
            print(event)
            print("----")
    
    if __name__ == "__main__":
        run_workflow()
  4. Run your LangGraph application.

    python langgraph_app.py

    The application executes the multi-agent workflow and displays the execution steps:

    Figure 1. LangGraph output
    LangGraph output
  5. View data in Instana.

    After running your application, data will appear in the Instana Gen AI observability dashboard:

    Figure 2. LangGraph traces in Instana
    LangGraph traces in Instana

    The dashboard shows:

    • Multi-agent workflow execution

    • State transitions between agents

    • Tool invocations (search operations)

    • LLM interactions with latency and token usage

    • Conditional routing decisions

Troubleshooting

For common issues such as traces not appearing or connection errors, see Troubleshooting.

Import errors with create_react_agent

Issue: Metrics or traces are incomplete when using create_react_agent.

Possible cause: The data is imported from the wrong package.

To troubleshoot this issue, try importing create_react_agent from langgraph.prebuilt:

# Correct import
from langgraph.prebuilt import create_react_agent

# Don't use this
from langchain.agents import create_react_agent

Workflow execution errors

Issue: Workflow fails with recursion limit errors.

To troubleshoot this issue, try adjusting the recursion_limit parameter in the stream() call based on your workflow complexity. For complex multi-agent workflows, you may need to increase it beyond the default value.