does update_chat_ctx remove tool-calls?
# ask-ai
c
does update_chat_ctx remove tool-calls?
t
Thanks for reaching out, dexter. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’d love to help you understand the
update_chat_ctx
method a bit better! It can remove tool calls depending on how the
exclude_invalid_function_calls
parameter is set: • If
exclude_invalid_function_calls
is set to
True
(which is the default), it will remove function calls and outputs when the tools are no longer available in the agent's tool set. • But if you set it to
False
, it will keep all function calls and outputs, even if those tools aren’t present anymore. This way, you have the flexibility to manage tool interactions in a way that best fits your needs. If you have any questions or want to explore this further, I’m here for you! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | plugins/google/src/beta/realtime/realtime_api.ts
c
how can this be called
t
You can easily update the chat context of an Agent instance by calling
update_chat_ctx
. Let me walk you through a simple example:
Copy code
from livekit.agents import llm

# Create or modify a chat context
new_chat_ctx = llm.ChatContext()
new_chat_ctx.add_message(role="system", content="Updated system instructions")
new_chat_ctx.add_message(role="user", content="New user message")

# Update the agent's chat context
await agent.update_chat_ctx(new_chat_ctx)

# Or with custom options
await agent.update_chat_ctx(new_chat_ctx, exclude_invalid_function_calls=False)
This method takes the following parameters: • chat_ctx: This is where you provide the new or updated chat context. • exclude_invalid_function_calls (optional): This controls whether to exclude function calls for unavailable tools, and it defaults to
True
. If anything feels unclear or if you'd like me to guide you through it step-by-step, just let me know—I'm here to help! Sources: Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | Agents v0.x migration guide - Python | LiveKit Docs | livekit-agents/livekit/agents/voice/agent.py
c
does agent.update_tools remove function calls for tools that are no longer in the tool list (in chat_ctx)
t
Yes,
agent.update_tools
does remove function calls for tools that are no longer in the tool list within
chat_ctx
, but how this happens depends on the type of agent you’re working with: • For standard LLMs: When you call
agent.update_tools
, it updates the tool list and also syncs the chat context by removing any function calls linked to tools that aren’t available anymore. This is done by using
update_chat_ctx
with
exclude_invalid_function_calls=True
. • For RealtimeModel-based agents: The server takes care of keeping the tools consistent, so any invalid function calls are handled remotely, and there’s no need for local cleanup. If anything isn’t clear or if you’d like me to walk you through this in more detail, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki
c
can I override this so it doesn't remove the tool in chat_ctx
t
I understand that you'd like to customize the
agent.update_tools
method, but it can't be overridden to prevent the removal of tools in
chat_ctx
. This is actually a built-in feature designed to keep the agent's tool list and the chat context perfectly in sync. That said, there's a friendly workaround you can try: • First, get the current tools using
agent.tools
. • Then, merge those with any new tools you want to add. • Finally, call
update_tools
with this combined list to make sure nothing gets removed unintentionally. Another neat option is to use the
llm_node
method, which lets you inject temporary tools just for a single inference cycle—without changing your persistent tool list or updating
chat_ctx
. If you'd like, I'm here to help you step through these options or answer any questions you might have! Sources: livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | Pipeline nodes and hooks | LiveKit Docs | Tool definition and use | LiveKit Docs | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki