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

  1. Create a LangSmith Account

    • Sign up for an account at LangSmith.
    • Obtain your API key from the LangSmith dashboard.
  2. Set Environment Variables

    Configure your environment with the LangSmith API key and other necessary settings:

    1export LANGSMITH_API_KEY=<your-key>
    2export LANGSMITH_TRACING=true
    3export LANGSMITH_ENDPOINT=https://eu.api.smith.langchain.com # or https://api.smith.langchain.com for US
    4export LANGSMITH_PROJECT=<your-project-name>
  3. 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 os
2
3from kern.agent import Agent
4from kern.models.openai import OpenAIResponses
5from kern.tools.hackernews import HackerNewsTools
6from openinference.instrumentation.kern import AgnoInstrumentor
7from opentelemetry import trace as trace_api
8from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
9from opentelemetry.sdk.trace import TracerProvider
10from opentelemetry.sdk.trace.export import SimpleSpanProcessor
11
12# Set the endpoint and headers for LangSmith
13endpoint = "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}
18
19# Configure the tracer provider
20tracer_provider = TracerProvider()
21tracer_provider.add_span_processor(
22 SimpleSpanProcessor(OTLPSpanExporter(endpoint=endpoint, headers=headers))
23)
24trace_api.set_tracer_provider(tracer_provider=tracer_provider)
25
26# Start instrumenting kern
27AgnoInstrumentor().instrument()
28
29# Create and configure the agent
30agent = Agent(
31 name="Stock Market Agent",
32 model=OpenAIResponses(id="gpt-5.2"),
33 tools=[HackerNewsTools()],
34 markdown=True,
35 debug_mode=True,
36)
37
38# Use the agent
39agent.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_ENDPOINT based on your data region.

By following these steps, you can effectively integrate Kern with LangSmith, enabling comprehensive observability and monitoring of your AI agents.