How to use LangSmith without LangChain
Quick answer
Use the LangSmith Python SDK directly by installing
langsmith, initializing a Client with your API key, and manually tracing your AI calls with the @traceable decorator or client.trace() method. This approach enables LangSmith's observability features without requiring LangChain.PREREQUISITES
Python 3.8+LangSmith API keypip install langsmith
Setup
Install the langsmith package and set your LangSmith API key as an environment variable.
- Install LangSmith SDK:
pip install langsmith - Set environment variable:
export LANGSMITH_API_KEY=your_api_key(Linux/macOS) orset LANGSMITH_API_KEY=your_api_key(Windows)
pip install langsmith Step by step
Initialize the LangSmith Client and use the @traceable decorator to trace a simple function that calls an AI model. This example shows how to manually trace calls without LangChain.
import os
from langsmith import Client, traceable
# Initialize LangSmith client with API key from environment
client = Client(api_key=os.environ["LANGSMITH_API_KEY"])
@traceable(client=client)
def simple_ai_call(prompt: str) -> str:
# Simulate an AI call (replace with your actual AI call)
response = f"Response to: {prompt}"
return response
if __name__ == "__main__":
result = simple_ai_call("Hello LangSmith without LangChain")
print("AI call result:", result) output
AI call result: Response to: Hello LangSmith without LangChain
Common variations
You can also use client.trace() as a context manager for manual tracing or integrate LangSmith with other AI SDKs by wrapping calls. Async tracing is supported similarly with async functions.
import asyncio
from langsmith import Client
client = Client(api_key=os.environ["LANGSMITH_API_KEY"])
async def async_ai_call(prompt: str) -> str:
# Simulate async AI call
await asyncio.sleep(0.1)
return f"Async response to: {prompt}"
async def main():
async with client.trace("async_call") as trace:
response = await async_ai_call("Async LangSmith tracing")
trace.add_output(response)
print("Async AI call result:", response)
if __name__ == "__main__":
asyncio.run(main()) output
Async AI call result: Async response to: Async LangSmith tracing
Troubleshooting
- If you see authentication errors, verify your
LANGSMITH_API_KEYenvironment variable is set correctly. - Ensure network connectivity to LangSmith's API endpoint.
- For missing imports, confirm
langsmithis installed and up to date.
Key Takeaways
- Use the LangSmith Python SDK directly by initializing
Clientwith your API key. - Trace AI calls manually with the
@traceabledecorator orclient.trace()context manager. - LangSmith works independently of LangChain for observability and tracing.
- Async and sync tracing are both supported in the standalone SDK.
- Always set
LANGSMITH_API_KEYin your environment for authentication.