How to set up Langfuse alerts
Quick answer
Use the
Langfuse Python SDK to configure alerts by creating an alert rule with conditions and notification channels. Initialize the Langfuse client with your API keys, define alert criteria, and register the alert to receive notifications on model usage or errors.PREREQUISITES
Python 3.8+Langfuse API keys (public and secret)pip install langfuse
Setup
Install the langfuse Python package and set your environment variables for API keys.
- Install Langfuse SDK:
pip install langfuse - Set environment variables
LANGFUSE_PUBLIC_KEYandLANGFUSE_SECRET_KEYwith your Langfuse credentials.
pip install langfuse Step by step
This example shows how to initialize the Langfuse client, create an alert rule for monitoring error rates, and register it to receive email notifications.
import os
from langfuse import Langfuse
# Initialize Langfuse client with your API keys
langfuse = Langfuse(
public_key=os.environ["LANGFUSE_PUBLIC_KEY"],
secret_key=os.environ["LANGFUSE_SECRET_KEY"],
host="https://cloud.langfuse.com"
)
# Define an alert rule for error rate exceeding 5%
alert_rule = {
"name": "High error rate alert",
"description": "Alert when error rate exceeds 5%",
"conditions": [
{
"metric": "error_rate",
"operator": ">",
"threshold": 0.05
}
],
"notifications": [
{
"type": "email",
"recipients": ["alerts@example.com"]
}
]
}
# Create the alert in Langfuse
created_alert = langfuse.alerts.create(alert_rule)
print(f"Alert created with ID: {created_alert.id}") output
Alert created with ID: 1234567890abcdef
Common variations
You can customize alerts for different metrics like latency, usage volume, or custom tags. Langfuse supports multiple notification channels such as Slack, webhook, or SMS. Use async calls if integrating in async applications.
import asyncio
async def create_alert_async():
alert_rule = {
"name": "High latency alert",
"conditions": [{"metric": "latency_ms", "operator": ">", "threshold": 1000}],
"notifications": [{"type": "slack", "channel": "#alerts"}]
}
created_alert = await langfuse.alerts.acreate(alert_rule)
print(f"Async alert created with ID: {created_alert.id}")
asyncio.run(create_alert_async()) output
Async alert created with ID: abcdef1234567890
Troubleshooting
- If you get authentication errors, verify your
LANGFUSE_PUBLIC_KEYandLANGFUSE_SECRET_KEYenvironment variables are set correctly. - If alerts do not trigger, check your alert conditions and ensure metrics are being reported to Langfuse.
- For notification failures, verify your email or Slack webhook configurations.
Key Takeaways
- Initialize the
Langfuseclient with your public and secret API keys from environment variables. - Create alert rules specifying metrics, thresholds, and notification channels using the Langfuse SDK.
- Use async methods for integration in asynchronous Python applications.
- Verify environment variables and notification settings if alerts do not work as expected.