Context
When using LangChain or LangGraph with LangSmith for tracing and observability, you need to properly configure environment variables for authentication. Users often need guidance on how to set up these environment variables correctly across different operating systems and environments.
Answer
There are several ways to set up your LangSmith API key environment variables:
Method 1: Using .env File (Recommended)
Create a file named `.env` in your project directory
Add the following lines to your .env file:
LANGCHAIN_API_KEY="your-api-key-here" LANGCHAIN_TRACING_V2=trueIn your Python code, add the following at the beginning of your file:
from dotenv import load_dotenv load_dotenv()
Method 2: Direct Environment Variable Setting
For Windows PowerShell:
$env:LANGCHAIN_API_KEY="your-api-key-here"
$env:LANGCHAIN_TRACING_V2="true"
For Linux/Mac Terminal:
export LANGCHAIN_API_KEY="your-api-key-here"
export LANGCHAIN_TRACING_V2=true
Important Notes:
The environment variable name is `LANGCHAIN_API_KEY` (not LANGSMITH_API_KEY)
In some deployment environments (like certain cloud platforms), these variables might be automatically configured for you
You can find your API key in the LangSmith UI when creating a new project