I'm using multiagents - whats the best way to inje...
# ask-ai
r
I'm using multiagents - whats the best way to inject system instructions at the very beginning so that all agents get the context?
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.
To ensure that all agents in a multi-agent setup receive system instructions at the very beginning, you can inject these instructions into the chat context as a system message. Here are some approaches: • Use a function like
update_instructions
to check if a system instruction message already exists. If it does, update it; otherwise, insert the instructions at the beginning of the chat context. • When adding context from retrieved documents, check if the first message is a system message. If it is, append the instructions to it; otherwise, insert a new system message at the start. • Ensure that system messages are preserved during context truncation by explicitly retaining them at the beginning of the context. These methods ensure that system instructions are consistently placed at the start of the chat context, providing all agents with the necessary context from the outset. Sources: GitHub::livekit::agents | livekit-agents/livekit/agents/llm/chat_context.py
r
so I'm utilizing a base agent and each agent will inherit from the base layer. What's the best way to do that?
t
Hey there! 👋 Let me walk you through how to create a super flexible multi-agent setup that'll make your code feel like a well-oiled machine. • Create a Base Agent Class: Think of this like creating a friendly template that all your other agents can use. It's like setting up a shared playbook that everyone follows:
Copy code
class BaseAgent(Agent):
    def __init__(self, instructions: str, **kwargs):
        super().__init__(instructions=instructions, **kwargs)
Extend the Base Agent: Now comes the fun part – customizing your agents! It's like giving each of your digital teammates their own unique superpower:
Copy code
class CustomAgent(BaseAgent):
    def __init__(self, custom_param: str, **kwargs):
        super().__init__(instructions="Custom instructions", **kwargs)
        self.custom_param = custom_param
Agent Handoff: Imagine your agents smoothly passing the baton in a relay race. This is how they can tag-team and hand off tasks:
Copy code
@function_tool
async def switch_agent(self, context, param):
    new_agent = AnotherAgent(param)
    return new_agent, "Switching to another agent."
Lifecycle Methods: These are like welcome mats for your agents, helping them get settled and ready to rock:
Copy code
async def on_enter(self):
    self.session.generate_reply()
By doing this, you're creating a super flexible system where your agents can share core functionality but still have their own unique personalities. Pretty cool, right? 🚀 Sources: GitHub::livekit::agents | README.md
r
I want to pass in context/instructions that applies to all the agents in the base class and I only want to do it once
t
To create a smooth shared context for all your agents, I've got some friendly approaches that can help! Think of these like setting up a group chat where everyone's on the same page. • Define Common Instructions: It's like creating a team handbook that everyone can reference. Here's how you can set up a shared guide for your agents:
Copy code
common_instructions = "Your shared instructions here."

class BaseAgent(Agent):
    def __init__(self, **kwargs):
        super().__init__(instructions=common_instructions, **kwargs)
Use Chat Context: Imagine passing around a shared notebook that keeps everyone's notes in sync. By using a
chat_ctx
object, you're making sure all your agents have the same context:
Copy code
class BaseAgent(Agent):
    def __init__(self, chat_ctx, **kwargs):
        super().__init__(chat_ctx=chat_ctx, **kwargs)
Utilize Userdata: Think of this like a team bulletin board where everyone can see and access the same important info. You can store shared instructions or state that all agents can tap into:
Copy code
@dataclass
class SharedData:
    instructions: str = "Your shared instructions here."

session = AgentSession(userdata=SharedData())

# Access in agents
self.session.userdata.instructions
These methods are like creating a friendly, collaborative environment where all your agents are working together seamlessly. No more confusion, just smooth teamwork! Sources: GitHub::livekit::agents | examples/voice_agents/llamaindex-rag/retrieval.py | Workflows | LiveKit Docs
r
wont every children of BaseAgent re inits the instructions?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!