0Pricing
AI Engineering Academy · Ders

LangSmith ile İzleme

Her zincir adımını, LLM çağrısını, belirteç sayısını ve gecikmeyi aranabilir bir iz gezginine kaydetmek için LangChain uygulamanızı LangSmith izlemeyle donatın.

LangSmith ile İzleme, CoddyKit'te ücretsiz bir AI Engineering Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, AI Engineering Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. AI Engineering Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

What Is LangSmith?

LangSmith is an observability platform built specifically for LLM applications. It automatically captures traces of every LangChain run — every chain step, LLM call, tool execution, retrieval, and output parser — and displays them in a searchable, hierarchical trace explorer. You can filter traces by latency, cost, error status, or custom metadata, and replay any trace to debug failures.

# Install: pip install langsmith
import os

# Set environment variables to enable automatic tracing
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
os.environ['LANGCHAIN_API_KEY'] = 'lsv2_...your_key_here...'
os.environ['LANGCHAIN_PROJECT'] = 'my-rag-app'  # project name in LangSmith UI

# That's all - LangChain now sends traces to LangSmith automatically
# No code changes needed to your chain or agent

Automatic Tracing with Zero Code Changes

The most compelling feature of LangSmith is that once you set the three environment variables, every LangChain operation is automatically traced with no additional code. Every LCEL chain, every ChatOpenAI call, every retriever call, every tool execution gets captured with inputs, outputs, timing, and token counts. You can deploy LangSmith tracing to production with a single environment variable change.

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# This chain is automatically traced - no extra code needed
llm = ChatOpenAI(model='gpt-4o')
prompt = ChatPromptTemplate.from_template('Answer this question: {question}')
chain = prompt | llm | StrOutputParser()

# This call creates a trace in LangSmith showing:
# - The formatted prompt (with question substituted)
# - The LLM call with model, temperature, token counts
# - The parsed output
# - End-to-end latency and cost
result = chain.invoke({'question': 'What is RAG?'})
print(result)

Tracing RAG Pipelines

For RAG applications, LangSmith traces are especially valuable because they capture the entire retrieve-then-generate pipeline. You can see: which documents were retrieved, what their similarity scores were, how the context was formatted in the prompt, and what the LLM generated. This makes it immediately obvious whether a wrong answer was caused by bad retrieval or poor generation.

from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import Chroma
from langchain_core.runnables import RunnablePassthrough

embeddings = OpenAIEmbeddings()
vectorstore = Chroma(embedding_function=embeddings)
retriever = vectorstore.as_retriever(search_kwargs={'k': 5})

rag_chain = (
    {'context': retriever, 'question': RunnablePassthrough()}
    | ChatPromptTemplate.from_template('Context: {context}\n\nQuestion: {question}\n\nAnswer:')
    | ChatOpenAI(model='gpt-4o')
    | StrOutputParser()
)

# LangSmith traces EVERY step:
# 1. Retriever: query embedding + vector search + returned documents (with scores)
# 2. Prompt: formatted template with context injected
# 3. LLM: full prompt, response, tokens, latency, cost
# 4. Parser: final string output
answer = rag_chain.invoke('What is the capital of France?')

Adding Metadata to Traces

By default, LangSmith traces contain the inputs and outputs of each step. You can enrich traces with custom metadata tags: the user ID, session ID, feature flag values, A/B test variant, or any other context that helps you filter and analyze traces in the UI. Use the RunnableConfig to pass metadata that will appear on every trace from that request.

from langchain_core.runnables import RunnableConfig

def handle_user_request(user_id: str, query: str, ab_variant: str):
    config = RunnableConfig(
        tags=['production', ab_variant],
        metadata={
            'user_id': user_id,
            'ab_variant': ab_variant,
            'feature': 'rag_qa'
        }
    )
    
    result = rag_chain.invoke(query, config=config)
    return result

# In LangSmith UI you can now:
# - Filter traces by user_id to debug a specific user's issue
# - Compare latency between ab_variant='A' and ab_variant='B'
# - See all traces tagged 'production' vs 'staging'

