Lesson 12 / 31

Automatic Tracing for LangChain and LangGraph

Explain how LangChain and LangGraph runs appear in LangSmith and add tags and metadata through config.

By · 15 years in web development

Zero-code tracing and its limits

When the LangSmith environment variables are set, LangChain and LangGraph trace automatically: every chain, prompt template, model call, retriever, tool and graph node becomes a run, nested in the order it executed. For a LangGraph agent this means you can see each node, each loop iteration and the state passed between nodes. You add context through the config argument on invoke(): tags, metadata and run_name flow down to child runs, which is how you mark the user, session or experiment. The trade-off is volume and noise: frameworks create many small runs (prompt formatting, output parsing), so traces can be deep. Learn to collapse the tree and filter by run type. Also remember that auto tracing only covers framework code; if your app calls a database or an internal API outside the framework, wrap that function with @traceable so it appears in the same trace.

Passing tags and metadata through config

Works for any LangChain runnable or compiled LangGraph graph. chain stands for your runnable.

result = chain.invoke(
    {"question": "Where is my order?"},
    config={
        "run_name": "order-status",
        "tags": ["support", "prod"],
        "metadata": {"session_id": "chat-77", "user_id": "u-1042"},
    },
)

Auto-recorded train journey

Automatic tracing is like a train's black box: it logs every signal and station without the driver doing anything. But if a passenger took an auto-rickshaw after getting off, the black box knows nothing about that leg; you record it yourself.

Quick check: Your LangGraph agent is traced automatically, but a direct database query inside a helper function does not appear. What should you do?

  • Disable LangGraph tracing and log everything manually
  • Switch the project name
  • Increase the trace sampling rate
  • Wrap the helper with `@traceable` so it joins the current trace
Answer

Wrap the helper with `@traceable` so it joins the current trace — Auto tracing only covers framework components. Decorating your own function makes it a child run of the currently active trace.