await session.say(message) How do I prevent this m...
# ask-ai
l
await session.say(message) How do I prevent this message from getting added to the context?
t
Thanks for reaching out, Computer. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to send a message using
session.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:
Copy code
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 Docs
l
How do I manually add a message to chat context
t
If you want to manually add a message to the chat context in LiveKit, you can easily do that using the
add_message
method from the
ChatContext
class. Here’s a quick example to get you started:
Copy code
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:
Copy code
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.py
l
Will that message get added to the session history:?
t
Just a quick heads-up: when you add a message to the chat context using
add_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 Docs
l
Could you give python code for that?
t
Here’s a friendly Python code example showing how you can keep manually added messages saved in the session history using `update_chat_ctx`:
Copy code
from 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!