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.

langfuseobservabilitytracingopenaiopentelemetry
Setup time15-45 minutes (estimate)
Estimated costLangfuse Cloud Hobby is free (50k units/month); Core starts at $29/month. Self-host is free of Cloud fees. You still pay your OpenAI (or other) token prices — see langfuse.com/pricing and your provider's pricing page.
VerificationLast verified 2026-10-16

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)

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.

– votes

Related blueprints

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.