Langfuse environment variables setup
Quick answer
Set the
LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY environment variables to authenticate the Langfuse Python SDK. These keys enable secure tracing and observability of your AI API calls when using the Langfuse client.PREREQUISITES
Python 3.8+Langfuse API keys (public and secret)pip install langfuse
Setup
Install the Langfuse Python SDK and set the required environment variables for authentication. The SDK requires both LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY to function properly.
pip install langfuse Step by step
Here is a complete example demonstrating how to set environment variables in your shell and initialize the Langfuse client in Python.
# In your shell (Linux/macOS)
export LANGFUSE_PUBLIC_KEY="your_public_key_here"
export LANGFUSE_SECRET_KEY="your_secret_key_here"
# In Python script
import os
from langfuse import Langfuse
langfuse = Langfuse(
public_key=os.environ["LANGFUSE_PUBLIC_KEY"],
secret_key=os.environ["LANGFUSE_SECRET_KEY"],
host="https://cloud.langfuse.com"
)
print("Langfuse client initialized successfully") output
Langfuse client initialized successfully
Common variations
You can also set environment variables in Windows PowerShell or directly in your Python code (not recommended for production). Additionally, Langfuse supports custom hosts if you run a self-hosted instance.
# Windows PowerShell
$env:LANGFUSE_PUBLIC_KEY = "your_public_key_here"
$env:LANGFUSE_SECRET_KEY = "your_secret_key_here"
# Python direct assignment (for testing only)
import os
os.environ["LANGFUSE_PUBLIC_KEY"] = "your_public_key_here"
os.environ["LANGFUSE_SECRET_KEY"] = "your_secret_key_here" Troubleshooting
- If you see authentication errors, verify that both
LANGFUSE_PUBLIC_KEYandLANGFUSE_SECRET_KEYare correctly set and accessible in your environment. - Ensure no extra whitespace or quotes are included when exporting environment variables.
- Check network connectivity to
https://cloud.langfuse.comor your custom host.
Key Takeaways
- Always set both
LANGFUSE_PUBLIC_KEYandLANGFUSE_SECRET_KEYenvironment variables for Langfuse SDK authentication. - Use
os.environin Python to access these keys securely without hardcoding. - For production, avoid setting keys directly in code; use environment variables or secret management.
- Langfuse supports custom hosts via the
hostparameter during client initialization. - Verify environment variables carefully to prevent authentication or connectivity issues.