python --version
pip install langsmith openai
Get from smith.langchain.com
export OPENAI_API_KEY=sk-...
Instrument your LLM application with LangSmith tracing to capture inputs, outputs, latency, and token usage for every call.
1 import os 2 from langsmith import traceable 3 from openai import OpenAI 4 5 # Configure via env vars: 6 # LANGCHAIN_TRACING_V2=true 7 # LANGCHAIN_API_KEY=ls__... 8 # LANGCHAIN_PROJECT=my-project 9 10 os.environ.setdefault("LANGCHAIN_TRACING_V2", "true") 11 os.environ.setdefault("LANGCHAIN_PROJECT", "ai-observability-demo") 12 13 client = OpenAI() 14 15 @traceable(name="chat_completion", tags=["production"]) 16 def chat(user_message: str, user_id: str = "anonymous") -> str: 17 response = client.chat.completions.create( 18 model="gpt-4o-mini", 19 messages=[ 20 {"role": "system", "content": "You are a helpful assistant."}, 21 {"role": "user", "content": user_message}, 22 ], 23 metadata={"user_id": user_id}, 24 ) 25 return response.choices[0].message.content or "" 26 27 @traceable(name="multi_step_pipeline") 28 def pipeline(query: str) -> dict: 29 # Step 1: classify 30 category = chat(f"Classify this query in one word: {query}", user_id="system") 31 # Step 2: answer 32 answer = chat(query, user_id="user-123") 33 return {"category": category, "answer": answer} 34 35 if __name__ == "__main__": 36 result = pipeline("Explain the difference between RAG and fine-tuning") 37 print(result["answer"]) 38
Sign in to share your feedback and join the discussion.