Manual Span Creation

For code that does not go through LangChain (custom API calls, database queries, preprocessing steps), you can create manual spans using the LangSmith client directly. This ensures your non-LangChain steps are captured in the same trace as the LangChain steps, giving you a complete picture of each request's execution path.

from langsmith import Client, traceable

client = Client()

# Decorate functions to auto-trace them
@traceable(name='preprocess_query')
def preprocess_query(raw_query: str) -> str:
    # This step is now traced even though it doesn't use LangChain
    cleaned = raw_query.strip().lower()
    cleaned = ' '.join(cleaned.split())  # normalize whitespace
    return cleaned

@traceable(name='fetch_user_history')
def fetch_user_history(user_id: str) -> list[str]:
    # Database call - also traced
    return db.query('SELECT message FROM chat_history WHERE user_id = ? ORDER BY timestamp DESC LIMIT 5', user_id)

# All three steps appear in the same trace
def handle_request(user_id: str, raw_query: str):
    query = preprocess_query(raw_query)         # traced
    history = fetch_user_history(user_id)       # traced
    result = rag_chain.invoke({'query': query, 'history': history})  # traced by LangChain
    return result

Evaluating Traces in LangSmith

LangSmith includes an evaluation framework that lets you run evaluators over your trace dataset. You can select a set of traced examples, run automated evaluators (including LLM-as-judge scorers for correctness and relevance), and compare the results across different pipeline versions. This turns your production traces into a feedback loop for improving your application.

from langsmith.evaluation import evaluate, LangChainStringEvaluator

# Create an evaluator that uses an LLM to judge correctness
correctness_evaluator = LangChainStringEvaluator(
    'qa',
    config={'llm': ChatOpenAI(model='gpt-4o')}
)

# Run evaluation against a dataset of traced examples
results = evaluate(
    rag_chain,
    data='my-rag-test-set',      # name of dataset in LangSmith
    evaluators=[correctness_evaluator],
    experiment_prefix='rag-v2-chunking-test'
)

print('Evaluation results:')
print(f'Correctness: {results.results["correctness"].mean():.2f}')
print(f'Average latency: {results.results["latency"].mean():.2f}s')

Creating Test Datasets from Traces

One of LangSmith's most powerful features is the ability to create test datasets directly from production traces. When you notice an interesting trace (a failure, an edge case, or a great example), you can add it to a dataset with a single click. Over time, you build a comprehensive regression test suite from real user queries rather than synthetic examples.

from langsmith import Client

client = Client()

# Create a dataset from existing traces
dataset = client.create_dataset('rag-regression-tests')

# Add examples from production traces (by trace ID)
for trace_id in failed_trace_ids:
    run = client.read_run(trace_id)
    client.create_example(
        inputs=run.inputs,
        outputs={'answer': run.outputs.get('output', '')},
        dataset_id=dataset.id,
        metadata={'source': 'production_failure', 'date': run.start_time.isoformat()}
    )

print(f'Added {len(failed_trace_ids)} examples to regression test dataset')

Filtering and Searching Traces

In production, you will have thousands of traces. LangSmith's UI and API support rich filtering and search: find traces with latency above a threshold, with a specific error type, from a specific user, containing a specific keyword in the output, or with a completion token count above a limit. This makes it practical to investigate specific failure categories or monitor the behavior of specific users.

from langsmith import Client

client = Client()

# Find slow traces (useful for performance investigation)
slow_runs = client.list_runs(
    project_name='my-rag-app',
    filter='gt(latency, 5)',  # latency > 5 seconds
    limit=20
)

# Find error traces
error_runs = client.list_runs(
    project_name='my-rag-app',
    filter='eq(error, true)',
    limit=50
)

# Find traces from a specific user
user_runs = client.list_runs(
    project_name='my-rag-app',
    filter='has(metadata, user_id="user_abc123")',
    limit=100
)

for run in slow_runs:
    print(f'Slow run: {run.id}, latency: {run.end_time - run.start_time}')

