Can I update an agent instructions from my server?
# ask-ai
a
Can I update an agent instructions from my server?
t
Thanks for reaching out, Anne. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, you can update an agent's instructions from your server programmatically using the
update_instructions
method provided by the Agent class. This method allows you to dynamically change the agent's instructions during runtime. Here are the key details: • When the agent is inactive: The method updates the stored instructions. • When the agent is running: It updates both the stored instructions and any ongoing realtime session. Here is an example of how you can use this method:
Copy code
async def on_enter(self):
    if self.session.participant.name.startswith("sip"):
        await self.update_instructions("""
            You are a helpful agent speaking on the phone.
        """)
    self.session.generate_reply()
Note: If the agent is running in realtime mode, the method also updates the instructions for the ongoing realtime session. If this update fails, it raises an
llm.RealtimeError
. This functionality enables server-side applications to dynamically modify agent behavior based on context, user data, or other runtime conditions. Sources: README.md | mcp/agent.py