LangfuseLangfuse Python SDK 4.15.4 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
38 small wins to finish your pathNext lesson

Using Langfuse Cloud and a hosted model

Two lines change to send traces to Langfuse Cloud and call a hosted model: the Langfuse client reads real keys, and the OpenAI client points at a provider.

Every lesson so far sent its traces to local_langfuse.py and asked shop_model.reply for answers. These two lines make the switch to the real services: run it, then open Langfuse and the trace is there.

Examplecloud.py
import os

from langfuse import get_client
from langfuse.openai import OpenAI

langfuse = get_client()  # LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY and LANGFUSE_BASE_URL
client = OpenAI(base_url="https://api.groq.com/openai/v1", api_key=os.environ["GROQ_API_KEY"])
print(langfuse.auth_check())

get_client() builds the client from the three environment variables, so no server is named in the code. The OpenAI line is the traced client with a provider's address and key; any provider from the setup table works, with its own model name. auth_check asks Langfuse whether the keys are valid. The documentation does not recommend it in production code; it makes a request and waits for the answer.

Examplecloud.py, continued
response = client.chat.completions.create(
    model="openai/gpt-oss-120b", name="write-reply",
    messages=[{"role": "user", "content": "Where is my order A17?"}],
)
print(response.choices[0].message.content)
langfuse.flush()

The call itself does not change. In Langfuse's interface, the trace list shows one trace with a generation holding the messages, the model and the token usage the provider reported, and Langfuse adds the cost if it has a price for that model. Everything from parts 3 to 6 applies unchanged: attributes, masking, prompts, scores and experiments.

Keeping the data on your own servers

The Langfuse server is open source. A self-hosted Langfuse runs on your own infrastructure, with Docker Compose on a single machine, Kubernetes with Helm, or the major clouds, and the SDK needs only its address as LANGFUSE_BASE_URL. Self-hosting is its own subject, with databases and upgrades to run, which is its own subject.

Whichever server you use, what you send was decided earlier: which observations, with which inputs, after which masking. Tests against the local server keep checking that, with no keys, whatever the production destination.

Try it yourself
  • Run cloud.py with your keys and open the trace in Langfuse.
  • Print langfuse.get_trace_url(trace_id=...) for a trace id you created with a seed.
  • Set LANGFUSE_BASE_URL to https://us.cloud.langfuse.com with EU keys and read what auth_check reports.

This is what real progress feels like.