How to beginner · 3 min read

How to set up LangSmith

Quick answer
To set up LangSmith, install the langsmith Python package and set the environment variables LANGSMITH_API_KEY and LANGCHAIN_API_KEY (they are the same key). Initialize the langsmith.Client with your API key and use the @traceable decorator to trace your AI calls.

PREREQUISITES

  • Python 3.8+
  • LangSmith API key
  • pip install langsmith

Setup

Install the langsmith Python package and set your API key in environment variables. LANGCHAIN_API_KEY and LANGSMITH_API_KEY must be set to the same value for tracing to work.

bash
pip install langsmith

Step by step

Use the langsmith.Client to initialize the client and decorate your functions with @traceable to automatically trace AI calls.

python
import os
from langsmith import Client, traceable

# Set your LangSmith API key in environment variables
# export LANGSMITH_API_KEY=os.environ["LANGSMITH_API_KEY"]
# export LANGCHAIN_API_KEY=os.environ["LANGSMITH_API_KEY"]  # same as LANGSMITH_API_KEY

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

@traceable()
def generate_response(prompt: str) -> str:
    # Simulate an AI call or your LLM invocation here
    return f"Response to: {prompt}"

if __name__ == "__main__":
    result = generate_response("What is LangSmith?")
    print(result)
output
Response to: What is LangSmith?

Common variations

You can use langsmith.Client for manual tracing or rely on environment variable auto-tracing with LangChain. The @traceable decorator supports async functions as well.

python
import asyncio
from langsmith import Client, traceable
import os

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

@traceable()
async def async_generate(prompt: str) -> str:
    # Simulate async AI call
    await asyncio.sleep(0.1)
    return f"Async response to: {prompt}"

async def main():
    response = await async_generate("Async LangSmith setup")
    print(response)

if __name__ == "__main__":
    asyncio.run(main())
output
Async response to: Async LangSmith setup

Troubleshooting

  • If you see no traces in the LangSmith dashboard, verify that LANGSMITH_API_KEY and LANGCHAIN_API_KEY environment variables are set and identical.
  • Ensure your network allows outbound HTTPS requests to https://cloud.langsmith.com.
  • Use the latest langsmith package version to avoid compatibility issues.

Key Takeaways

  • Install the langsmith package and set LANGSMITH_API_KEY and LANGCHAIN_API_KEY environment variables to the same value.
  • Use langsmith.Client and the @traceable decorator to automatically trace AI calls in your Python code.
  • Async functions are supported by @traceable for flexible integration.
  • Check environment variables and network connectivity if traces do not appear in the LangSmith dashboard.
Verified 2026-04
Verify ↗