LangSmith
Integrate Kern with LangSmith to send traces and gain insights into your agent's performance.
Integrating Kern with LangSmith
LangSmith offers a comprehensive platform for tracing and monitoring AI model calls. By integrating Kern with LangSmith, you can utilize OpenInference to send traces and gain insights into your agent's performance.
Prerequisites
-
Create a LangSmith Account
- Sign up for an account at LangSmith.
- Obtain your API key from the LangSmith dashboard.
-
Set Environment Variables
Configure your environment with the LangSmith API key and other necessary settings:
1export LANGSMITH_API_KEY=<your-key>2export LANGSMITH_TRACING=true3export LANGSMITH_ENDPOINT=https://eu.api.smith.langchain.com # or https://api.smith.langchain.com for US4export LANGSMITH_PROJECT=<your-project-name> -
Install Dependencies
Ensure you have the necessary packages installed:
1uv pip install openai openinference-instrumentation-kern-ai opentelemetry-sdk opentelemetry-exporter-otlp
Sending Traces to LangSmith
This example demonstrates how to instrument your Kern agent with OpenInference and send traces to LangSmith.
1import os23from kern.agent import Agent4from kern.models.openai import OpenAIResponses5from kern.tools.hackernews import HackerNewsTools6from openinference.instrumentation.kern import AgnoInstrumentor7from opentelemetry import trace as trace_api8from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter9from opentelemetry.sdk.trace import TracerProvider10from opentelemetry.sdk.trace.export import SimpleSpanProcessor1112# Set the endpoint and headers for LangSmith13endpoint = "https://eu.api.smith.langchain.com/otel/v1/traces"14headers = {15 "x-api-key": os.getenv("LANGSMITH_API_KEY"),16 "Langsmith-Project": os.getenv("LANGSMITH_PROJECT"),17}1819# Configure the tracer provider20tracer_provider = TracerProvider()21tracer_provider.add_span_processor(22 SimpleSpanProcessor(OTLPSpanExporter(endpoint=endpoint, headers=headers))23)24trace_api.set_tracer_provider(tracer_provider=tracer_provider)2526# Start instrumenting kern27AgnoInstrumentor().instrument()2829# Create and configure the agent30agent = Agent(31 name="Stock Market Agent",32 model=OpenAIResponses(id="gpt-5.2"),33 tools=[HackerNewsTools()],34 markdown=True,35 debug_mode=True,36)3738# Use the agent39agent.print_response("What is news on the stock market?")Notes
- Environment Variables: Ensure your environment variables are correctly set for the API key, endpoint, and project name.
- Data Regions: Choose the appropriate
LANGSMITH_ENDPOINTbased on your data region.
By following these steps, you can effectively integrate Kern with LangSmith, enabling comprehensive observability and monitoring of your AI agents.