how to dynamically update the system prompt when u...
# ask-ai
m
how to dynamically update the system prompt when using realtime
t
Thanks for reaching out, Austin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you dynamically update the system prompt in LiveKit Realtime! Depending on how you're working with it, there are a couple of ways to do this: • Using the Python SDK: You can use the
update_instructions()
method in the
RealtimeModel
class to update the system prompt on the fly. Just a heads up, this will restart the session to apply the new instructions. Here's a quick example:
Copy code
# Assuming you have a realtime_model instance
await realtime_model.update_instructions("You are a financial advisor. Be concise and accurate.")
Using JavaScript Frontend: If you're working with a web app, the
add_system_anytime()
function is a great way to update the system prompt without restarting the session. Here's how you might do it:
Copy code
const chat = new SimpleChat("my-chat");
chat.add_system_anytime("You are a friendly assistant. Be helpful and kind.", "user-action");
If you have any questions or want me to walk you through a specific example tailored to your needs, just let me know—I'm here to help! Sources: livekit-agents/livekit/agents/voice/chat_cli.py | tools/server/public_simplechat/readme.md | Vision Agent Quickstart | LiveKit Docs | Vision Agent Quickstart | LiveKit Docs
m
what if I want to update tools?
t
To dynamically update tools in LiveKit Realtime, you can use the
update_tools()
method from the
Agent
class. This handy method lets you replace the current tools with a fresh list and keeps everything in sync with your session. Here’s a quick example:
Copy code
# Add a new tool dynamically
await agent.update_tools(agent.tools + [new_tool])
If you want to remove tools, you can easily filter the current list like this:
Copy code
# Remove a specific tool
tools = [t for t in agent.tools if t != tool_to_remove]
await agent.update_tools(tools)
Feel free to reach out if you’d like more details or examples tailored to your situation—I’m here to help! Sources: Tool definition and use | LiveKit Docs | livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | tool_calling/update_tools.py
m
I'm initializing an AgentSession object for my realtime interface. I have followed the documentation here https://docs.livekit.io/agents/integrations/realtime/openai/. the
update_instructions
method is for a RealtimeSession object, how do I access that object?
t
To help you access the
RealtimeSession
object from an
AgentSession
, you can use the
realtime_llm_session
property in Python or the
realtimeLLMSession
property in JavaScript/TypeScript. This property gives you the session linked to the agent's activity context. Python Example:
Copy code
# Access the RealtimeSession
try:
    realtime_session = agent.realtime_llm_session
    # Use the session, for example, to update instructions
    await realtime_session.update_instructions("New system prompt")
except RuntimeError as e:
    print("Realtime session is not available:", e)
JavaScript Example:
Copy code
// Access the RealtimeSession
const realtimeSession = agentActivity.realtimeLLMSession;
if (realtimeSession) {
    realtimeSession.updateInstructions("New system prompt");
} else {
    console.error("Realtime session is not available.");
}
Just a quick reminder to make sure your agent is running and that a
RealtimeModel
is provided to the
Agent
or
AgentSession
. The
RealtimeSession
only gets created in this setup. If anything feels unclear or you want to dive deeper, please don’t hesitate to ask—I’m here to help! Sources: livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/__init__.... | plugins/google/src/beta/realtime/realtime_api.ts