wooden-beard-26644
08/13/2025, 3:43 PMtall-airline-16756
08/13/2025, 3:43 PMtall-airline-16756
08/13/2025, 3:44 PMon_user_turn_completed
node. This lets you tweak the chat context or the user’s message before the agent jumps in with a response. Here’s how you can do it:
• Using on_user_turn_completed
to add context: Simply add more context to the chat with turn_ctx.add_message()
. This way, the agent will have that info ready for the next turn without you having to spell it out every time.
• Modifying the user's message: You can also change the user's message to include extra details before it gets added to the chat context. That makes sure the agent sees the updated message when it’s time to respond.
• Updating the chat context persistently: If you want the context to stick around beyond just the current turn, use update_chat_ctx()
. This keeps everything saved nicely.
Using these methods makes it super smooth to provide context for the agent’s next move. Hope that helps!
Sources: AgentCallContext | LiveKit Agents | Workflows | LiveKit Docs | Voice pipeline nodes | LiveKit Docs | basics/context_variables.py | Agents v0.x migration guide | LiveKit Docswooden-beard-26644
08/13/2025, 3:44 PMtall-airline-16756
08/13/2025, 3:45 PMfunction_tool
, you simply return a string from the tool. This message will be sent back to the LLM, which can then use it to craft a response or take the next steps. Here’s a quick guide on how to do it:
• Adding a Message: Just return a string from the tool, like this:
return "This is a message from the function tool."
• What Kind of Message to Add:
◦ Clear and descriptive: Make sure the message clearly shows what the tool is doing or the info it’s sharing.
◦ Specific: Customize the message to fit the tool’s purpose, like explaining errors or giving status updates.
◦ Concise: Keep it short but meaningful.
For example, if you’re handling an error, you might raise a ToolError
with a friendly message like this:
raise ToolError("This location is coming soon. Please join our mailing list to stay updated.")
Or, if you want to share a status update:
return f"Order {order_id} has been successfully processed."
These messages help provide useful feedback, handle exceptions smoothly, or keep users in the loop about what the tool is up to.wooden-beard-26644
08/13/2025, 3:45 PMtall-airline-16756
08/13/2025, 3:46 PMfunction_tool
without actually sending any info back to the user, here are some friendly ways to do it:
• Keep Context with `chat_ctx`: You can use chat_ctx.copy()
to hold onto the conversation history between tasks.
• Use Session UserData: Store and tweak shared info quietly using context.userdata
so it flows smoothly between tasks.
• Silent State Updates: Change the context behind the scenes without showing anything to the user by returning None
or managing state internally.
• Agent Handoffs: Switch between agents while keeping the chat history and context intact.
Here’s a quick example of updating internal state quietly:
@function_tool()
async def update_internal_state(self, context: RunContext, data: str):
context.session.userdata.some_attribute = data
await self._process_data_internally(data)
return None
Using these approaches, you can handle context smoothly without bothering the user with extra messages. Hope this helps!
Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Workflows | LiveKit Docs | Workflows | LiveKit Docs | rag/rag_handler.py