LangSmith environment variables setup
Quick answer
Set the
LANGCHAIN_TRACING_V2 environment variable to "true" to enable LangSmith tracing. Also set LANGCHAIN_API_KEY with your LangSmith API key and optionally LANGCHAIN_PROJECT to specify your project name. These variables enable automatic tracing of LangChain calls and LangSmith SDK usage.PREREQUISITES
Python 3.8+LangSmith API keypip install langsmith
Setup
Install the LangSmith Python SDK and set the required environment variables to enable tracing and authentication.
pip install langsmith Step by step
Set the environment variables in your shell or Python environment before running your LangChain or LangSmith code.
LANGCHAIN_TRACING_V2=trueenables LangSmith tracing.LANGCHAIN_API_KEYholds your LangSmith API key.LANGCHAIN_PROJECT(optional) specifies the project name for organizing traces.
Example Python code to verify environment variables and initialize LangSmith client:
import os
from langsmith import Client
# Verify environment variables
print("Tracing enabled:", os.environ.get("LANGCHAIN_TRACING_V2"))
print("API key set:", bool(os.environ.get("LANGCHAIN_API_KEY")))
print("Project name:", os.environ.get("LANGCHAIN_PROJECT"))
# Initialize LangSmith client
client = Client(api_key=os.environ["LANGCHAIN_API_KEY"])
# Example usage: simple traceable function
@client.traceable
def greet(name: str) -> str:
return f"Hello, {name}!"
print(greet("World")) output
Tracing enabled: true API key set: True Project name: my-project Hello, World!
Common variations
You can set environment variables in different ways depending on your environment:
- In Unix/Linux/macOS shell:
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=your_api_keyexport LANGCHAIN_PROJECT=my-project - In Windows PowerShell:
$env:LANGCHAIN_TRACING_V2 = "true"$env:LANGCHAIN_API_KEY = "your_api_key"$env:LANGCHAIN_PROJECT = "my-project" - Set variables programmatically in Python before imports:
import osos.environ["LANGCHAIN_TRACING_V2"] = "true"os.environ["LANGCHAIN_API_KEY"] = "your_api_key"
LangSmith tracing works automatically with LangChain after setting these variables.
Troubleshooting
- If tracing does not appear, verify
LANGCHAIN_TRACING_V2is set totrueexactly. - If authentication fails, confirm
LANGCHAIN_API_KEYis correct and has no extra spaces. - Check that environment variables are set in the same context where your Python process runs.
- Restart your IDE or terminal after setting environment variables to ensure they are loaded.
Key Takeaways
- Set
LANGCHAIN_TRACING_V2=trueto enable LangSmith tracing. - Always provide
LANGCHAIN_API_KEYfor authentication. - Optionally set
LANGCHAIN_PROJECTto organize traces by project. - Environment variables must be set in the runtime environment before execution.
- LangSmith SDK uses these variables for automatic LangChain call tracing.