How do I send a system message to a realtime AI?
# ask-ai
l
How do I send a system message to a realtime AI?
t
Thanks for reaching out, Tom. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring how to send a system message to a realtime AI in LiveKit! You can achieve this smoothly by using the OpenAI Realtime API plugin. The key is to create a
ConversationItemCreateEvent
with a
SystemItem
that carries your system message, which gently guides the AI's behavior and responses. Here’s a simple way to do it: 1. Define the System Message: Start by creating a
SystemItem
with this structure:
Copy code
{
  "type": "message",
  "role": "system",
  "content": [
    {
      "type": "input_text",
      "text": "Your system message here."
    }
  ]
}
2. Create the Event: Next, use the
conversation.item.create
event to add your system message into the conversation context:
Copy code
{
  "type": "conversation.item.create",
  "item": {
    "type": "message",
    "role": "system",
    "content": [
      {
        "type": "input_text",
        "text": "You are a helpful assistant."
      }
    ]
  }
}
3. Send the Event: If you’re working with JavaScript, you can send the event through the real-time model’s websocket connection like this:
Copy code
await model.sendEvent({
  type: 'conversation.item.create',
  item: {
    type: 'message',
    role: 'system',
    content: [{ type: 'input_text', text: 'Adjust your tone to be more formal.' }],
  },
});
And if Python is your language of choice, the
livekit.plugins.openai.realtime.utils.livekit_item_to_openai_item
function is a handy tool to help you create a compatible system message. If you have any questions or want me to walk you through more examples, just let me know—I’m here to help! Sources: plugins/openai/src/realtime/realtime_model.ts | LLM Integration & Real-time Models | livekit/livekit_composite | DeepWiki | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | OpenAI Realtime API plugin guide | LiveKit Docs
l
I’m in a python backend, where our livekit agent is running. Is there a standard way to do this that works for all AI models?
t
In a Python backend, LiveKit offers a wonderful way to send system messages consistently across all AI models by using the
llm.ChatContext
class. This helps keep things smooth and uniform whether you’re working with OpenAI, LangChain, or AWS. Here’s a simple guide to get you started: 1. Create a Chat Context: Begin by using the
llm.ChatContext
class to set up your system messages:
Copy code
from livekit.agents import llm

chat_ctx = llm.ChatContext()
chat_ctx.add_message(role="system", content="You are a helpful AI assistant.")
2. Multiple System Messages: You can layer your instructions by adding several system messages:
Copy code
chat_ctx.add_message(role="system", content="Always respond in a professional tone.")
chat_ctx.add_message(role="system", content="Focus on providing accurate technical information.")
3. Provider-Agnostic Handling: The great part is that LiveKit automatically adapts these system messages to fit the right format for whichever AI model you’re using, whether it’s OpenAI, AWS, or LangChain. If you’d like, I’m here to help with more details or examples tailored to your specific integration—just let me know! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | LangChain integration guide | LiveKit Docs
l
I’ve done this: self.session.history.add_message( role=“system”, content=message ) and there were no errors, but the AI didnt seem to know about the message I added to the history.
t
Hi there! I understand that adding a system message to the session history might not always change the AI's behavior if it’s not correctly integrated into the model’s context. Let me walk you through some steps to help make sure your system message gets recognized: 1. Use `update_instructions()`: Instead of just adding the message directly to the history, try using the
update_instructions()
method to set persistent system-level instructions:
await activity.update_instructions("You are a helpful AI assistant.")
2. Check Chat Template Compatibility: It’s a good idea to confirm that the AI model’s chat template supports system messages. Some templates might not recognize system roles unless they’re explicitly supported. 3. Use
update_chat_ctx()
for Initial Context:
If you want to set system messages right at the start of a session, you can use
update_chat_ctx()
to inject that initial context:
Copy code
initial_ctx = llm.ChatContext()
initial_ctx.add(llm.ChatMessage(role="system", text="You are a helpful assistant."))
await model.update_chat_ctx(initial_ctx)
4. Verify Model-Specific Behavior: Keep in mind that different models handle system messages in their own ways. For example, the OpenAI Realtime API maps system messages to
realtime.ConversationItemSystemMessage
, while others might need specific formatting. If you’d like, I’m here to help you implement these changes or troubleshoot any issues further. Just let me know! Sources: client/lib/chat/actions.tsx | livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | Pipeline nodes and hooks | LiveKit Docs | Agent Session & Activity Management | livekit/livekit_composite | DeepWiki | tests/test-chat-template.cpp
l
I dont want to update the instructions - I want to add a message from the system or developer so that the AI can be made aware of things that happen in the conversation that ARENT direct messages from the user. I’m doing this from a python object that extends LiveKit’s Agent class, and so has access to self.session
t
I'd love to help you dynamically add system or developer messages to a LiveKit Agent session in Python! You can easily do this using the
update_chat_ctx()
method, which lets you insert messages into the chat history for the AI to consider during your conversation. Here’s a simple way to get started: 1. Copy the Current Chat Context: It’s a good idea to copy the existing chat context first so you won’t accidentally overwrite any other messages.
chat_ctx = self.chat_ctx.copy()
2. Add a System or Developer Message: Then, use the
add_message
method to add your message:
Copy code
chat_ctx.add_message(
    role="system",
    content="You are now in troubleshooting mode."
)
3. Update the Chat Context: Finally, save your changes by calling `update_chat_ctx()`:
await self.update_chat_ctx(chat_ctx)
This way, your message becomes part of the conversation history and helps guide the AI’s responses. If you’d like, I’m here to walk you through more examples or answer any other questions you have! Sources: livekit/agent/livekit_agent_session.pb.go | Agent Session & Activity Management | livekit/livekit_composite | DeepWiki | docs/index.yaml | livekit-plugins/livekit-plugins-google/livekit/plugins/google/realtime/real... | Pipeline nodes and hooks | LiveKit Docs