how can I connect an agent as a participant
# ask-ai
a
how can I connect an agent as a participant
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! Could you please help me understand a bit more about what you mean by connecting an agent as a participant? Are you looking to add a user to a LiveKit room, or is this about a particular feature or integration? I'm here to help you through this!
a
I am trying to use a python sdk to connect to my agent - another agent to join as a user/particpant
t
To connect an agent as a participant using the Python SDK, it really depends on the type of participant you're working with: • SIP Participants: You can add SIP participants (like phone calls) directly to a room using the
create_sip_participant
method. Just a heads-up, this requires having an outbound SIP trunk set up in LiveKit. If you'd like, I’m happy to walk you through the detailed steps for this! • Standard Participants: For web or mobile users, the way to go is generating a join token on your server, which the client then uses to connect to the room. This is the best way to add standard participants programmatically. Could you let me know which type of participant you’re aiming to add? Also, if you want, I’m here to help with token generation or setting up SIP configuration—just say the word! Sources: livekit-agents/livekit/agents/job.py | Docs::Telephony | README.md | telephony/sip_lifecycle.py
a
how to generate a join token?
t
To generate a join token using the Python SDK, let's walk through these steps together: 1. Set Up Environment Variables: Make sure you have your
LIVEKIT_API_KEY
and
LIVEKIT_API_SECRET
set up in your environment or directly in your code. 2. Initialize the AccessToken: Create a token object using the
api.AccessToken
class. 3. Set Participant Identity: Use
.with_identity(identity)
to give your participant a unique identifier. 4. Define Permissions: Specify what the participant can do, like joining a room, publishing, and subscribing, using
api.VideoGrants
. 5. Set Token Expiration: Optionally, you can decide how long the token should last with
.with_ttl()
. 6. Generate the Token: Finally, call
.to_jwt()
to create your token. Here’s a simple example to guide you:
Copy code
from livekit import api
import datetime

def generate_token(room_name, identity):
    token = (
        api.AccessToken("your-api-key", "your-api-secret")
        .with_identity(identity)
        .with_grants(
            api.VideoGrants(
                room_join=True,
                room=room_name,
                can_publish=True,
                can_subscribe=True
            )
        )
        .with_ttl(datetime.timedelta(minutes=15))
        .to_jwt()
    )
    return token

print(generate_token("my-room", "user-123"))
If you have any questions or need a hand with any part of this, just let me know—I’m here to help! Sources: Authentication | LiveKit Docs | Docs::Agents | Generating tokens | LiveKit Docs | auth.py | livekit-api/livekit/api/access_token.py
a
how to use this token after that?
t
To connect a participant to a LiveKit room using the generated join token, here’s a simple guide to help you through the process: 1. Install the LiveKit Python SDK: First, make sure the SDK is installed in your environment. 2. Create a Room Instance: Use
rtc.Room()
to create your room object. 3. Connect to the Room: Call the
connect()
method with your LiveKit server’s WebSocket URL and the token you generated. 4. Handle Events: It’s helpful to set up event listeners for connection events like
connected
and
disconnected
to stay informed about the connection status. Here’s a friendly example to illustrate:
Copy code
import asyncio
from livekit import rtc

async def connect_to_room():
    room = rtc.Room()

    @room.on("connected")
    def on_connected():
        print("Connected to the room!")

    @room.on("disconnected")
    def on_disconnected(reason):
        print(f"Disconnected: {reason}")

    token = "your-generated-token"
    url = "wss://your-livekit-server-url"

    try:
        await room.connect(url, token)
    except rtc.ConnectError as e:
        print(f"Failed to connect: {e}")

asyncio.run(connect_to_room())
If you have any questions or need a hand with any part of this, please don’t hesitate to ask—I’m here to help! Sources: examples/agent_dispatch.py | Connecting to LiveKit | LiveKit Docs | Authentication | LiveKit Docs | Generating tokens | LiveKit Docs