how to log the speech generated by the agent. Is t...
# ask-ai
l
how to log the speech generated by the agent. Is there any hook?
t
Thanks for reaching out, Deepak. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want to keep track of the speech generated by the agent in LiveKit, you can do so by listening to a couple of helpful events: • agent_stopped_speaking: This fires when the agent finishes speaking. • user_speech_committed: This happens when the agent commits their speech to the conversation. Here’s a friendly example in Python to show you how to log the agent’s speech:
Copy code
@agent.on("agent_stopped_speaking")
def on_agent_stopped_speaking(msg: llm.ChatMessage):
    if isinstance(msg.content, list):
        msg.content = "\n".join(
            "[image]" if isinstance(x, llm.ChatImage) else x for x in msg
        )
    log_queue.put_nowait(f"[{datetime.now()}] AGENT:\n{msg.content}\n\n")
You can do the same for user speech by using the user_speech_committed event like this:
Copy code
@agent.on("user_speech_committed")
def on_user_speech_committed(msg: llm.ChatMessage):
    if isinstance(msg.content, list):
        msg.content = "\n".join(
            "[image]" if isinstance(x, llm.ChatImage) else x for x in msg
        )
    log_queue.put_nowait(f"[{datetime.now()}] USER:\n{msg.content}\n\n")
If you want to dive deeper, feel free to check out the LiveKit Documentation and take a look at this handy example code. Hope this helps!
l
agent_stopped_speaking event is not in livekit 1.0
t
In LiveKit 1.0, you’ve got some handy events to keep track of agent speech: • AGENT_STARTED_SPEAKING: This fires right when the agent starts talking. • AGENT_STOPPED_SPEAKING: This happens as soon as the agent stops speaking. • AGENT_SPEECH_COMMITTED: This one triggers when the agent’s speech is officially added to the chat context. • AGENT_SPEECH_INTERRUPTED: This fires if the agent’s speech gets interrupted. • speech_created: Emitted whenever new agent speech is created—super useful for logging speech generated by methods like
session.say
or
session.generate_reply
. • conversation_item_added: This event happens when a new agent speech item is added to the conversation history. Here’s a quick example showing how you can use these events:
Copy code
@agent.on("AGENT_STARTED_SPEAKING")
def on_agent_started_speaking():
    print("Agent started speaking.")

@agent.on("AGENT_STOPPED_SPEAKING")
def on_agent_stopped_speaking():
    print("Agent stopped speaking.")

@agent.on("speech_created")
def on_speech_created(event):
    print(f"New agent speech created. Source: {event.source}")
These events make it easy to log everything your agent says. If you want to dive deeper or have any questions, just give me a shout—I’m here to help! Sources: livekit-agents/livekit/agents/voice/events.py | VPAEvent | LiveKit Agents | Events and error handling | LiveKit Docs