how can <prompt linking via langfuse> be implement...
# ask-ai
l
how can prompt linking via langfuse be implemented with livekit agents when using an
llm_node
function in STT->LLM->TTS pipeline? I tried setting
langfuse.prompt.name
and
langfuse.prompt.version
attributes on the
llm_node
span but that does not enable langfuse to link traces to specific prompts in the UI. I believe the prompt name and prompt version attributes need to the set on the
llm_request
generation (defined at https://github.com/livekit/livekit_composite/blob/cd1a2500/livekit/agents/livekit-agents/livekit/agents/llm/llm.py), is there a nice way of doing this?
t
Thanks for reaching out, vlad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you implement prompt linking via Langfuse with LiveKit agents in an STT->LLM->TTS pipeline, here’s a friendly step-by-step guide: 1. Enable Langfuse Tracing: Start by using
setup_langfuse()
to initialize Langfuse with your credentials and set the tracer provider before kicking off the agent session. Don’t forget to have your environment variables like
LANGFUSE_PUBLIC_KEY
and
LANGFUSE_SECRET_KEY
properly configured. 2. Customize `llm_node`: You’ll want to override the
llm_node
method in your custom agent class. This lets you inject trace identifiers into the chat context and enrich prompts with metadata, making sure everything is traceable throughout the pipeline. 3. Use Lifecycle Hooks: Take advantage of hooks like
on_enter()
and
on_user_turn_completed()
to capture and tweak prompts or user input at different points during the interaction. It’s a great way to stay in control of the flow. 4. Ensure Trace Propagation: Keep the trace going smoothly by attaching metadata (like session ID) to spans. If needed, you can also override other pipeline nodes such as
stt_node
or
tts_node
to maintain consistency. 5. Validate in Langfuse: Finally, keep an eye on the Langfuse dashboard to confirm that traces are linking prompts, LLM outputs, and other pipeline events correctly. It’s reassuring to see everything connected as expected. Following these steps should help you achieve prompt linking by combining OpenTelemetry tracing with your custom
llm_node
logic. If you’d like, I’m here to provide more details or examples—just let me know! Sources: examples/voice_agents/langfuse_trace.py | Tool definition and use | LiveKit docs | Pipeline nodes and hooks | LiveKit docs
l
how should the
llm_node
be customized? Give an explicit example of this would be achieved
t
I'd be happy to help you customize the
llm_node
for prompt linking or traceability! You can easily override it in your agent implementation. Here's a clear example to guide you:
Copy code
from livekit.agents import llm, Agent

class CustomAgent(Agent):
    def llm_node(
        self,
        chat_ctx: llm.ChatContext,
        tools: list[llm.FunctionTool],
        model_settings: ModelSettings,
    ) -> AsyncIterable[llm.ChatChunk | str]:
        # Example: Append traceable context or prompt link
        chat_ctx.append(
            text="TRACE_ID: 12345 - Prompt initiated from user query",
            role="system"
        )

        # Delegate to default behavior (or call your custom LLM logic)
        return Agent.default.llm_node(self, chat_ctx, tools, model_settings)
Here's a quick breakdown: • Traceable Context: We add a trace ID to the chat context to help link prompts with downstream actions, making things easier to track. • Custom Logic: Feel free to replace the default LLM logic with your own if that suits your needs better. • ChatChunk Metadata: Use
ChatChunk
objects to capture text, tool calls, or metadata during inference — this really helps with detailed traceability. If you have any questions or want me to walk you through more examples, just let me know! I’m here to help. Sources: Using Custom LLM Providers with the Agent | Agents v0.x migration guide - Node.js | LiveKit Docs | Pipeline nodes and hooks | LiveKit docs