rough-hairdresser-81331
06/26/2025, 4:04 PMtall-airline-16756
06/26/2025, 4:04 PMtall-airline-16756
06/26/2025, 4:04 PMupdate_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.pyrough-hairdresser-81331
06/26/2025, 4:05 PMtall-airline-16756
06/26/2025, 4:06 PMclass 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: 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: @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: 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.mdrough-hairdresser-81331
06/26/2025, 4:06 PMtall-airline-16756
06/26/2025, 4:07 PMcommon_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: 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: @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 Docsrough-hairdresser-81331
06/26/2025, 4:08 PMtall-airline-16756
06/26/2025, 4:09 PM