In livekit agent, I want to store the log in a man...
# ask-ai
e
In livekit agent, I want to store the log in a manner where after each turn I would want to see the complete context/system prompt. is there a way to do that?
t
Thanks for reaching out, Sandeep. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you want to store logs so that after each turn you can see the complete context (including the system prompt). Yes — you can do that. Here are the practical options and a minimal example for each so you can pick what fits your setup. Which part do you want to use: logging the context before the agent responds, after the agent responds, or saving snapshots to session storage for later inspection? Options • Log before the agent responds: use the on_user_turn_completed lifecycle hook to read
turn_ctx.messages
(includes system instructions) and log/store them. • Log after the agent responds: listen for conversation events (e.g.,
conversation_item_added
) and inspect
session.chat_ctx.messages
when an assistant message is added. • Persist snapshots: write a copy of the full
chat_ctx
messages into
session.userdata
(or your own storage) each turn so you can review complete context history later. • For tests: the
run
method returns a result with
session.chat_ctx.messages
you can examine. Minimal examples 1) Before agent replies (Python-style):
Copy code
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
    # Full context (system + history + new user message)
    snapshot = [{ "role": m.role, "content": m.content } for m in turn_ctx.messages]
    print("Context before reply:", snapshot)
    # Or store snapshot in persistent storage
2) After agent replies (session event):
Copy code
@session.on("conversation_item_added")
def on_conversation_item(item):
    if item.type == "message" and item.role == "assistant":
        # Full context after assistant response
        print("Context after reply:", session.chat_ctx.messages)
3) Save snapshot to session.userdata:
Copy code
self.session.userdata["last_full_context"] = [
  {"role": m.role, "content": m.content} for m in session.chat_ctx.messages
]
Notes •
ChatContext
/
session.chat_ctx.messages
contains system instructions, user messages, agent replies, and tool calls — so it gives the complete prompt context. • Use on_user_turn_completed to capture the prompt the LLM will see next; use conversation events to capture the state after the agent response. Tell me which option you prefer and whether you want a complete code snippet for Node.js or Python, and I’ll provide it. Sources: Agents (Node.js) | livekit/livekit_composite | DeepWiki | Testing and evaluation | LiveKit docs | Pipeline nodes and hooks | LiveKit docs