This message was deleted.
# ask-ai
s
This message was deleted.
t
Thanks for reaching out, Heorhi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're right to ask — this can feel like the agent "losing" its instructions. Here's what's happening and how to fix it. What’s happening • Calling
toolOptions.ctx.session.generateReply()
does not erase the agent's base instructions stored on the Agent instance. • If you pass an
instructions
argument to
generateReply()
, those instructions override the prompt for that single generated reply, which can make it look like the original instructions were dropped. • If you create a new agent/task or replace/clear
chat_ctx
(the chat context that contains the initial system message), the new context won't include the original instructions and behavior will change. How to troubleshoot and fix 1. Check for transient overrides ◦ Search your tool calls for
generateReply(instructions=...)
. Remove or adjust those overrides if you need the original instructions to persist. 1. Preserve
chat_ctx
across handoffs ◦ Ensure you pass the existing
chat_ctx
when handing off to another agent or starting a new task so the system message with initial instructions remains. 1. Avoid recreating the agent without instructions ◦ If you instantiate an Agent or start a new session, make sure its
instructions
are set the same as the original or reuse the existing Agent instance. 1. Log the chat context for debugging ◦ Before and after your
generateReply()
call, log
toolOptions.ctx.chat_ctx
(or equivalent) to confirm the system/instructions message remains present. If you want, share a short code snippet showing the
generateReply()
call and how you manage
chat_ctx
or agent handoffs — I’ll point out the exact line that’s causing the override. Sources: Tool definition and use | LiveKit docs | Agent speech and audio | LiveKit docs | Agents and handoffs | LiveKit docs
q
Problem is with one agent, I do not switch agents and do not use handoffs.
t
Thanks — that helps narrow it down. Since you’re not switching agents or doing handoffs, the most likely causes are transient instruction overrides or unintentional chat context modification inside the tool. To diagnose quickly, I need a minimal snippet showing how you call
generateReply()
and how you manage the session/chat context. Please share: 1. The tool function (or the relevant portion) where you call
toolOptions.ctx.session.generateReply()
(3–10 lines around the call). 2. Any
instructions
argument you pass to
generateReply()
. 3. Where/if you modify
toolOptions.ctx.chat_ctx
(or
session.chat_ctx
) before or after the call. 4. The Agent creation code showing the original
instructions
(if short). If you prefer, paste a short reproduction (pseudo-code is fine). I’ll point out exactly which line is causing the override or context change and suggest the minimal fix.
Thanks — the snippet makes the cause clear. What’s happening • Calling
toolOptions.ctx.session.generateReply({ instructions: ... })
overrides the agent’s base instructions for that single LLM call. That transient override is why the agent appears to “lose” its initial instructions even though the agent itself still has them. What you can do (pick one) • Remove the
instructions
override and pass the update as `userInput`:
Copy code
toolOptions.ctx.session.generateReply({
    userInput: `Updated customer status to "${updatedCustomer.status}". Please continue the conversation.`
  });
