how does generate_reply work?
# ask-ai
h
how does generate_reply work?
t
Thanks for reaching out, James. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how
generate_reply
works — here's a concise explanation and how to use it. What it does • Triggers the agent to produce a response (via the LLM). • Converts that response to speech with the TTS plugin and plays it in the room. • Returns a
SpeechHandle
you can use to monitor playback state or interruptions. Two ways to call it 1. With instructions (not added to chat history) ◦ Python:
session.generate_reply(instructions="Greet the user and ask how you can help.")
◦ TypeScript:
session.generateReply({ instructions: 'Greet the user and ask how you can help.' })
1. With user input (added to chat history) ◦ Python:
session.generate_reply(user_input="How is the weather today?")
◦ TypeScript:
session.generateReply({ userInput: 'How is the weather today?' })
Behavior notes • If the frontend sends text via the
lk.chat
topic, the agent interrupts any ongoing speech and internally calls
generate_reply
. • Use the returned
SpeechHandle
to wait for playback to finish or detect interruptions. If you want, tell me which language (Python or TypeScript) and whether you need an example that checks
SpeechHandle
state, and I’ll provide a short snippet. Sources: Agent speech and audio | LiveKit docs | Text and transcriptions | LiveKit docs
h
I want the agent to generate a reply telling the user that they've exceeded the maximum call duration. How do i go about this without using session.say()?
t
Acknowledged — you want the agent to tell the user “you've exceeded the maximum call duration” using
generate_reply
(not
session.say()
). You can do this by passing that text as either
instructions
(not added to chat history) or
user_input
(added to history). Use whichever fits your flow. If you want the message sent as the agent's normal reply and played via TTS, use
generate_reply
with the text. Examples: • Python (as user input):
Copy code
speech_handle = session.generate_reply(user_input="You've exceeded the maximum call duration.")
# wait for playback or check state on speech_handle as needed
• TypeScript (as user input):
Copy code
const speechHandle = await session.generateReply({
  userInput: "You've exceeded the maximum call duration."
});
// await or monitor speechHandle for playback state
If you prefer the message not be added to chat history (treated as an instruction), pass it as `instructions`: • Python:
Copy code
speech_handle = session.generate_reply(instructions="Tell the user they've exceeded the maximum call duration.")
• TypeScript:
Copy code
const speechHandle = await session.generateReply({
  instructions: "Tell the user they've exceeded the maximum call duration."
});
Notes •
generate_reply
returns a
SpeechHandle
to monitor playback or detect interruptions. • If you need the agent to interrupt ongoing speech before sending this message,
generate_reply
will handle interruption when called (the agent interrupts current speech and plays the new reply). • Tell me whether you want the text added to chat history or not, and if you want a snippet showing how to wait for or handle the
SpeechHandle
state.
h
So if I set a instruction inside generate_reply the reply generated would be based off that instruction?
t
Yes — if you pass text via
instructions
to
generate_reply
, the agent generates its reply based on those instructions. The instructions are used to guide the LLM but are not added to the chat history. Example (TypeScript):
Copy code
const speechHandle = await session.generateReply({
  instructions: "Tell the user they've exceeded the maximum call duration."
});
Example (Python):
Copy code
speech_handle = session.generate_reply(instructions="Tell the user they've exceeded the maximum call duration.")
If you want the response recorded in chat history, pass the text as
user_input
instead. Would you like a snippet that waits on the returned
SpeechHandle
or handles interruptions?
h
does generate_reply always generate an agent response? or is it dependent on the chat context?
t
Good question — here's how it works. • generate_reply always triggers the agent to produce a reply. • If you pass
instructions
, the agent generates the reply based solely on those instructions (instructions are not added to chat history). • If you pass
user_input
, the agent generates a reply that can be influenced by the existing chat history and context (the
user_input
is added to history). If you want, I can provide a short Python or TypeScript snippet showing each case and how to wait on the returned
SpeechHandle
. Which language do you prefer?