How to print the llm input and llm output for each...
# ask-ai
l
How to print the llm input and llm output for each conversation in logs.
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to print the LLM input and LLM output for each conversation in logs. Below are concise, actionable ways to do that in LiveKit — pick one or combine them depending on how much detail you want. 1) Log each chat message (simple, covers both sides) • Use the conversation_item_added event to print every message as it's added.
Copy code
@session.on("conversation_item_added")
def on_conversation_item_added(event):
    item = event.item
    if item.role == "user":
        print(f"[LLM Input] User: {item.text_content}")
    elif item.role == "assistant":
        print(f"[LLM Output] Agent: {item.text_content}")
2) Log the full input context sent to the LLM (when user turn completes) • Use on_user_turn_completed to capture the whole chat context before generation.
Copy code
class LoggingAgent(Agent):
    async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
        for msg in turn_ctx.messages:
            if msg.role == "system":
                print(f"[System Prompt] {msg.text_content}")
            elif msg.role == "user":
                print(f"[LLM Input] User: {msg.text_content}")
            elif msg.role == "assistant":
                print(f"[Context Assistant] {msg.text_content}")
3) Capture streaming LLM output (fine-grained output chunks) • Override
llm_node
to intercept streamed chunks from the model.
Copy code
class LoggingAgent(Agent):
    async def llm_node(self, chat_ctx: ChatContext, tools: list[FunctionTool], model_settings: ModelSettings) -> AsyncIterable:
        async for chunk in Agent.default.llm_node(self, chat_ctx, tools, model_settings):
            if hasattr(chunk, "delta") and hasattr(chunk.delta, "content"):
                print(f"[LLM Output Chunk] {chunk.delta.content}")
            yield chunk
4) Optional: correlate with metrics • Use metrics_collected to print timing/tokens alongside conversation logs for debugging:
Copy code
@session.on("metrics_collected")
def on_metrics_collected(metrics_event):
    print(f"[LLM Metrics] {metrics_event.metrics}")
Recommendations • For full coverage: combine conversation_item_added (simple message-level logs) with llm_node (streamed output) or on_user_turn_completed (complete input context). • In production, use structured logging (JSON) and include conversation IDs/timestamps to correlate messages and metrics. If you want, tell me which language/framework you’re using (Python vs. another SDK) and whether you need example JSON logging or correlation with conversation IDs — I’ll provide the exact snippet. Sources: Events and error handling | LiveKit docs | Pipeline nodes and hooks | LiveKit docs