miniature-nail-35141
10/04/2025, 1:37 PMtall-airline-16756
10/04/2025, 1:37 PMtall-airline-16756
10/04/2025, 1:38 PMminiature-nail-35141
10/04/2025, 1:41 PMtall-airline-16756
10/04/2025, 1:42 PMon_user_turn_completed hook from the VoiceAgent class. This method is called right after the user finishes speaking and just before the LLM generates a response, making it a perfect spot to check and log the chat context.
Here’s a friendly example of how you might override this method to print out the chat context:
async def on_user_turn_completed(
self, turn_ctx: llm.ChatContext, new_message: llm.ChatMessage
):
# Let's print the entire chat context so you can see what's going on
for message in turn_ctx.items:
print(f"{message.role}: {message.text_content}")
# Or, if you prefer, convert it to a dictionary for easier logging
print("Chat context updated:", turn_ctx.to_dict())
If you’re working with a custom LLM node, like in a RAG pipeline, you can also add logging inside the llm_node method when you update the chat context. For instance, when you inject retrieved context, logging the updated instructions can be really helpful:
print(f"update instructions: {instructions[:100].replace('\n', '\n')}...")
And just a quick tip: for more structured logging in a production environment, using Python’s built-in logging module to output JSON-formatted logs can make things much clearer. You can even add custom fields like room name or session ID to keep everything organized.
Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Job lifecycle | LiveKit Docs