let's assume my agent session has a predefined lim...
# ask-ai
b
let's assume my agent session has a predefined limited time i'd like to pass that information to the agent to make sure it wraps up the session if the time is running up how to do it best
t
Thanks for reaching out, mateuszkasprowicz. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I understand how important it is to manage your agent’s session time smoothly. To help your agent wrap up a session when time is running out, you can pass session time limit information as metadata during agent dispatch. This metadata can include helpful parameters like maximum session length or idle time, depending on how you’ve set things up. Here are a couple of examples to guide you: • You can use the
metadata
field in the
RoomAgentDispatch
struct (Swift SDK) to include structured configuration data. • Alternatively, you might pass session-level metadata via
Session.withAgent()
by setting the
agentMetadata
parameter during token generation. Also, plugins like Simli or Ultravox support time limit configurations that could be really useful. For example, Simli lets you define
max_session_length
and
max_idle_time
, while Ultravox offers a
time_exceeded_message
parameter to manage how the session ends. If you’d like, I’m here to walk you through any part of this or provide more details—just let me know! Sources: livekit-plugins/livekit-plugins-simli/livekit/plugins/simli/avatar.py | Ultravox plugin guide | LiveKit Docs | Sources/LiveKit/Types/RoomConfiguration.swift | LiveKit Pricing
b
so how to pass it to the context? append assistant message after a turn completes?
t
I'm happy to help you pass session time limit information to the context and add a message after a turn completes! You can achieve this smoothly by using the
on_user_turn_completed
lifecycle hook. This way, you can easily check how much time has passed and gently notify users when their session is nearing its end. Here’s a simple approach to guide you through: 1. Start by tracking the session start time as soon as the session begins (for example, in the
on_enter
hook). 2. Set up a background task that keeps an eye on the elapsed time and figures out how much time is left. 3. Then, inside the
on_user_turn_completed
hook, you can add a friendly message to the context when the session is close to finishing. For instance:
Copy code
async def on_user_turn_completed(self, turn_ctx, new_message):
    elapsed_time = time.time() - self.session_start_time
    remaining_time = self.session_duration - elapsed_time

    if 0 < remaining_time <= 120:  # Within 2 minutes
        warning_msg = f"You have {int(remaining_time)} seconds remaining in this session."
        turn_ctx.add_message(role="assistant", content=warning_msg)
If you'd like, I’d be more than happy to provide more details or even a full example to get you started. Just let me know! Sources: Analytics API | LiveKit docs | Quotas and limits | LiveKit docs | Pipeline nodes and hooks | LiveKit docs | livekit-agents/livekit/agents/voice/agent_activity.py | observability/sessiontimer.go
b
which message type will be available to the assistant context but won't be visible in the transcript?
t
Hi there! I wanted to share a bit about the message types that are part of the assistant’s context but don’t show up in the transcript you see: • FunctionCall: This is when the assistant initiates a function call behind the scenes to use a tool. • FunctionCallOutput: This shows the result of that function call, helping the assistant think things through. • AgentHandoff: This marks when the conversation is passed between agents, but it stays hidden from the user. These message types help the assistant work smoothly but aren’t visible in your transcript. If you’d like me to explain any part of this in more detail, just let me know—I’m here to help! Sources: Session recording and transcripts | LiveKit Docs | plugins/openai/src/realtime/api_proto.ts | protobufs/agent/livekit_agent_session.proto
b
I'm thinking about this part turn_ctx.add_message(role="assistant", content=warning_msg) which ChatRole should I use?
t
When you're adding internal messages to the assistant context, it's best to use the
assistant
role. This role is thoughtfully designed for responses or internal thoughts that the assistant generates. For example:
Copy code
turn_ctx.add_message(role="assistant", content=warning_msg)
If you'd like, I’m here to help with any more details or examples you might need—just let me know! Sources: agents/src/llm/llm.ts | Pipeline nodes and hooks | LiveKit docs | livekit-agents/livekit/agents/llm/_provider_format/mistralai.py | packages/basics/src/listen-and-respond.ts | SIP participant | LiveKit docs