• Or simply return the status string from the tool and let the framework send it to the LLM:
Copy code
return `Customer status updated to "${updatedCustomer.status}". Please continue conversation.`;
• If you must use
instructions
, include the agent’s essential behavior rules in that instruction (not recommended long-term). If you want, paste a short run with logs of
toolOptions.ctx.chat_ctx
before/after the call and I’ll point to the exact change to make. Sources: Agent speech and audio | LiveKit docs | Tool definition and use | LiveKit docs | Prompting guide | LiveKit docs
q
If i simply return the status string, realtime model doesn't say anything
t
Thanks — useful detail. Why returning a string can be silent • When a tool returns a string, that becomes the tool's output (a FunctionCallOutput). The LLM must then generate a follow-up assistant message for speech to be synthesized. If the LLM doesn't produce an assistant message after the tool output, no audio is spoken. How to make the agent speak while keeping its instructions • Use
generateReply()
but do not pass
instructions
. Use
userInput
so the agent keeps its base instructions and is prompted to respond:
Copy code
await toolOptions.ctx.session.generateReply({
  userInput: `Updated customer status to "${updatedCustomer.status}". Please continue the conversation.`
});
• Or return a clear prompt that encourages a reply, e.g.:
Copy code
return `Customer status updated to "${updatedCustomer.status}". Ask the customer if they need anything else.`;
(If the model still stays silent, combine this with
userInput
.) Additional tips • Ensure the real-time model is configured with audio modalities (e.g.,
modalities: ['text','audio']
) so assistant messages are spoken. • If you need a guaranteed immediate spoken message, call
generateReply()
with
userInput
as shown above; avoid
instructions
unless you include the agent's full behavior rules. If you want, paste a short run log showing the tool output event and whether an assistant message follows — I’ll point out exactly where the pipeline stops. Would you like that? Sources: Tool definition and use | LiveKit docs | Agent speech and audio | LiveKit docs | OpenAI Realtime API plugin guide | LiveKit docs
q
How LLM handles userInput ?
t
Good question — here’s how
userInput
is handled when you call
generateReply()
. What
userInput
does •
userInput
is treated as a new user message appended to the conversation history (chat context). • The LLM sees it the same way it would see a real user utterance, then generates an assistant reply that follows the agent’s base instructions and the full chat context. Behavioral effects • Preserves agent identity and rules: because you’re not supplying an
instructions
override, the agent’s system prompt (its core instructions) remains active. • Drives response content: the LLM uses prior messages + the new
userInput
to decide how to respond, including whether to speak, ask questions, or take actions. • Triggers multimodal output if configured: if the real-time model is enabled with audio modalities (e.g.,
modalities: ['text','audio']
), an assistant message produced in response to
userInput
will generate speech. When to use
userInput
• Use
userInput
when you want the model to integrate tool results or internal state into a normal assistant reply while keeping the agent’s behavior consistent. • Prefer it over
instructions
when you want to avoid temporarily overriding the agent’s system prompt. Example
Copy code
await session.generateReply({
  userInput: `Updated customer status to "${updatedCustomer.status}". Please continue the conversation.`
});
This appends that user message and asks the LLM to generate a normal assistant turn that follows the agent’s instructions. If you want, share a snippet of how you call
generateReply()
and your agent’s modalities; I can confirm whether the reply should produce audio in your setup.
q
I user want to switch speaking language. And I'm using tool for that, what I should return in userInput then?
t
You're on the right track asking this. Quick answer: returning a string via
userInput
won't reliably switch the agent's speaking language. To change language from inside a tool, call
session.generateReply()
and either: • Use
instructions
to immediately force the agent to reply in the new language (guaranteed but temporarily overrides base instructions):
Copy code
await toolOptions.ctx.session.generateReply({
  instructions: `From now on, respond in Spanish. Greet the user in Spanish to confirm the change.`
});
• Preferably, update session state and use
userInput
so the agent keeps its identity, then let the agent produce the next reply in the chosen language (recommended for preserving behavior): 1. Store the preference:
Copy code
toolOptions.ctx.session.userdata.active_language = 'es';
1. Prompt the agent via
userInput
referencing that change:
Copy code
await toolOptions.ctx.session.generateReply({
  userInput: `Set language to Spanish ("es"). Please continue the conversation and respond in Spanish.`
});
Notes and best practices • If you need an immediate spoken confirmation, use
instructions
for that one reply, but include the agent’s role if you want to keep behavior consistent. • To make the language persistent, keep the preference in
session.userdata
and ensure your agent's base instructions or reply-generation logic checks that field on each turn. • Ensure your real-time model has audio enabled (e.g.,
modalities: ['text','audio']
) so replies are spoken. If you share whether you want the switch to be immediate-only or persistent, and a short snippet of your agent’s base instructions or how you synthesize TTS, I’ll suggest the exact minimal change. Sources: Agent speech and audio | LiveKit docs | Agents and handoffs | LiveKit docs | LiveKit Docs
q
And after language change I need to mentioned that each time I use generateReply in any other tool ?
t
You do not need to restate the language on every
generateReply()
call. Persist the choice in session state and make the agent consult that state when composing replies. What to do (recommended) 1. Save the preference once:
Copy code
toolOptions.ctx.session.userdata.active_language = 'es';
1. Ensure your agent’s reply logic (or base instructions) looks at
session.userdata.active_language
and uses it to set output language for every turn. Example pattern:
Copy code
// when generating a reply (inside tools or main loop)
const lang = toolOptions.ctx.session.userdata.active_language || 'en';
await toolOptions.ctx.session.generateReply({
  userInput: `System note: respond in ${lang}. ${your_prompt_or_tool_output}`
});
1. For an immediate spoken confirmation after switching, send one
generateReply()
with
instructions
or a
userInput
that prompts a reply in the new language, then rely on the stored preference thereafter. Notes / best practices • Prefer storing language in
session.userdata
so the agent’s identity and behavior remain consistent. • Use
userInput
rather than
instructions
when possible so you do not temporarily override the agent’s core instructions. • If your agent’s base instructions don’t automatically consult
session.userdata
, update them to include a line like: “Check session.userdata.active_language and respond in that language.” • For persistent TTS in the chosen language, ensure audio modalities remain enabled. If you want, paste your agent’s base instruction snippet and I’ll show the exact minimal line to add so it respects
session.userdata.active_language
automatically.