little-king-41579
12/14/2025, 4:54 PMtall-airline-16756
12/14/2025, 4:54 PMtall-airline-16756
12/14/2025, 4:54 PM@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.
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.
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:
@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