I have self hosted livekit, write a simple agent c...
# ask-ai
d
I have self hosted livekit, write a simple agent code that i can use which will join the agents-playground.livekit.io
t
Thanks for reaching out, Harsha. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to write an agent code that joins a LiveKit room on your self-hosted instance, here’s a simple step-by-step guide to help you out: 1. Define the Agent Entrypoint: Start by creating an
entrypoint
function that connects your agent to the room. Here’s a quick example in Python:
Copy code
async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()
    # Add your custom agent logic here
1. Configure the Worker: Next, register your agent with the LiveKit server using
WorkerOptions
. Here’s how you can do it in Python:
Copy code
if __name__ == "__main__":
    agents.cli.run_app(agents.WorkerOptions(entrypoint_fnc=entrypoint))
1. Authentication and Tokens: You’ll need to generate a JWT token with these grants to keep things secure: •
agent: true
or
kind: "agent"
•
roomJoin: true
• Room name • API key and secret from your environment variables
Copy code
token = (
    api.AccessToken()
    .with_identity("agent_identity")
    .with_kind("agent")
    .with_grants(api.VideoGrants(room_join=True, room=room_name, agent=True))
    .to_jwt()
)
1. Dispatch the Agent: Finally, you can either let the worker join every new room automatically or explicitly dispatch it using
AgentDispatchClient
. Here’s an example in Python:
Copy code
dispatch = await lkapi.agent_dispatch.create_dispatch(
    api.CreateAgentDispatchRequest(agent_name="test-agent", room=room_name)
)
Just a quick reminder to double-check that your environment variables (
LIVEKIT_URL
,
LIVEKIT_API_KEY
,
LIVEKIT_API_SECRET
) are set up correctly. If you hit any bumps or have questions, feel free to reach out—I’m here to help! Sources: agent/main.py | OpenAI and LiveKit partner to turn Advanced Voice into an API | livekit-agents/livekit/agents/jupyter.py | examples/agent-dispatch/index.ts | examples/agent_dispatch.py
d
where can i find the room id?
t
Hey there! You can grab the room ID (also called the server-assigned room SID) right after you connect to the room. Here’s a quick rundown on how to find it using different SDKs: • JavaScript/TypeScript:
Copy code
const room = new Room();
await room.connect(wsUrl, token);
const roomSid = await room.getSid();
console.log('Room SID:', roomSid);
• Python:
Copy code
room = Room()
await room.connect(url, token)
room_sid = await room.sid
print(f"Room SID: {room_sid}")
• Server API: You can also check out active rooms and their SIDs using the LiveKit Server API. Here’s an example in Python:
Copy code
from livekit import api

lkapi = api.LiveKitAPI()
rooms = await lkapi.room.list_rooms()
for room in rooms.rooms:
    print(f"Name: {room.name}, SID: {room.sid}")
If you want a hand with retrieving the room ID or getting your setup just right, feel free to ask—I’m here to help! Sources: Connecting to LiveKit | LiveKit Docs | Connecting to LiveKit | LiveKit Docs | src/room/Room.ts | livekit-rtc/livekit/rtc/room.py | Sources/LiveKit/Core/Room.swift
d
i get this error - from livekit import agents, api, lkapi ImportError: cannot import name 'lkapi' from 'livekit' (unknown location)
t
Hey there! The error happens because
lkapi
isn’t actually something you import directly from the
livekit
module. Instead,
lkapi
is usually just a variable name for an instance of the
LiveKitAPI
class. No worries though — here’s a simple way to fix it: 1. First, make sure you have the right package installed:
Copy code
pip install livekit-api
1. Then, use the correct import and set it up like this:
Copy code
from livekit import api

lkapi = api.LiveKitAPI(
    url="https://your-project.livekit.cloud",
    api_key="your-api-key",
    api_secret="your-api-secret"
)

# For example, to create a room:
room_info = await lkapi.room.create_room(api.CreateRoomRequest(name="my-room"))
print(room_info)

await lkapi.aclose()
1. Optional tip: Use environment variables to keep things neat:
Copy code
export LIVEKIT_API_KEY="your-api-key"
export LIVEKIT_API_SECRET="your-secret"
export LIVEKIT_URL="https://your-project.livekit.cloud"
Copy code
lkapi = api.LiveKitAPI()  # This will automatically pick up those environment variables
Just a heads-up: don’t try importing
lkapi
directly since it’s not part of the module itself. If you get stuck or want me to walk you through anything else, just give me a shout — I’m happy to help! Sources: examples/agent_dispatch.py | Still Have Questions