How to Intermediate · 3 min read

How to use LangSmith with LangGraph

Quick answer
Use the langsmith Python SDK to create a Client and decorate your LangGraph node functions with @client.traceable to automatically trace state transitions. Compile your StateGraph into an app and invoke it normally; LangSmith captures detailed traces for observability.

PREREQUISITES

  • Python 3.8+
  • pip install langsmith langgraph
  • LangSmith API key set in environment variable LANGSMITH_API_KEY

Setup

Install the required packages and set your LangSmith API key in the environment.

  • Install packages: pip install langsmith langgraph
  • Set environment variable: export LANGSMITH_API_KEY=your_api_key (Linux/macOS) or set LANGSMITH_API_KEY=your_api_key (Windows)
bash
pip install langsmith langgraph

Step by step

This example shows how to create a LangGraph stateful agent and use LangSmith to trace its execution by decorating node functions with @client.traceable.

python
import os
from langsmith import Client, traceable
from langgraph.graph import StateGraph, END
from typing import TypedDict

# Initialize LangSmith client
client = Client(api_key=os.environ["LANGSMITH_API_KEY"])

# Define the state type
class State(TypedDict):
    messages: list[str]

# Define a traced node function
@client.traceable
def add_greeting(state: State) -> State:
    new_messages = state["messages"] + ["Hello from LangGraph with LangSmith!"]
    return {"messages": new_messages}

# Create the graph
graph = StateGraph(State)
graph.add_node("greet", add_greeting)
graph.set_entry_point("greet")
graph.add_edge("greet", END)

# Compile the graph into an app
app = graph.compile()

# Invoke the app
result = app.invoke({"messages": []})
print("Final state messages:", result["messages"])
output
Final state messages: ['Hello from LangGraph with LangSmith!']

Common variations

You can trace multiple nodes by decorating each with @client.traceable. For asynchronous LangGraph nodes, use @client.traceable on async functions. LangSmith automatically captures traces for all decorated nodes, enabling detailed observability.

python
import asyncio

@client.traceable
async def async_node(state: State) -> State:
    await asyncio.sleep(0.1)
    new_messages = state["messages"] + ["Async node executed"]
    return {"messages": new_messages}

# Add async node example
graph.add_node("async_node", async_node)
graph.add_edge("greet", "async_node")
graph.add_edge("async_node", END)

app = graph.compile()

# Run async invocation
async def main():
    result = await app.invoke({"messages": []})
    print("Async final messages:", result["messages"])

asyncio.run(main())
output
Async final messages: ['Hello from LangGraph with LangSmith!', 'Async node executed']

Troubleshooting

  • If you see Missing LANGSMITH_API_KEY errors, ensure your environment variable is set correctly before running your script.
  • If traces do not appear in the LangSmith dashboard, verify network connectivity and that your API key is valid.
  • For issues with async nodes, confirm you are using Python 3.8+ and that your functions are properly declared async.

Key Takeaways

  • Use langsmith.Client and @client.traceable to instrument LangGraph nodes for tracing.
  • Compile your StateGraph into an app and invoke it normally; LangSmith captures detailed execution traces.
  • Support for async LangGraph nodes is seamless with @client.traceable on async functions.
  • Ensure your LANGSMITH_API_KEY environment variable is set to enable tracing.
  • LangSmith enhances debugging and observability of complex LangGraph stateful agents.
Verified 2026-04
Verify ↗