faint-arm-51818
09/26/2025, 9:30 AMtall-airline-16756
09/26/2025, 9:30 AMtall-airline-16756
09/26/2025, 9:31 AMon_enter hook! You can easily do this by using the agent's update_instructions method. This allows you to dynamically change the system message in the agent's chat context at runtime, making sure that all following responses align with the updated instructions.
Here’s a friendly example in Python to illustrate this:
from livekit.agents import JobContext
from livekit.agents.voice import Agent, AgentSession
from livekit.plugins import deepgram, openai, silero
class ChangeInstructionsAgent(Agent):
def __init__(self):
super().__init__(
instructions="""
You are a helpful agent. When the user speaks, you listen and respond.
""",
stt=deepgram.STT(), # Speech-to-Text
llm=openai.LLM(model="gpt-4o"), # Language Model
tts=openai.TTS(), # Text-to-Speech
vad=silero.VAD.load() # Voice Activity Detection
)
async def on_enter(self):
# Dynamically change instructions based on participant
if self.session.participant.name.startswith("sip"):
self.update_instructions("""
You are a helpful agent speaking on the phone.
""")
# Trigger immediate response with new instructions
await self.session.generate_reply()
async def entrypoint(ctx: JobContext):
session = AgentSession()
await session.start(
agent=ChangeInstructionsAgent(),
room=ctx.room
)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
In this example, whenever a SIP participant joins, the instructions update dynamically to better suit the context, helping the agent respond more appropriately.
If you have any questions or a specific scenario you’d like to explore, please feel free to reach out—I’m here to help!
Sources: Docs::Agents | basics/change_agent_instructions.py | tests/test_agent_session.py | examples/src/restaurant_agent.tsfaint-arm-51818
09/26/2025, 9:32 AMtall-airline-16756
09/26/2025, 9:33 AM