rough-hairdresser-81331
06/26/2025, 4:21 PMtall-airline-16756
06/26/2025, 4:21 PMtall-airline-16756
06/26/2025, 4:21 PMrough-hairdresser-81331
06/26/2025, 4:23 PMclass 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 interacttall-airline-16756
06/26/2025, 4:24 PMsuper().__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:
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:
# 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