How to beginner · 3 min read

How to set up alerts in LangSmith

Quick answer
To set up alerts in LangSmith, use the Client from the langsmith Python SDK to create alert rules based on trace data. Configure alert conditions and destinations such as email or webhooks to get notified when specific events occur.

PREREQUISITES

  • Python 3.8+
  • LangSmith API key
  • pip install langsmith

Setup

Install the langsmith Python package and set your API key as an environment variable.

  • Install LangSmith SDK: pip install langsmith
  • Set environment variable: export LANGSMITH_API_KEY=your_api_key (Linux/macOS) or set LANGSMITH_API_KEY=your_api_key (Windows)
bash
pip install langsmith

Step by step

This example demonstrates creating a simple alert in LangSmith that triggers when a trace contains an error. The alert sends a notification to a webhook URL.

python
import os
from langsmith import Client

# Initialize LangSmith client with API key from environment
client = Client(api_key=os.environ["LANGSMITH_API_KEY"])

# Define alert rule parameters
alert_name = "Error Trace Alert"

# Create an alert rule that triggers when any trace has an error
alert_rule = client.alerts.create(
    name=alert_name,
    description="Alert when a trace contains an error",
    filter_expression="trace.status == 'error'",
    destinations=[
        {
            "type": "webhook",
            "url": "https://your-webhook-url.example.com/notify"
        }
    ]
)

print(f"Created alert with ID: {alert_rule.id}")
output
Created alert with ID: alert_1234567890abcdef

Common variations

You can customize alerts in LangSmith by:

  • Using different filter_expression to monitor specific metrics or tags.
  • Adding multiple destinations such as email or Slack integrations.
  • Creating alerts asynchronously if your application requires it.

Example for email destination:

python
alert_rule = client.alerts.create(
    name="High Latency Alert",
    filter_expression="trace.metrics.latency > 1000",
    destinations=[
        {
            "type": "email",
            "email": "alerts@example.com"
        }
    ]
)
print(f"Created alert for high latency: {alert_rule.id}")
output
Created alert for high latency: alert_abcdef1234567890

Troubleshooting

  • If alerts do not trigger: Verify your filter_expression syntax matches your trace data fields.
  • If notifications fail: Check your webhook or email destination configuration and network accessibility.
  • Authentication errors: Ensure your LANGSMITH_API_KEY is valid and has alert permissions.

Key Takeaways

  • Use the langsmith.Client to create and manage alerts programmatically.
  • Configure filter_expression precisely to target relevant trace events.
  • Set up notification destinations like webhooks or email for real-time alerting.
Verified 2026-04
Verify ↗