i have built a livekit agent (using cursor) that i...
# ask-ai
e
i have built a livekit agent (using cursor) that interviews users. it has this line of code: # Create agent instance using custom class agent = InterviewAgent( instructions=dynamic_instructions, tools=[end_interview_tool, end_interview_early_tool] ) can you tell me how the instructions are used by the agent?
t
Thanks for reaching out, Jason. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I noticed that the term InterviewAgent and the parameter dynamic_instructions aren't clearly defined in the LiveKit documentation. Could you please share a bit more about whether this is a custom implementation? For instance, is the InterviewAgent based on a public repository, some example code, or perhaps an internal project? Understanding this will really help me guide you more effectively. I'm here to help! Sources: examples/voice_agents/llamaindex-rag/retrieval.py | Agent speech and audio | LiveKit Docs | tracking_state/npc_character.py
e
i am asking about the instructions parameter
and the term InterviewAgent is just the name of my custom class agent
t
The
instructions
parameter in LiveKit agents is like a helpful guide that shapes how the agent behaves, its tone, and its overall purpose. Think of it as the system-level direction for the language model (LLM) that influences how the agent interacts with you. Here’s a friendly overview of how it works: • Initialization: When the agent is first created, the
instructions
parameter is set and stored internally. This helps shape the agent’s responses and behavior right from the beginning. • Dynamic Updates: If you want the agent to adjust its behavior as things progress, you can update the instructions anytime using the
update_instructions()
method. This way, the agent can better respond to the context or your interactions. • Temporary Adjustments: For specific replies, you can pass temporary instructions through the
generate_reply()
method. These work alongside the main instructions for that moment but aren’t saved permanently. • Multi-Agent Workflows: In setups with multiple agents, each one can have its own special instructions tailored to its role. This makes sure interactions flow smoothly and stay relevant to the context. • Text-to-Speech (TTS): If you’re using TTS plugins, the
instructions
parameter helps guide the tone, pace, and style of the speech output, making the experience more natural and engaging. If you’d like, I’m here to help with more details or examples on how to make the most of the
instructions
parameter. Just let me know! Sources: examples/voice_agents/multi_agent.py | examples/src/comprehensive_test.ts | multi-agent/long_or_short_agent.py | Gemini TTS plugin guide | LiveKit Docs | OpenAI TTS plugin guide | LiveKit Docs
The
instructions
parameter in your custom
InterviewAgent
class is a wonderful tool to shape how your agent behaves, its tone, and its overall purpose. Think of it as the guiding voice for the language model (LLM) behind the scenes. Here's a friendly breakdown of how it works: • Initialization: When you set
instructions=dynamic_instructions
during initialization, these instructions become the foundation that guides your agent’s responses. • Dynamic Updates: You’re also able to tweak these instructions on the fly with methods like
update_instructions()
, so your agent can smoothly adjust based on the situation or what the user needs. • Per-Reply Customization: And if you want to tailor a single response differently, you can pass temporary instructions to the
generate_reply()
method. These blend with the main instructions just for that reply and don’t change the overall setup. If you have any particular scenarios in mind or more questions about making the most out of the
instructions
parameter in your custom agent, please don’t hesitate to reach out—I’m here to help!
e
can you please elaborate on how dynamic updates work and maybe give some examples?
t
I'm glad to help you understand how dynamic updates to the
instructions
parameter in LiveKit agents work! This feature lets you modify the agent's behavior during runtime using the
update_instructions()
method, which is super handy for tailoring the agent's tone or purpose based on the context, user attributes, or session state. Here are some examples to guide you through: • Adjusting Instructions Based on Participant Type: For example, if a user joins via phone (SIP), you can update the instructions so the agent speaks in a telephony-appropriate tone:
Copy code
class ChangeInstructionsAgent(Agent):
    def __init__(self):
        super().__init__(instructions="You are a helpful agent. When the user speaks, you listen and respond.")

    async def on_enter(self):
        if self.session.participant.name.startswith("sip"):
            await self.update_instructions("You are a helpful agent speaking on the phone.")
        self.session.generate_reply()
• Using Participant Attributes: In an IVR system, you might want to update instructions based on a specific task assigned to the user:
Copy code
async def on_enter(self):
    task = self.session.participant.attributes.get("task")
    if task:
        await self.update_instructions(f"Guide the user through completing task: {task}")
• Context Injection in Retrieval-Augmented Generation (RAG): You can also dynamically add context retrieved from a knowledge base into the instructions to make responses even more relevant:
Copy code
async def llm_node(self, chat_ctx: llm.ChatContext, ...):
    user_query = chat_ctx.items[-1].text_content
    retriever = self.index.as_retriever()
    nodes = await retriever.aretrieve(user_query)

    instructions = "Context that might help answer the user's question:"
    for node in nodes:
        instructions += f"\n\n{node.get_content(metadata_mode=MetadataMode.LLM)}"

    await self.update_instructions(instructions)
When you call
update_instructions()
, it updates the agent's internal state and, if the agent is in a realtime session, it also refreshes the active session's instructions. This way, the agent can adapt smoothly to any new context. If you have any questions or would like more examples, please don't hesitate to ask. I'm here to help! Sources: examples/warm-transfer/warm_transfer.py | multi-agent/long_or_short_agent.py | Gemini Live API plugin | LiveKit Docs | Gemini TTS plugin guide | LiveKit Docs