lively-coat-10905
08/14/2025, 4:09 AMtall-airline-16756
08/14/2025, 4:09 AMtall-airline-16756
08/14/2025, 4:09 AMsession.say
but don’t want it to be added to the chat context, you can simply use the add_to_chat_ctx
parameter and set it to False
. Here’s a quick example:
await session.say(
"Hello world",
add_to_chat_ctx=False
)
Just so you know, add_to_chat_ctx
is True
by default, which means the message gets added to the chat context. Setting it to False
keeps the message out of the context. Hope that helps!
Sources: Agent speech and audio | LiveKit Docslively-coat-10905
08/14/2025, 5:26 AMtall-airline-16756
08/14/2025, 5:26 AMadd_message
method from the ChatContext
class. Here’s a quick example to get you started:
from livekit.agents import ChatContext
# Initialize the chat context
chat_ctx = ChatContext()
# Add a message with role and content
chat_ctx.add_message(
role="user",
content=["Hello, how can I assist you today?"]
)
This lets you specify who’s sending the message (like "user" or "assistant") and what the message says.
If you’re working with JavaScript, you can do something similar using the append
method:
const chatContext = new ChatContext();
// Create a message
const message = {
role: 'user',
text: 'Hello, how can I assist you today?'
};
// Append the message to the context
chatContext.append(message);
Feel free to reach out if you want a hand with anything else!
Sources: ChatContext | LiveKit Agents | Testing and evaluation | LiveKit Docs | examples/drive-thru/test_agent.pylively-coat-10905
08/14/2025, 5:30 AMtall-airline-16756
08/14/2025, 5:31 AMadd_message
or append
, it doesn’t automatically get saved in the session history. To make sure your message sticks around beyond the current turn, you’ll want to call update_chat_ctx
right after adding it. That way, your message is safely saved for later!
Sources: Pipeline nodes and hooks | LiveKit Docslively-coat-10905
08/14/2025, 5:31 AMtall-airline-16756
08/14/2025, 5:32 AMfrom livekit.agents import ChatContext
from livekit.agents import Agent
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
# Add a new message to the context
turn_ctx.add_message(
role="assistant",
content="This message will be persisted in the chat history."
)
# Persist the message by updating the chat context
await self.update_chat_ctx(turn_ctx)
Explanation:
• turn_ctx.add_message()
temporarily adds a message to the current context.
• await self.update_chat_ctx(turn_ctx)
makes sure the message is saved permanently in the session history.
If you have any questions or want me to walk you through it more, just let me know—I’m here to help!