After my app connected to the livekit room, the ag...
# agents
b
After my app connected to the livekit room, the agent sometimes doesn’t join. any suggestion on how to debug?
Copy code
Token received successfully - Length: 329 chars
🎫 Access token received successfully
πŸ”„ LiveKit state changed to: LiveKitVoiceState.connected
🏠 Room state changed - Participants: 0
πŸ“Š Local participant: user
πŸ‘₯ No remote participants in room (waiting for agent...)
🏠 Room state changed - Participants: 0
πŸ“Š Local participant: user
πŸ‘₯ No remote participants in room (waiting for agent...)
🏠 Room state changed - Participants: 0
d
Hello @broad-lamp-30178 Could you provide some code snippet so we could help you ?
Did you awaited your participant before awaiting the session start ? Something in this order:
Copy code
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
participant = await ctx.wait_for_participant()

#define your AgentSession here
session = AgentSession(...)

# start the session
await session.start(agent=agent, room=ctx.room)
If you do intermediate asynchronous operation in between (for example calling an external API) be careful of race conditions which seems to be the cause at first sight but cannot be sure without proper look at the code πŸ™‚
b
@damp-belgium-53878 This is my agent code
Copy code
async def entrypoint(ctx: agents.JobContext):
    session = AgentSession(
        stt=deepgram.STT(model="nova-3", language="multi"),
        llm=openai.LLM(model="gpt-3.5-turbo"),
        tts=cartesia.TTS(model="sonic-2", voice="f786b574-daa5-4673-aa0c-cbe3e8534c02"),
        vad=silero.VAD.load(),
        turn_detection=MultilingualModel(),
    )

    await session.start(
        room=ctx.room,
        agent=Assistant(),
        room_input_options=RoomInputOptions(
            noise_cancellation=noise_cancellation.BVC(), 
        ),
    )
do you mean i need to add the following before the session starts in agent code?
Copy code
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
participant = await ctx.wait_for_participant()
d
I think you might want your agent to wait until the participants arrives so you'll probably have to add the participant await part if you are in a standard setup. This the setup provided in the documentation for single agent dispatch
I am sorry I was quite busy all day I didn't have time to give you a proper response. I noticed that they changed the way they handled the session apparently so I might have been wrong. This worked for me quite well with livekit-agents older versions but it seems to have changed in the doc as well
This is the latest basic agent example I could find in the repository: https://github.com/livekit/agents/blob/main/examples/voice_agents/basic_agent.py
It seems to be doing just like you did except the room_input_options is provided during the session.start() as a parameter
So I am not sure. Is your Assistant similar to the one in the example with an on_enter() method ?
b
ok, my agent works fine actually, just it take quite a few seconds (~5+s) to join. Is this typical?
d
@broad-lamp-30178 Haaaa sorry I did'nt understand your need. Yes that's typical if you do not to use the prewarm_fnc to preload and intitialize all locally runned models ( VAD for example ) especially if they are heavy.
I do not know about the Multilingual Model. Is it loaded locally or make usage of an API ( like openai API ) for example ? If it's a heavy model that runs locally you might want to add it in the prewarm_fnc as well. One example from the doc with the VAD.
Copy code
def prewarm_fnc(proc: JobProcess):
    # load silero weights and store to process userdata
    proc.userdata["vad"] = silero.VAD.load()


async def entrypoint(ctx: JobContext):
    # access the loaded silero instance
    vad: silero.VAD = ctx.proc.userdata["vad"]

opts = WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm_fnc)
So in your case you could use the process userdata directly into the agentSession definition after defining your prewarm_fnc:
Copy code
session = AgentSession(
        vad=ctx.proc.userdata["vad"],
...
b
@damp-belgium-53878 Thanks a lot for the help! I have added the prerwarm_fnc. Not sure how much it helps since i need to add some logging. But from what i see, there are still times that the agent takes very long time (5+s) to join. This plus the token server response delay makes the initialization latency too long. I will look more into it.
In case you have tips about the initial token server response latency, pls let me know. It is hosted on render. The first request after a long inactive period took the most time. Maybe the server was asleep.
d
Hello @broad-lamp-30178, I do not think it prevents you from adding logging ? If that takes a long time after a long inactive period then yes it might be: β€’ server related (in a serverless settings) β€’ process or thread related For process or thread related you could use the Worker option
Copy code
num_idle_process
parameter to keep the process/thread warm and avoid cold start. If you are in a serverless context then it's maybe because your server is going down after inactivity and the restart takes a bit of time
b
Just loop back.
num_idle_process
+ upgrade render server to a paid plan helps! πŸ˜‚
d
Hahaha Great! πŸ˜‰