crooked-dawn-90821
09/24/2025, 7:04 PMtall-airline-16756
09/24/2025, 7:04 PMtall-airline-16756
09/24/2025, 7:04 PMcrooked-dawn-90821
09/24/2025, 7:08 PMtall-airline-16756
09/24/2025, 7:09 PMfrom livekit.agents import llm
initial_ctx = llm.ChatContext().append(
role="system",
text="You are a helpful assistant."
)
You can pass this initial_ctx to the agent when you create it. This sets the tone right from the start!
2. Update the System Message Dynamically: from livekit.agents import llm
def update_instructions(chat_ctx: llm.ChatContext, instructions: str):
idx = chat_ctx.index_by_id(INSTRUCTIONS_MESSAGE_ID)
if idx is not None:
chat_ctx.items[idx] = llm.ChatMessage(
id=INSTRUCTIONS_MESSAGE_ID,
role="system",
content=[instructions],
created_at=chat_ctx.items[idx].created_at,
)
else:
chat_ctx.items.insert(0, llm.ChatMessage(
id=INSTRUCTIONS_MESSAGE_ID,
role="system",
content=[instructions]
))
This way, you can easily tweak the system message while your app is running. Pretty handy, right?
3. Insert System Messages for Specific Use Cases: For instance, if you're working on content moderation: moderation_ctx = llm.ChatContext([
llm.ChatMessage(
role="system",
content=["You are a content moderator. Respond ONLY with 'APPROPRIATE' or 'INAPPROPRIATE'."]
),
llm.ChatMessage(role="user", content=[f"Evaluate: {text}"])
])
This helps guide the LLM to respond exactly how you want.
If you have any questions or want me to walk you through any part of this, just let me know—I'm here to help!
Sources: Baseten LLM integration guide | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | pipeline-llm/llm_powered_content_filter.py | examples/voice_agents/llamaindex-rag/retrieval.py | agent_extensions/agent_extensions/rag/handler.pycrooked-dawn-90821
09/24/2025, 7:15 PMtall-airline-16756
09/24/2025, 7:16 PMChatContext of a LiveKit agent while it’s running, here are some friendly methods you can try:
1. Access the Full Conversation History: You can use the session.history property to get the entire conversation history like this:
# Python Example
chat_ctx = agent_session.history This gives you the global ChatContext instance, which updates in real-time as new conversation items come in.
2. Read-Only Access to Current Context: If you want to peek at the current chat context without changing it, try the agent.chat_ctx property:
# Python Example
current_ctx = agent.chat_ctx This is especially handy when you’re working within agent lifecycle methods like on_user_turn_completed.
3. Accessing from Within an Agent: When you’re writing your own custom logic, you can access the session directly, like so:
# Python Example
async def on_user_turn_completed(self, turn_ctx, new_message):
full_history = self.session.history
# Use history for decision making
If you’d like me to walk you through any of these steps or provide more examples, just let me know—I’m here to help!
Sources: Workflows | LiveKit Docs | agents/src/llm/remote_chat_context.test.ts | tests/test_agent_session.py