Comparing Experiments in LangSmith

LangSmith supports experiment comparison: run the same test dataset through two versions of your pipeline (e.g., chunk size 500 vs chunk size 1000), and compare them side by side on latency, cost, and quality metrics. This makes it easy to validate that a pipeline change is an improvement rather than a regression before deploying to production.

from langsmith.evaluation import evaluate

test_dataset = 'my-rag-eval-set'

# Run experiment A: chunk size 500
results_a = evaluate(
    rag_pipeline_v1,
    data=test_dataset,
    evaluators=[correctness_evaluator, relevance_evaluator],
    experiment_prefix='chunk-500'
)

# Run experiment B: chunk size 1000
results_b = evaluate(
    rag_pipeline_v2,
    data=test_dataset,
    evaluators=[correctness_evaluator, relevance_evaluator],
    experiment_prefix='chunk-1000'
)

# Compare in LangSmith UI: Experiments tab shows A vs B side by side
# Or compare programmatically:
print(f'Correctness - v1: {results_a.results["correctness"].mean():.2f}, v2: {results_b.results["correctness"].mean():.2f}')

LangSmith in Production

LangSmith is available as a hosted SaaS at smith.langchain.com and as a self-hosted option. In production, tracing can be made asynchronous (non-blocking) to avoid adding latency to your critical path. You can also sample traces (e.g., trace only 10% of requests in high-traffic production) to control cost while maintaining visibility. The dashboard shows real-time graphs of request volume, latency, cost, and error rate.

import os

# Production configuration
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'
os.environ['LANGCHAIN_PROJECT'] = 'production'

# Enable async tracing (non-blocking - does not add latency to requests)
os.environ['LANGCHAIN_CALLBACKS_BACKGROUND'] = 'true'

# Optional: sample 10% of traces to reduce cost in high-traffic scenarios
import random

def should_trace() -> bool:
    return random.random() < 0.10  # 10% sampling rate

def handle_request(query):
    config = RunnableConfig()
    if not should_trace():
        config = RunnableConfig(callbacks=[])  # disable tracing for this request
    return rag_chain.invoke(query, config=config)

LangSmith vs Custom Logging

You could build your own trace logging system, and for some use cases that is the right choice. LangSmith's advantages over custom logging are: zero-code integration with LangChain, a purpose-built UI for exploring LLM traces (not generic Kibana/Grafana dashboards), native support for evaluation and experiment comparison, and automatic token count and cost tracking. The tradeoff is vendor dependency and cost at scale.

Quick Check

Test your understanding of tracing with LangSmith from this lesson.

Lesson Recap

In this lesson you learned: LangSmith enables automatic end-to-end tracing of LangChain applications by setting three environment variables with no code changes, the @traceable decorator extends tracing to non-LangChain steps like database calls and preprocessing, and experiment comparison lets you validate pipeline improvements against a test dataset before deploying. Next up we explore Langfuse as a model-agnostic observability alternative.

Sıkça Sorulan Sorular

“LangSmith ile İzleme” dersi ücretsiz mi?

Evet — “LangSmith ile İzleme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve AI Engineering Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. AI Engineering Academy kursu toplamda 4 dersten oluşur.

“LangSmith ile İzleme” dersinde ne öğreneceğim?

Her zincir adımını, LLM çağrısını, belirteç sayısını ve gecikmeyi aranabilir bir iz gezginine kaydetmek için LangChain uygulamanızı LangSmith izlemeyle donatın. AI Engineering Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

AI Engineering Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te AI Engineering Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.

“LangSmith ile İzleme” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu AI Engineering Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her AI Engineering Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. LLM Uygulamalarında Hata Ayıklamanın Neden Zor Olduğu
  2. LangSmith ile İzleme
  3. Modelden Bağımsız Gözlemlenebilirlik İçin Langfuse
  4. Gecikme, Maliyet ve Kalite Düşüşleri İçin Uyarı
← AI Engineering Academy Sayfasına Dön