How to beginner · 3 min read

How to use LangSmith with LangChain

Quick answer
Use LangSmith as a callback handler in LangChain to track and visualize your AI workflows. Install langsmith, configure your API key, and pass LangSmithTracer to your LangChain chains or agents for seamless integration.

PREREQUISITES

  • Python 3.8+
  • LangChain 0.2+
  • LangSmith account and API key
  • pip install langchain langsmith

Setup

Install the required packages and set your LangSmith API key as an environment variable.

bash
pip install langchain langsmith

Step by step

This example shows how to use LangSmithTracer from langsmith with a simple LangChain LLMChain using OpenAI's gpt-4o model.

python
import os
from langchain import LLMChain, PromptTemplate
from langchain_openai import ChatOpenAI
from langsmith import LangSmithTracer

# Set your LangSmith API key in environment variable LANGSMITH_API_KEY
# os.environ["LANGSMITH_API_KEY"] = "your_langsmith_api_key"

# Initialize LangSmith tracer
tracer = LangSmithTracer()

# Create a simple prompt template
prompt = PromptTemplate(template="Translate this English text to French: {text}", input_variables=["text"])

# Initialize OpenAI chat model
llm = ChatOpenAI(model="gpt-4o", temperature=0)

# Create LLMChain with LangSmith tracer as callback
chain = LLMChain(llm=llm, prompt=prompt, callbacks=[tracer])

# Run the chain
result = chain.run(text="Hello, how are you?")
print("Translation:", result)
output
Translation: Bonjour, comment ça va ?

Common variations

  • Use LangSmithTracer with Agents or Chains by passing it in the callbacks parameter.
  • Enable asynchronous tracing by using async LangChain calls with the tracer.
  • Switch models by changing ChatOpenAI(model=...) to other supported models like gpt-4o-mini.
python
from langchain.agents import initialize_agent
from langchain.agents import Tool

# Example: Using LangSmithTracer with an agent
agent = initialize_agent(
    tools=[],
    llm=llm,
    agent="zero-shot-react-description",
    callbacks=[tracer],
    verbose=True
)

response = agent.run("What is the capital of France?")
print(response)
output
Paris

Troubleshooting

  • If you see no data in LangSmith dashboard, verify your LANGSMITH_API_KEY environment variable is set correctly.
  • Ensure your callbacks parameter includes LangSmithTracer() instance.
  • For network errors, check your internet connection and firewall settings.

Key Takeaways

  • Use LangSmithTracer as a callback in LangChain to enable automatic tracking.
  • Set your LangSmith API key in the environment variable LANGSMITH_API_KEY before running code.
  • LangSmith integrates seamlessly with LLMChain, Agents, and other LangChain components.
  • You can switch models or use async calls without changing the LangSmith integration.
  • Troubleshoot by verifying API keys and callback usage if no data appears in LangSmith dashboard.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