I'm trying to setup langfuse tracer for my livekit...
# ask-ai
c
I'm trying to setup langfuse tracer for my livekit agent but the langfuse doesn't capture input and output of the llm or STT or TTS. How do I make the tracer capture input and output at these stages?
t
Thanks for reaching out, Bharath. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I hear that Langfuse is not capturing the LLM, STT, and TTS input/output for your LiveKit agent. You can make Langfuse capture those payloads by explicitly instrumenting each stage in your agent and sending the data to the OpenTelemetry tracer provider bound to Langfuse. Below are the exact, minimal steps to get that working. 1) Ensure tracer provider is set to Langfuse • Call LiveKit’s helper to set the global tracer provider with your Langfuse keys and endpoint:
Copy code
from livekit.agents.metrics import set_tracer_provider

set_tracer_provider("your-langfuse-public-key", "your-langfuse-secret-key", "<https://cloud.langfuse.com>")
This configures OpenTelemetry to send spans to Langfuse. It does not automatically capture payloads. 2) Instrument each pipeline node to log input/output • Override the nodes in your Agent:
stt_node
,
llm_node
, and
tts_node
. Log inputs before processing and outputs after (or as chunks stream). Example for LLM:
Copy code
from langfuse import current_run  # or the Langfuse SDK object you use
from opentelemetry import trace

tracer = trace.get_tracer("livekit-agent")

async def llm_node(self, chat_ctx, tools, model_settings=None):
    current_run.log_input({"chat_context": [m.dict() for m in chat_ctx.messages]})
    with tracer.start_as_current_span("LLM_Inference") as span:
        span.set_attribute("stage", "llm")
        result_chunks = []
        async for chunk in Agent.default.llm_node(self, chat_ctx, tools, model_settings):
            # log each chunk as output
            current_run.log_output({"content": getattr(chunk, "delta", getattr(chunk, "text", str(chunk)))})
            span.add_event("llm_chunk", {"chunk": str(chunk)})
            result_chunks.append(chunk)
            yield chunk
        span.set_attribute("llm_output", "".join([getattr(c, "delta", {}).get("content","") if hasattr(c, "delta") else str(c) for c in result_chunks]))
• Use the same pattern for
stt_node
(log raw audio metadata and transcript text) and
tts_node
(log input text and synthesized result or audio metadata). 3) Use spans and events for structured traces • Wrap each stage in an OpenTelemetry span so Langfuse shows timing and attributes:
Copy code
with tracer.start_as_current_span("STT") as span:
    span.set_attribute("stage", "stt")
    span.set_attribute("input_audio_length", audio_length)
    # process and log transcript via current_run.log_output(...)
4) (Optional) Add global metadata via a span processor • If you want
user_id
or
session_id
on every span, register a custom span processor that sets those attributes on span start. This will surface the metadata in Langfuse UI. What I need from you if it still doesn’t work • Which SDK/language are you using for the agent (Python or other)? • Do you already see spans in Langfuse but no payload fields, or do you see nothing at all? • Example snippet of your current `llm_node`/`stt_node`/`tts_node` (sanitized) if you want me to point out where to add logs. I can give a tailored code snippet for your language and where to insert the log calls once you tell me the SDK and whether you see spans in Langfuse today. Sources: Setting custom user_id and session_id in Langfuse traces | Pipeline nodes and hooks | LiveKit docs | Agents (Python) | livekit/livekit_composite | DeepWiki