How can I add new system instructions to the agent...
# ask-ai
f
How can I add new system instructions to the agent within on_enter hook?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to guide you through adding new system instructions to the agent within the
on_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:
Copy code
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.ts
f
can I add it as new chat message instead of updating full instructions?
t
Absolutely, you can add a new chat message instead of updating the full instructions. Let me walk you through two approaches based on what you might need: 1. To add a message and trigger an immediate response: `async def on_enter(self): await self.session.generate_reply( text="Hello! How can I assist you today?" )`This will send a system-generated message into the chat context and prompt a response from the agent right away. 2. To add a message silently to the chat context: `async def on_enter(self): chat_ctx = self.chat_ctx.copy() chat_ctx.add_message(role="system", content="Initializing session...") await self.update_chat_ctx(chat_ctx)`This way, you add a message to the chat history without triggering an immediate response. It’s perfect for including background context or metadata. If you have any questions or need a hand with anything else, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent.py | Docs::Agents | Pipeline nodes and hooks | LiveKit Docs