I want to update my agent's metadata inside Agent'...
# ask-ai
r
I want to update my agent's metadata inside Agent's
async def *on_exit*(_self_):
method, how to do that?
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you update your agent's metadata inside the
on_exit
method, you can use the
update_metadata
method on the
local_participant
object. Here’s a simple example to guide you:
Copy code
import json
import time
from livekit.agents.voice import Agent

class MyAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="Your agent instructions here",
            # ... other configuration
        )
    
    async def on_exit(self):
        # Update metadata with exit information
        exit_metadata = json.dumps({
            "status": "exited",
            "exit_timestamp": time.time(),
            "session_duration": time.time() - self.session_start_time,
            "final_state": "completed"
        })
        
        # Update the agent's metadata
        await self.session.room.local_participant.update_metadata(exit_metadata)
        
        # Optional: Say goodbye before exiting
        await self.session.say("Goodbye!")
Just a quick reminder: make sure your agent has the
canUpdateMetadata
permission to modify its own metadata. Without this permission, the update won’t go through. If you have any questions or need a bit more help, please don’t hesitate to ask—I’m here for you! Sources: lib/src/proto/livekit_models.pb.dart | Pipeline nodes and hooks | LiveKit docs | livekit-agents/livekit/agents/voice/room_io/room_io.py | server/agent.py
r
error in
self.session.room.local_participant.update_metadata
as
AttributeError: 'AgentSession' object has no attribute 'room'
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!