brief-lock-33347
12/25/2025, 9:01 AMtall-airline-16756
12/25/2025, 9:01 AMtall-airline-16756
12/25/2025, 9:01 AMbrief-lock-33347
12/25/2025, 9:03 AMtall-airline-16756
12/25/2025, 9:03 AMChatContext and persist it with update_chat_ctx.
# inside your agent class
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage) -> None:
# 1) Retrieve or build additional context (RAG result, DB lookup, etc.)
rag_content = await my_rag_lookup(new_message.text_content)
# 2) Add that content to the chat context so it becomes part of history
turn_ctx.add_message(
role="assistant",
content=f"Additional context: {rag_content}"
)
# 3) Persist the updated ChatContext so future LLM generations see it
await self.update_chat_ctx(turn_ctx)
Notes:
• Place this in your agent handler (e.g., inside on_user_turn_completed) after you compute any extra context.
• turn_ctx.add_message(...) can also use other roles and structured content if needed.
• update_chat_ctx ensures the added message is stored for subsequent turns.
Want me to adapt this to include metadata, multiple messages, or a specific RAG pattern?brief-lock-33347
12/25/2025, 9:07 AMtall-airline-16756
12/25/2025, 9:07 AMupdate_chat_ctx to save them. Example for a Python agent:
class MyAgent(Agent):
async def on_init(self) -> None:
# create a ChatContext (or load an existing one)
initial_ctx = ChatContext()
# add one or more messages you want persisted
initial_ctx.add_message(role="system", content="You are a helpful assistant for Acme Corp.")
initial_ctx.add_message(role="assistant", content="Here is some product knowledge: ...")
# persist the chat context so future turns include this context
await self.update_chat_ctx(initial_ctx)
Notes:
• Use role="system", role="assistant", or other roles as appropriate.
• You can load RAG results or DB content here before calling update_chat_ctx.
• If your agent framework provides a pre-existing ChatContext on init, modify that instance instead of creating a new one.brief-lock-33347
12/25/2025, 9:09 AMtall-airline-16756
12/25/2025, 9:09 AMChatContext from livekit.agents.llm.
from livekit.agents.llm import ChatContext
Would you like a brief example showing on_init using that import?
Sources: External data and RAG | LiveKit docs