Trace LLM Apps with Langfuse in Python (2026) — see prompts, completions, latency, and cost without rewriting your agent loop.
After you route models with LiteLLM, extract with Instructor, or optimize with DSPy, you still need observability. Langfuse is the tracing layer.
TL;DR
- Set
LANGFUSE_PUBLIC_KEY,LANGFUSE_SECRET_KEY, and base URL via env - Drop-in:
from langfuse.openai import openai - Wrap the parent call with
@observe()to nest generations in one trace - Capture tokens/cost/latency automatically on instrumented OpenAI calls
Install
pip install langfuse openai
# or: uv add langfuse openai
export LANGFUSE_PUBLIC_KEY=pk-...
export LANGFUSE_SECRET_KEY=sk-...
export LANGFUSE_BASE_URL=https://cloud.langfuse.com # or your self-host URL
export OPENAI_API_KEY=sk-...
Example 1 — drop-in OpenAI client
from langfuse.openai import openai
resp = openai.chat.completions.create(
model="gpt-4o-mini",
name="hello-trace",
messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(resp.choices[0].message.content)
Example 2 — nest calls under @observe
from langfuse import observe
from langfuse.openai import openai
@observe()
def capital_poem(country: str) -> str:
capital = openai.chat.completions.create(
model="gpt-4o-mini",
name="get-capital",
messages=[
{"role": "system", "content": "Reply with only the capital city."},
{"role": "user", "content": country},
],
).choices[0].message.content
poem = openai.chat.completions.create(
model="gpt-4o-mini",
name="generate-poem",
messages=[
{"role": "system", "content": "Write a two-line poem about the city."},
{"role": "user", "content": capital},
],
).choices[0].message.content
return poem
print(capital_poem("Japan"))
Both OpenAI calls land under one parent trace, so debugging multi-step LLM flows stays readable.
Production tips
- Never commit keys; use env / secret manager
- Name generations (
name=...) so traces are searchable - Disable huge IO capture when payloads are large
- Self-host when data residency matters; point
LANGFUSE_BASE_URLat your instance
Wrap-up
In 2026, shipping LLM features without traces is flying blind. Langfuse’s OpenAI drop-in plus @observe gets you prompts, outputs, and cost visibility with almost no app rewrite.