how do we conversation.item.delete with openai rea...
# ask-ai
s
how do we conversation.item.delete with openai realtime livekit python
t
Thanks for reaching out, MBMBMB. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to guide you through deleting a conversation item using the
conversation.item.delete
event in LiveKit's Python SDK. Here's a simple way to do it: 1. Access the Current Chat Context: First, you'll want to get the current
chat_ctx
from your
RealtimeSession
. This holds all the conversation items you have so far.
current_ctx = session.chat_ctx
2. Create a New ChatContext Without the Item: Next, filter out the item you wish to delete by its ID, and create a new
ChatContext
without it.
Copy code
# Assume 'item_to_delete_id' is the ID of the item to remove
new_items = [item for item in current_ctx.items if item.id != item_to_delete_id]
new_chat_ctx = llm.ChatContext(items=new_items)
3. Update the Chat Context: Finally, update the chat context by passing this new
ChatContext
to the
update_chat_ctx
method. The SDK will recognize the missing item and send the
conversation.item.delete
event for you.
await session.update_chat_ctx(new_chat_ctx)
This approach helps keep everything in sync between your app and the server. If you have any questions or want me to walk you through any part, just let me know—I'm here to help! Sources: livekit-agents/livekit/agents/llm/remote_chat_context.py | agents/src/llm/remote_chat_context.test.ts
s
cpu staircase between chatcontxt update how come how can we prevent this
t
I understand that increased CPU usage during
chat_ctx
updates can be a bit frustrating. This happens because the SDK calculates a diff between the old and new contexts using a dynamic programming algorithm, which can be quite CPU-intensive when dealing with large contexts or frequent updates. But don’t worry, there are some helpful strategies to ease this: 1. Minimize Chat Context Size: You can use the
truncate()
method to limit how many messages are included in the context before updates. Keeping the context smaller really helps reduce computation time.
new_chat_ctx.truncate(max_items=20)
2. Batch Updates: Try gathering multiple changes together and updating the context less often. This way, you consolidate the diff computations and lighten the CPU load. 3. Use Agent Handoffs: Another approach is to hand off control to a new agent with a fresh context, instead of constantly modifying a large existing one. 4. Leverage Data Packets: For scenarios with very frequent updates, consider using LiveKit's data packet feature instead of the chat context system—it’s designed for efficiency. These tips should help bring down the CPU usage quite a bit. If you’d like, I’m here to walk you through any of these steps or answer any questions you have! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Agents v0.x migration guide - Node.js | LiveKit Docs | Agents 0.x migration guide | LiveKit Docs
s
1. 2. Use Agent Handoffs: Another approach is to hand off control to a new agent with a fresh context, instead of constantly modifying a large existing one. this one ?
t
Absolutely, using agent handoffs is a wonderful way to manage chat context without having to overhaul large existing contexts. Let me walk you through how you can implement this smoothly: 1. Basic Agent Handoff: You simply pass the current agent's
chat_ctx
to the new agent during the handoff to keep the conversation flowing naturally.
Copy code
@function_tool()
async def transfer_to_technical_support(self):
    await self.session.generate_reply(instructions="Inform the customer that you're transferring them to the technical support team.")
    return TechnicalSupportAgent(chat_ctx=self.session.chat_ctx)
2. Selective Context Transfer: If you want more control, you can truncate or filter the chat context before passing it along to the new agent.
Copy code
new_chat_ctx = self.session.chat_ctx.truncate(max_items=20)
return NewAgent(chat_ctx=new_chat_ctx)
3. Advanced Context Management: For even greater customization, you might merge or tailor the chat context by combining previous agent contexts or adding system messages.
Copy code
chat_ctx = self.chat_ctx.copy()
chat_ctx.add_message(role="system", content="User data: {userdata.summarize()}")
await self.update_chat_ctx(chat_ctx)
If you'd like, I’m here to help with more details on any of these methods—just let me know! Sources: Workflows | LiveKit Docs | Workflows | LiveKit Docs | main.py | telephony/warm_handoff.py
s
whats the difference with instructions update and self.session.chat_ctx.truncate(max_items=0)
t
Thanks for reaching out! Could you please help me understand a bit more about what you mean by "instructions update"? Are you talking about updating the instructions for the session, the agent, or maybe something else? Once I have a clearer picture, I’ll be better able to compare it with
self.session.chat_ctx.truncate(max_items=0)
and assist you more effectively.
s
this vs new agent whats the best
t
Thanks for reaching out! Choosing between updating instructions and creating a new agent really depends on what you’re aiming to achieve: • Use `update_instructions()`: This is great when you want to gently tweak or refine the agent’s behavior during a session without starting over or changing its core identity. It keeps the session context, tools, and chat history intact, so it’s perfect for small adjustments. • Create a New Agent: This works best when you need to make bigger changes—like switching roles, isolating security or state, or setting up specialized workflows. It’s ideal for when the agent needs a fresh start with different behavior or tools. Here’s a little tip: Try using
update_instructions()
for ongoing tweaks and save new agents for distinct, role-based tasks. If you want, I’m happy to walk you through how to set up either one—just let me know! Sources: basics/change_agent_instructions.py | main.py | Workflows | LiveKit Docs | examples/voice_agents/restaurant_agent.py
s
will create new agent cause a cpu spike ?
t
Hi there! When you create a new agent using the LiveKit Python SDK, it usually doesn’t cause a big CPU spike because the process is pretty light—mainly setting up a WebSocket connection and initializing some monitoring tools. That said, CPU usage tends to go up when the agent begins handling tasks like real-time audio/video streaming or running models such as STT, LLM, or TTS. If you’re seeing CPU spikes right when the agent is created, it might be because jobs are starting immediately after the agent registers or there’s some heavy initialization happening behind the scenes. Taking a closer look at the agent’s entrypoint and startup logic could really help smooth things out. Feel free to reach out if you’d like a hand with optimizing how your agent starts up or managing your workloads better—I’m here to help! Sources: Operational Guidance | livekit/livekit_composite | DeepWiki | README.md | Deploying to custom environments | LiveKit Docs
s
but when we make the previous one exit to welcome the new one ?
t
Thanks for reaching out! Could you please share a bit more about how the previous agent is being exited? For example, are you using a specific method to terminate the agent, or is it being replaced automatically? Understanding this will really help me figure out if the transition might be causing the CPU spike. I’m here to help!
s
if we make the previous agent xit the session before welcoming the new one will it cause cpu spike ?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
s
will clearing the chat context enough to have agent behaving like in a fresh start
?