Can I add a system prompt inside agent "on_enter" ...
# ask-ai
r
Can I add a system prompt inside agent "on_enter" hook?
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can add a system prompt inside the agent's
on_enter
hook. This hook is a great place to run custom logic when an agent becomes active in a session, so it’s perfect for dynamically setting or updating the agent’s behavior. Here’s how you can do it: 1. First, access the chat context using
self.chat_ctx
. 2. Then, add a system message with the
add_message()
method, making sure to use
role="system"
. 3. Finally, call
self.update_chat_ctx()
to save your changes. For example:
Copy code
async def on_enter(self) -> None:
    agent_name = self.__class__.__name__
    chat_ctx = self.chat_ctx.copy()
    chat_ctx.add_message(
        role="system",
        content=f"You are the {agent_name}. {userdata.summarize()}"
    )
    await self.update_chat_ctx(chat_ctx)
    self.session.generate_reply()
If you have any questions or need a hand with this, please don’t hesitate to reach out—I’m here to help! Sources: pipeline-llm/large_context.py | Company directory phone assistant | LiveKit Docs | delay-handling/agent.py | Pipeline nodes and hooks | LiveKit docs