How to easy · 3 min read

How to set up Langfuse tracing

Quick answer
Use the langfuse Python SDK to initialize tracing by creating a Langfuse client with your public_key and secret_key. Decorate your AI call functions with @observe() to automatically capture telemetry and send it to Langfuse.

PREREQUISITES

  • Python 3.8+
  • Langfuse account with <code>public_key</code> and <code>secret_key</code>
  • pip install langfuse

Setup

Install the langfuse Python package and set your Langfuse API keys as environment variables for secure authentication.

bash
pip install langfuse

Step by step

Initialize the Langfuse client with your keys, then use the @observe() decorator on your AI function to enable automatic tracing.

python
import os
from langfuse import Langfuse
from langfuse.decorators import observe

# Initialize Langfuse client
langfuse = Langfuse(
    public_key=os.environ["LANGFUSE_PUBLIC_KEY"],
    secret_key=os.environ["LANGFUSE_SECRET_KEY"],
    host="https://cloud.langfuse.com"
)

@observe()
def call_ai_model(prompt: str) -> str:
    # Simulate AI call (replace with actual API call)
    response = f"Response to: {prompt}"
    return response

if __name__ == "__main__":
    result = call_ai_model("Hello, Langfuse tracing!")
    print(result)
output
Response to: Hello, Langfuse tracing!

Common variations

  • Use @observe() on async functions for asynchronous AI calls.
  • Integrate Langfuse with OpenAI or other AI SDKs by wrapping their calls inside @observe() decorated functions.
  • Configure a custom host if using a self-hosted Langfuse instance.
python
import asyncio
from langfuse import Langfuse
from langfuse.decorators import observe

langfuse = Langfuse(
    public_key=os.environ["LANGFUSE_PUBLIC_KEY"],
    secret_key=os.environ["LANGFUSE_SECRET_KEY"]
)

@observe()
async def async_ai_call(prompt: str) -> str:
    # Simulate async AI call
    await asyncio.sleep(0.1)
    return f"Async response to: {prompt}"

async def main():
    response = await async_ai_call("Async Langfuse tracing")
    print(response)

if __name__ == "__main__":
    asyncio.run(main())
output
Async response to: Async Langfuse tracing

Troubleshooting

  • If you see no traces in the Langfuse dashboard, verify your public_key and secret_key environment variables are set correctly.
  • Ensure network connectivity to https://cloud.langfuse.com or your custom host.
  • Check for exceptions in your AI call functions; unhandled errors may prevent trace submission.

Key Takeaways

  • Initialize Langfuse with your public and secret keys from environment variables.
  • Use the @observe() decorator to automatically trace AI API calls.
  • Support both synchronous and asynchronous AI functions with Langfuse tracing.
  • Verify keys and network access if traces do not appear in the dashboard.
Verified 2026-04
Verify ↗