Langfuse Tracing for OpenAI Agents
Drop in Langfuse's OpenAI wrapper to capture prompts, completions, latency, and token usage for every agent LLM call — then nest tool steps under one trace.
Use Langfuse as the tracing backend for agents that already call the OpenAI Python SDK. The drop-in wrapper records each chat completion; wrap tool steps with start_as_current_observation so one user request becomes one nested trace. For dollar budgets and hard spend caps, see Agent Cost Monitoring with LiteLLM. For a tool-agnostic span checklist, see Agent Tracing and Evaluation Setup.
Requirements
- A Langfuse Cloud project (Hobby plan is free) or a self-hosted Langfuse instance
- Public and secret API keys from the Langfuse project settings
- Python with the OpenAI SDK (
openai) already in use
Install and credentials
pip install langfuse openai
export LANGFUSE_PUBLIC_KEY=pk-lf-...
export LANGFUSE_SECRET_KEY=sk-lf-...
export LANGFUSE_BASE_URL=https://cloud.langfuse.com # EU; use us/jp/hipaa URLs if your project is there
export OPENAI_API_KEY=sk-...Drop-in OpenAI tracing
from langfuse.openai import openai
from langfuse import get_client
completion = openai.chat.completions.create(
name="agent-chat",
model="gpt-6-luna", # re-check OpenAI's current model list before shipping
messages=[
{"role": "system", "content": "You are a support agent. Be concise."},
{"role": "user", "content": "Summarize ticket 4821 in one sentence."},
],
metadata={"agent": "support-bot", "ticket": "4821"},
)
print(completion.choices[0].message.content)
# Short-lived scripts must flush or traces can be lost
get_client().flush()Open the Langfuse UI → Traces after the run. You should see the generation with input, output, model, latency, and usage.
Nest tool calls under one agent trace
from langfuse import get_client, propagate_attributes
from langfuse.openai import openai
langfuse = get_client()
with langfuse.start_as_current_observation(
as_type="span",
name="support-ticket-agent",
input={"ticket_id": "4821"},
) as root:
with propagate_attributes(
user_id="end-user-99",
session_id="ticket-4821",
tags=["agent:support"],
metadata={"env": "staging"},
):
with langfuse.start_as_current_observation(
as_type="span", name="fetch-ticket"
) as tool_span:
ticket = {"id": "4821", "body": "..."} # your CRM/API call
tool_span.update(output={"chars": len(ticket["body"])})
with langfuse.start_as_current_observation(
as_type="generation",
name="draft-reply",
model="gpt-6-luna",
) as gen:
completion = openai.chat.completions.create(
model="gpt-6-luna",
messages=[
{"role": "system", "content": "Draft a short reply."},
{"role": "user", "content": ticket["body"]},
],
)
reply = completion.choices[0].message.content
gen.update(output=reply)
root.update(output={"reply_preview": (reply or "")[:120]})
langfuse.flush()Cost note
Langfuse Cloud Hobby includes 50k billable units/month (traces, observations, scores) with 30-day data access and 2 users. Paid plans start at Core $29/month. Self-hosting the open-source stack is free of Langfuse Cloud fees; you still pay your LLM provider. See Langfuse pricing. Do not treat Hobby limits as guaranteed forever — re-check the pricing page.
Sources (official docs, checked 2026-10-16)
- Get started with tracing
- Python SDK instrumentation
- SDK overview
- Langfuse pricing
- PyPI langfuse
- OpenAI GPT-6 Luna
Failure modes
- Short-lived scripts that exit without get_client().flush() (or shutdown) can drop buffered traces.
- Logging full prompts stores PII and secrets in Langfuse; redact or mask before export, and restrict who can view the project.
- Wrong LANGFUSE_BASE_URL (EU vs US vs JP vs HIPAA) sends credentials to the wrong region and auth fails or data lands in the wrong project.
- This is tracing and usage visibility, not a hard spend cap; pair with LiteLLM budgets (Agent Cost Monitoring) when you need enforcement.
Markdown version: /blueprints/langfuse-openai-tracing.md
Want a deeper build of this?
Vote for an extended, fully tested version (more tools, a full workflow file). Votes decide what we build next.
Related blueprints
AI Lead Follow-Up and Appointment Booking Agent (GoHighLevel)
Text every new lead within minutes, let a Conversation AI bot qualify them and book a call on your calendar, then hand off to a human, all inside GoHighLevel.
Agent Tracing and Evaluation Setup
Capture structured traces of every LLM and tool call and run a small regression eval suite on each change.
Evaluator-Optimizer Refinement Loop
One model generates, another grades against a rubric, and the loop repeats until the rubric passes or a limit is hit.
Get one fully built blueprint a week
A weekly email: one blueprint with config, cost, setup time and failure modes, plus a vote on what we build next.