fancy-planet-81885
11/05/2025, 9:59 PM/**
* Generate a LiveKit access token for a user to join an agent call
*/
async generateUserToken(
agentCode: string,
userName?: string,
): Promise<GenerateUserTokenResult> {
// Create unique room name: {agentCode}-{timestamp}-{randomId}
const timestamp = Date.now();
const randomId = nanoid(8);
const roomName = `${agentCode}-${timestamp}-${randomId}`;
// Generate user identity
const userIdentity = userName || `user-${nanoid(8)}`;
const agentName = `nebula-${this.agentEnv}`;
this.logger.log(
{ agentCode, roomName, userIdentity, agentEnv: this.agentEnv },
'Generating LiveKit token',
);
// Create access token
// NOTE: We do NOT pass the userName to avoid leaking it to the agent context
// The agent should learn the user's name through conversation, not from metadata
const token = new AccessToken(this.apiKey, this.apiSecret, {
identity: userIdentity,
// name: userName, // Commented out to prevent name leak
});
// Add video grants for room join
token.addGrant({
room: roomName,
roomJoin: true,
canPublish: true,
canSubscribe: true,
});
// Set participant metadata
token.metadata = JSON.stringify({
agentCode,
});
token.attributes = {
agentCode,
};
// Configure room to dispatch the agent when participant joins
token.roomConfig = new RoomConfiguration({
agents: [
new RoomAgentDispatch({
agentName: agentName,
metadata: JSON.stringify({ agentCode }),
}),
],
});
const jwt = await token.toJwt();
this.logger.log(
{ roomName, agentName },
'LiveKit token generated successfully with agent dispatch',
);
return {
token: jwt,
serverUrl: this.livekitUrl,
roomName,
};
}
Is there a way to make the agent join the room in advance when we generate the token instead of when the user joins to the room ? So it starts joining while the user is also joiningtall-airline-16756
11/05/2025, 9:59 PMtall-airline-16756
11/05/2025, 10:00 PMroomConfig and RoomAgentDispatch in the token. That configuration dispatches the agent when the room is created. To have the agent join concurrently with the user, create/initialize the room and dispatch the agent at token generation time (i.e., before the client calls join) using explicit agent dispatch. This lets the agent spin up while the user is connecting.
If you prefer a deferred approach, you can also trigger the agent on a participant-joined event via a server webhook.
For details, see:
• Token room configuration and agent dispatch docs
• Frontend timing/dispatch guidance here Sources: Authentication | LiveKit Docs | Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki | Voice Assistant Applications | livekit/livekit_composite | DeepWiki | Web and mobile frontends | LiveKit Docs | Live Conversations with AI using ChatGPT and WebRTCfancy-planet-81885
11/05/2025, 10:01 PMtall-airline-16756
11/05/2025, 10:02 PMroomConfig (as you’re doing). Then, when the room is created, the agent is dispatched immediately, so it can start joining while the user connects. You don’t need a separate backend call; just return the token and let the client join.
If you want the agent to start even earlier, you can proactively create the room server-side before the client joins, using the same roomConfig, so dispatch starts right away.
Sources: Agent dispatch | LiveKit Docs | Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki | Voice Assistant Applications | livekit/livekit_composite | DeepWiki | Web and mobile frontends | LiveKit Docsfancy-planet-81885
11/05/2025, 10:02 PMtall-airline-16756
11/05/2025, 10:04 PMroomConfig with RoomAgentDispatch so the agent starts as the room is created when the client joins.
import express from 'express';
import { AccessToken } from 'livekit-server-sdk';
import { RoomConfiguration, RoomAgentDispatch } from '@livekit/protocol';
const app = express();
app.use(express.json());
app.post('/connection-details', async (req, res) => {
const roomName = req.body.roomName || `room-${Math.random().toString(36).slice(2, 8)}`;
const identity = req.body.identity || `user-${Math.random().toString(36).slice(2, 8)}`;
const agentName = req.body.agentName || 'my-agent';
const token = new AccessToken(process.env.LIVEKIT_API_KEY, process.env.LIVEKIT_API_SECRET, {
identity,
});
token.addGrant({
room: roomName,
roomJoin: true,
canPublish: true,
canSubscribe: true,
});
token.roomConfig = new RoomConfiguration({
agents: [
new RoomAgentDispatch({
agentName,
metadata: JSON.stringify({ source: 'token-gen' }),
}),
],
});
const jwt = await token.toJwt();
res.json({
serverUrl: process.env.LIVEKIT_URL,
roomName,
token: jwt,
});
});
app.listen(3000);
Would you like the same pattern adapted for Next.js API routes or multiple agents in one room?
Sources: Agent dispatch | LiveKit Docs | Authentication | LiveKit Docs | Server-Side SDKs & Authentication | livekit/livekit_composite | DeepWiki | Generating tokens | LiveKit docs | Voice Assistant Applications | livekit/livekit_composite | DeepWikifancy-planet-81885
11/05/2025, 10:05 PMtall-airline-16756
11/05/2025, 10:06 PMfancy-planet-81885
11/06/2025, 3:30 PM