i want to send agent instructions before start, li...
# ask-ai
p
i want to send agent instructions before start, like prewarm the in chat context to reduce latency of first response. but im not able to do it with chat context append since that method doesnt exist. is there any way to achieve this?
t
Thanks for reaching out, Vaneeza. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help reduce the latency of the first response and provide agent instructions right from the start, here are some friendly tips you can try: • Predefined Agent Instructions: You can set initial instructions when creating the agent to gently guide its behavior. For example:
Copy code
agent = Agent(
    instructions="You are a helpful assistant. Respond concisely.",
    llm=openai.LLM(),
)
• Preload Chat Context: It’s a great idea to inject chat history or context before the session begins so the agent has all the relevant info immediately. Here’s how you might do it:
Copy code
chat_history = [
    {"role": "assistant", "content": "Hello, I am a travel planner."},
    {"role": "user", "content": "I want to go to Paris."},
]

chat_ctx = llm.ChatContext.empty()
for item in chat_history:
    chat_ctx.add_message(role=item["role"], content=item["content"])

agent = Agent(
    instructions="You are a helpful travel planner.",
    llm=openai.realtime.RealtimeModel(),
    chat_ctx=chat_ctx
)
• Prewarm Worker Processes: You might also consider using a prewarm function to load models or static data before a job starts. This can really help reduce that initial wait time. If you’d like, I’m here to walk you through any of these steps or provide more details—just let me know! Sources: Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | examples/voice_agents/realtime_load_chat_history.py | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | Worker options | LiveKit Docs | Real-time AI Models & Streaming | livekit/livekit_composite | DeepWiki
p
so if im adding in chat context do i need to add in
Agent.__init__()
as well? instructions as well? or whats the difference between the two?
t
I'm glad you asked about the
instructions
and
chat_ctx
parameters in
Agent.__init__()
! They each play a unique role in shaping how your agent works: • Instructions: Think of these as the heart of your agent’s personality and behavior. They act like a system prompt, guiding how the agent responds throughout its entire session. For instance, you might set instructions like "You are a helpful assistant." Plus, you can always update these instructions on the fly using
update_instructions()
to keep things fresh. • Chat Context (chat_ctx): This is all about the conversation history or the context your agent starts with. It can include previous messages, function calls, or any relevant info to keep the chat flowing smoothly. It’s especially handy for maintaining continuity or setting up a specific context before the session begins. And just like instructions, you can update the chat context anytime with
update_chat_ctx()
. So, to sum it up: use
instructions
to shape your agent’s behavior and
chat_ctx
to keep track of the conversation’s state. If you’d like, I’m here to help with more details or examples—just let me know! Sources: examples/voice_agents/realtime_load_chat_history.py | basics/context_variables.py | Agents v0.x migration guide - Python | LiveKit Docs | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki
p
great. you see i have system prompt which will guide agent's behaviour, but it is long. and i think that's why it takes time for the first response, because its too long. you see i play a greeting on session.start, so the conversation starts immedialtely and the user responds, but then the aent is loading the system instructions and responds after 4 seconds. latency seems high.
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!
p
great. you see i have system prompt which will guide agent's behaviour, but it is long. and i think that's why it takes time for the first response, because its too long. you see i play a greeting on session.start, so the conversation starts immedialtely and the user responds, but then the aent is loading the system instructions and responds after 4 seconds. latency seems high. so what to use? chat ctx or init agent instructions?