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

Installation and setup

The Langfuse Python SDK installs with pip. Without keys it switches itself off and your code still runs; with keys it sends traces to Langfuse Cloud.

pip install langfuse 4.15.4

Three packages: the SDK, the OpenAI client that model calls go through, and pytest.

pip install "langfuse==4.15.4" openai pytest
Exampleversions.py
from importlib.metadata import version

print("langfuse", version("langfuse"))
print("opentelemetry-sdk", version("opentelemetry-sdk"))
Example
python versions.py

The SDK brings the OpenTelemetry packages with it. Langfuse builds its tracing on them, which is why a Langfuse trace can also hold steps recorded by other OpenTelemetry tools.

What the SDK does without keys

Exampleno_keys.py
from langfuse import observe


@observe()
def answer(ticket):
    return "Order A17 shipped on 3 March."


print(answer("Where is my order A17?"))
Example
python no_keys.py

@observe asks the SDK to record the function; lesson 3 explains it. With no LANGFUSE_PUBLIC_KEY set, the client logs an authentication warning and disables itself, and the function still returns its answer. The SDK is designed so that a tracing problem never breaks the application it traces.

With keys set, the default destination is https://cloud.langfuse.com, Langfuse Cloud's EU region. Lesson 2 shows that happening, and points the SDK at a server on your own machine instead.

Langfuse Cloud keys and a free modelOptional

No lesson needs these. To see traces in Langfuse's own interface, create a free Langfuse Cloud project, copy its public and secret keys from the project settings, and set three variables. For a hosted model instead of the local one, any provider with an OpenAI-compatible endpoint works with lesson 7's code; these have a free tier.

export LANGFUSE_PUBLIC_KEY="pk-lf-..."
export LANGFUSE_SECRET_KEY="sk-lf-..."
export LANGFUSE_BASE_URL="https://cloud.langfuse.com"
export GROQ_API_KEY="gsk_..."
ProviderKeyOpenAI-compatible base_urlModel
GroqFree, console.groq.com/keyshttps://api.groq.com/openai/v1openai/gpt-oss-120b
GeminiFree, aistudio.google.com/apikeyhttps://generativelanguage.googleapis.com/v1beta/openai/gemini-2.5-flash
OpenRouterFree models end in :free, openrouter.ai/keyshttps://openrouter.ai/api/v1google/gemma-4-31b-it:free

Other Langfuse Cloud regions have their own address, such as https://us.cloud.langfuse.com; a self-hosted server has whatever address you gave it.

Try it yourself
  • Run pip show langfuse and find opentelemetry-sdk in its Requires line.
  • Set LANGFUSE_PUBLIC_KEY to any value, leave the secret key unset, and run no_keys.py again. Which key does the warning name now?

Little by little, you're building something great.