in my if name =="_main_" block would it be possibl...
# ask-ai
w
in my if name =="_main_" block would it be possible to just let the AI agent enter an LK room called test-room?
t
Thanks for reaching out, Marius. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes — you can dispatch an AI agent to join a LiveKit room (for example, "test-room") from inside your
if __name__ == "__main__":
block. What to do: • Create an agent dispatch that targets
room="test-room"
using the LiveKit API. • Start your agent server so it accepts the dispatched job; your
@server.rtc_session()
entrypoint will be invoked with
ctx.room.name == "test-room"
. • The room will be created automatically if it doesn't exist. Minimal example (async):
Copy code
from livekit import api
from livekit.agents import cli, AgentServer
import asyncio

# assume server and your @server.rtc_session() handler are defined

async def dispatch_and_run():
    lkapi = api.LiveKitAPI()
    await lkapi.agent_dispatch.create_dispatch(
        api.CreateAgentDispatchRequest(agent_name="your-agent-name", room="test-room")
    )
    await lkapi.aclose()
    cli.run_app(server)

if __name__ == "__main__":
    asyncio.run(dispatch_and_run())
Would you like a version that: • uses a sync wrapper instead, or • includes token generation and SDK connection code inside the agent session handler? Sources: Agent dispatch | LiveKit docs
w
my # assume server and your @breezy-insurance-66772.rtc_session() handler are defined is not defined
t
Thanks — I see your
server
and
@server.rtc_session()
handler aren't defined. I can show a minimal example you can drop into your project to define an AgentServer and an RTC session entrypoint that will run when you dispatch the agent to "test-room". Quick clarifying question before I provide code: are you using the LiveKit Python agents package (the example earlier used
livekit.agents
), or are you working from a different SDK/language?