npx skills add ...
npx skills add langchain-ai/langsmith-skills --skill langsmith-trace
INVOKE THIS SKILL when working with LangSmith tracing OR querying traces. Covers adding tracing to applications and querying/exporting trace data. Uses the langsmith CLI tool.
npx skills add langchain-ai/langsmith-skills --skill langsmith-trace
Authentication is REQUIRED: either set the LANGSMITH_API_KEY environment variable, or pass the --api-key flag to CLI commands (preferred):
IMPORTANT: Always check the environment variables or .env file for LANGSMITH_PROJECT before querying or interacting with LangSmith. This tells you which project contains the relevant traces and data. If the LangSmith project is not available, use your best judgement to identify the right one.
CLI Tool
<trace_langchain_oss> For LangChain/LangGraph apps, tracing is automatic. Just set environment variables:
Optional variables:
LANGSMITH_PROJECT - specify project name (defaults to "default")LANGCHAIN_CALLBACKS_BACKGROUND=false - use for serverless to ensure traces complete before function exit (Python)
</trace_langchain_oss><trace_other_frameworks> For non-LangChain apps, if the framework has native OpenTelemetry support, use LangSmith's OpenTelemetry integration.
If the app is NOT using a framework, or using one without automatic OTel support, use the traceable decorator/wrapper and wrap your LLM client.
Use @traceable decorator and wrap_openai() for automatic tracing. ```python from langsmith import traceable from langsmith.wrappers import wrap_openai from openai import OpenAIclient = wrap_openai(OpenAI())
@traceable def my_llm_pipeline(question: str) -> str: resp = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": question}], ) return resp.choices[0].message.content
@traceable def rag_pipeline(question: str) -> str: docs = retrieve_docs(question) return generate_answer(question, docs)
@traceable(name="retrieve_docs") def retrieve_docs(query: str) -> list[str]: return docs
@traceable(name="generate_answer") def generate_answer(question: str, docs: list[str]) -> str: return client.chat.completions.create(...)
Best Practices:
wrap_openai()/wrapOpenAI() records every LLM call<traces_vs_runs>
Use the langsmith CLI to query trace data.
Understanding the difference is critical:
Generally, query traces first — they provide complete context and preserve hierarchy needed for trajectory analysis and dataset generation. </traces_vs_runs>
<command_structure> Two command groups with consistent behavior:
Key differences:
traces * | runs * | |
|---|---|---|
| Filters apply to | Root run only | Any matching run |
--run-type | Not available | Available |
| Returns | Full hierarchy | Flat list |
| Export output | Directory (one file/trace) | Single file |
| </command_structure> |
<querying_traces>
Query traces using the langsmith CLI. Commands are language-agnostic.
</querying_traces>
All commands support these filters (all AND together):Basic filters:
--trace-ids abc,def - Filter to specific traces--limit N - Max results--project NAME - Project name--last-n-minutes N - Time filter--since TIMESTAMP - Time filter (ISO format)--error / --no-error - Error status--name PATTERN - Name contains (case-insensitive)Performance filters:
--min-latency SECONDS - Minimum latency (e.g., 5 for >= 5s)--max-latency SECONDS - Maximum latency--min-tokens N - Minimum total tokens--tags tag1,tag2 - Has any of these tagsAdvanced filter:
--filter QUERY - Raw LangSmith filter query for complex cases (feedback, metadata, etc.)<export_format>
Export creates .jsonl files (one run per line) with these fields:
Use --include-io or --full to include inputs/outputs (required for dataset generation).
</export_format>