Debug Fix easy · 3 min read

Fix LangSmith tracing not working

Quick answer
To fix LangSmith tracing not working, ensure you set the environment variables LANGCHAIN_TRACING_V2, LANGCHAIN_API_KEY, and LANGCHAIN_PROJECT before importing LangChain or LangSmith. Use the LangSmith Client with the @traceable decorator to wrap your functions for automatic tracing.
ERROR TYPE config_error
⚡ QUICK FIX
Set LANGCHAIN_TRACING_V2 and LANGCHAIN_API_KEY environment variables before importing LangChain and decorate your functions with @traceable from langsmith.

Why this happens

LangSmith tracing fails when environment variables LANGCHAIN_TRACING_V2 and LANGCHAIN_API_KEY are not set before importing LangChain or LangSmith. Also, not using the @traceable decorator or the Client properly prevents automatic tracing. Typical error is silent: no traces appear in the LangSmith dashboard despite correct API key.

Example broken code:

import os
from langsmith import Client

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

def my_llm_call(prompt: str) -> str:
    # Missing @traceable decorator
    return "response"

result = my_llm_call("Hello")

The fix

Set environment variables LANGCHAIN_TRACING_V2, LANGCHAIN_API_KEY, and LANGCHAIN_PROJECT before importing LangChain or LangSmith. Use the Client from langsmith and decorate your functions with @traceable to enable automatic tracing of LLM calls.

This works because LangSmith hooks into LangChain calls only if tracing is enabled early and functions are wrapped for observation.

python
import os
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = os.environ["LANGSMITH_API_KEY"]
os.environ["LANGCHAIN_PROJECT"] = "my-project"

from langsmith import Client, traceable

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

@traceable()
def my_llm_call(prompt: str) -> str:
    # Your LLM call logic here
    return "response to: " + prompt

result = my_llm_call("Hello")
print(result)
output
response to: Hello

Preventing it in production

  • Always set LANGCHAIN_TRACING_V2 and LANGCHAIN_API_KEY environment variables before any LangChain or LangSmith imports.
  • Use the @traceable decorator on all functions that invoke LLMs to ensure calls are traced.
  • Implement retry logic around API calls to handle transient network or rate limit errors.
  • Validate tracing is active by checking the LangSmith dashboard or logs after deployment.

Key Takeaways

  • Set LANGCHAIN_TRACING_V2 and LANGCHAIN_API_KEY before importing LangChain or LangSmith.
  • Use the @traceable decorator from langsmith to enable automatic tracing of LLM calls.
  • Validate tracing setup early in development to avoid silent failures in production.
Verified 2026-04
Verify ↗