I am using multiagents like the restaurant example...
# ask-ai
r
I am using multiagents like the restaurant example. I am using a baseAgent. Where and how should I put system instructions that could apply to all the agents
t
Thanks for reaching out, Weilan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you provide more details about your setup? For example: • Are you using a specific framework or library for your multiagent system? • How are you currently defining and managing your agents? • What kind of system instructions are you looking to apply universally? This information will help me assist you better.
r
I have a baseAgent
Copy code
class BaseAgent(Agent):
    def __init__(self, agent_config: Optional[dict] = None, **kwargs):
        super().__init__(**kwargs)
        self.agent_config = agent_config or {}

    async def on_enter(self) -> None:
        agent_name = self.__class__.__name__
        <http://logger.info|logger.info>(f"entering task {agent_name}")

        userdata: UserData = self.session.userdata
        chat_ctx = self.chat_ctx.copy()

        # add the previous agent's chat history to the current agent
        if isinstance(userdata.prev_agent, Agent):
            truncated_chat_ctx = userdata.prev_agent.chat_ctx.copy(
                exclude_instructions=True, exclude_function_call=False
            )
            existing_ids = {item.id for item in chat_ctx.items}
            items_copy = [
                item for item in truncated_chat_ctx.items if item.id not in existing_ids
            ]
            chat_ctx.items.extend(items_copy)

        await self.update_chat_ctx(chat_ctx)
        await self.enter()
And the agents inherit from BaseAgent I want to give it context for how the AI should interact
t
Hey there! Let me walk you through some friendly ways to set up universal system instructions for your agents. It's easier than you might think! 1. Define instructions in the constructor: When you're creating a new agent, you can simply pass the instructions right when you're setting it up. Like this:
Copy code
super().__init__(
    instructions="Your instruction text here",
    # other parameters
)
1. Update the chat context with system messages: Another cool approach is to add system messages directly to your chat context. It's super straightforward:
Copy code
chat_ctx.add_message(
    role="system",
    content=f"You are {agent_name} agent. Current user data is {userdata.summarize()}"
)
1. Use the
update_instructions
function:
This handy function lets you update or add instruction messages in your chat context. Check it out:
Copy code
# This function either updates existing instructions or adds new ones
def update_instructions(chat_ctx: ChatContext, instructions: str, add_if_missing: bool = True) -> None:
    # Implementation details
These methods make it super easy to keep your agent instructions consistent and clean. Need more help or have questions? I'm always here to help you out! Sources: GitHub::livekit::agents | examples/voice_agents/llamaindex-rag/retrieval.py