LangSmith tracing helps you watch what your LangChain app does step-by-step. It records details so you can understand and fix your app easily.
Setting up LangSmith tracing in LangChain
Start learning this pattern below
Jump into concepts and practice - no test required
import os os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "lsv2_your-key-here" # Use your LangChain components as usual - tracing is automatic from langchain.chat_models import ChatOpenAI llm = ChatOpenAI()
Set LANGCHAIN_TRACING_V2="true" and LANGCHAIN_API_KEY environment variables to start tracing.
LangChain components automatically send traces to LangSmith when env vars are set.
import os os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "lsv2_your-key-here" from langchain.chat_models import ChatOpenAI llm = ChatOpenAI() response = llm.invoke("Hello!")
import os os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "lsv2_your-key-here" from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain llm = ChatOpenAI() prompt = PromptTemplate.from_template("Echo: {input}") chain = LLMChain(llm=llm, prompt=prompt) result = chain.invoke({"input": "Input text"})["text"]
This program sets up LangSmith tracing for a ChatOpenAI LLM by configuring environment variables. It runs a prompt asking for the capital of France and prints the response. View the trace in the LangSmith dashboard.
import os from langchain.chat_models import ChatOpenAI # Set environment variables for LangSmith tracing os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "lsv2_your-key-here" # Create a ChatOpenAI LLM with tracing enabled automatically llm = ChatOpenAI() # Run a prompt response = llm.invoke("What is the capital of France?") print("Response:", response.content)
Make sure you have langchain, langsmith, and openai installed. Get your API key from LangSmith.
Tracing can slow down your app slightly because it records extra details.
You can view traces in LangSmith's dashboard or export them for analysis.
LangSmith tracing helps you watch your LangChain app's steps clearly.
Enable tracing by setting LANGCHAIN_TRACING_V2="true" and LANGCHAIN_API_KEY.
Use tracing to debug, understand, and improve your LangChain workflows.
Practice
Solution
Step 1: Understand LangSmith tracing purpose
LangSmith tracing is designed to help watch and understand the steps your LangChain app takes.Step 2: Identify the correct purpose
It does not speed up execution, fix errors automatically, or encrypt data, but helps monitor and visualize workflows.Final Answer:
To monitor and visualize the steps of your LangChain workflows -> Option DQuick Check:
Tracing = Monitoring steps [OK]
- Thinking tracing speeds up code
- Assuming tracing fixes bugs automatically
- Confusing tracing with data encryption
Solution
Step 1: Recall LangChainTracer creation syntax
The LangChainTracer is created by calling its constructor directly: LangChainTracer()Step 2: Check options for correct syntax
Options B, C, and D use incorrect function or class names or syntax not used in LangChain.Final Answer:
tracer = LangChainTracer() -> Option AQuick Check:
Constructor call = LangChainTracer() [OK]
- Using wrong class names like LangSmithTracer
- Calling non-existent functions like createTracer()
- Using 'new' keyword which is not Python syntax
from langchain.chat_models import ChatOpenAI
from langchain.callbacks import LangChainTracer
tracer = LangChainTracer()
llm = ChatOpenAI(callbacks=[tracer])
response = llm.chat([{'role': 'user', 'content': 'Hello!'}])Solution
Step 1: Understand passing tracer as callback
Passing LangChainTracer in callbacks enables tracing of LLM steps.Step 2: Analyze code behavior
The LLM will send its internal steps to LangSmith via the tracer, enabling monitoring.Final Answer:
The LLM will log its steps to LangSmith for tracing -> Option AQuick Check:
Callbacks with tracer = tracing enabled [OK]
- Assuming no tracing happens without explicit start call
- Thinking callbacks cause syntax errors here
- Believing tracer disables output
from langchain.chat_models import ChatOpenAI from langchain.callbacks import LangChainTracer tracer = LangChainTracer llm = ChatOpenAI(callbacks=[tracer])
Solution
Step 1: Check LangChainTracer assignment
tracer = LangChainTracer misses parentheses, so tracer is a class, not an instance.Step 2: Analyze usage in callbacks
Passing callbacks=[tracer] passes the class instead of an instance, causing a runtime error when callbacks are used.Final Answer:
LangChainTracer is assigned without parentheses, missing instantiation -> Option BQuick Check:
Instantiate with () to create tracer object [OK]
- Assigning class instead of instance
- Calling instance as function
- Ignoring callbacks parameter usage
Solution
Step 1: Instantiate LangChainTracer correctly
Use tracer = LangChainTracer() to create the tracer instance.Step 2: Pass tracer in callbacks for both LLM and chain
Both components accept callbacks as a list; passing [tracer] enables tracing on both.Step 3: Evaluate options
tracer = LangChainTracer() llm = ChatOpenAI(callbacks=[tracer]) chain = SomeChain(llm=llm, callbacks=[tracer]) correctly instantiates tracer and passes it as a list to both components. Others miss instantiation, use wrong types, or omit callbacks.Final Answer:
tracer = LangChainTracer() llm = ChatOpenAI(callbacks=[tracer]) chain = SomeChain(llm=llm, callbacks=[tracer]) -> Option CQuick Check:
Instantiate tracer and pass as list to callbacks [OK]
- Not instantiating tracer with ()
- Passing tracer directly instead of in a list
- Forgetting to add callbacks to chain
