How can I provide context to the agent without gen...
# ask-ai
w
How can I provide context to the agent without generating a reply? Can I send the agent a system agent giving it instructions or context for how to handle the next turn?
t
Thanks for reaching out, Alex. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! You can easily give some extra context to the agent without needing it to reply right away by using the
on_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 Docs
w
I want to do this within a function_tool, how can i add a message and what kind of message should I add?
t
If you want to add a message within a
function_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:
Copy code
raise ToolError("This location is coming soon. Please join our mailing list to stay updated.")
Or, if you want to share a status update:
Copy code
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.
w
I'm calling two tasks within a function tool, and i want to provide context between the two task calls, so returning info isn't great.
t
Hey there! If you want to keep the context between two task calls inside a
function_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:
Copy code
@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