How to access room_name (ctx.room.name) from agent...
# ask-ai
c
How to access room_name (ctx.room.name) from agentsession ?
t
Thanks for reaching out, Purna. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to read
room_name
(i.e.,
ctx.room.name
) from an
AgentSession
. You can access it from the
JobContext
passed to your agent entrypoint and then pass it into the session/agent so your agent logic can use it. Which language are you using (Node.js or Python)? I can give the exact snippet — quick examples below; tell me which one you want. Node.js example (use in your entry function):
Copy code
export default defineAgent({
  entry: async (ctx: JobContext) => {
    console.log('Room name:', ctx.room.name);

    const session = new voice.AgentSession({ /* stt/llm/etc */ });

    await session.start({
      agent: new voice.Agent({ instructions: '...' }),
      room: ctx.room,            // pass the Room
    });

    // or pass the name into your agent:
    await session.start({
      agent: new MyAgent(ctx.room.name),
      room: ctx.room,
    });
  },
});
Python example (use in your rtc_session handler):
Copy code
@server.rtc_session()
async def my_agent(ctx: JobContext):
    print(f"Room name: {ctx.room.name}")

    session = AgentSession(/* stt, llm, etc. */)

    await session.start(
        agent=MyAgent(ctx.room.name),
        room=ctx.room,
    )
If you need
ctx.room.name
inside agent methods (e.g.,
onEnter
), initialize the agent with the room name (as shown) or store `ctx`/`ctx.room` on the session/agent when starting it. Want the full code for your specific agent class or a pattern for accessing it inside tools/handlers? Sources: Job lifecycle | LiveKit docs | Agents v0.x migration guide - Node.js | LiveKit Docs | Authentication | LiveKit docs | Agents and handoffs | LiveKit docs | Agents (Node.js) | livekit/livekit_composite | DeepWiki