happy-diamond-58115
09/04/2025, 3:49 AMcreateSipParticipant: 1.135s
createAgent: 0.085ms
waitForParticipant: 0.096ms
Connecting to OpenAI Realtime API at wss://api.openai.com/v1/realtime?model=gpt-realtime
startAgent: 7.066s
sessionCreate: 0.422ms
sessionResponseCreate: 0.058ms
startAgent is the step where connection to OpenAI Realtime API is established and it takes a long time.
Here's my initialization code:
async function setupAgent(ctx: JobContext) {
await ctx.connect();
console.log(`Connected to room ${ctx.room.name}`);
const userIdentity = `phone_user_${Date.now()}`;
const phoneNumber = ctx.job.metadata;
console.log(`Dialing ${phoneNumber}`);
// Start SIP call first (but don't wait for answer)
console.time('createSipParticipant');
await sipClient.createSipParticipant(
process.env.LIVEKIT_SIP_TRUNK_ID!,
phoneNumber,
ctx.room.name!,
{
participantIdentity: userIdentity,
krispEnabled: true,
waitUntilAnswered: false, // Don't wait for answer
},
);
console.timeEnd('createSipParticipant');
// Wait for participant to join (this provides the audio context)
console.time('waitForParticipant');
const participant = await ctx.waitForParticipant(userIdentity);
console.timeEnd('waitForParticipant');
// Create agent (fast)
console.time('createAgent');
const agent = new multimodal.MultimodalAgent({ model });
console.timeEnd('createAgent');
// NOW start the agent with the participant (this is where the delay happens)
console.time('startAgent');
const session = await agent
.start(ctx.room, participant) // Pass the actual participant
.then((session) => session as openai.realtime.RealtimeSession);
console.timeEnd('startAgent');
// Create greeting immediately after session starts
console.time('sessionCreate');
session.conversation.item.create(
llm.ChatMessage.create({
role: llm.ChatRole.ASSISTANT,
text: 'Hello, how are you?',
}),
);
console.timeEnd('sessionCreate');
console.time('sessionResponseCreate');
session.response.create();
console.timeEnd('sessionResponseCreate');
console.log(`starting assistant example agent for ${participant.identity}`);
}
export default defineAgent({
entry: setupAgent,
});
Any way to make it faster?
P.S. I'm using v0.x of the agent-js librarytall-airline-16756
09/04/2025, 3:49 AMtall-airline-16756
09/04/2025, 3:51 AMstartAgent function is probably happening because of cold starts or some heavy initialization tasks. Don’t worry, there are a few ways we can make things run smoother:
• Use prewarm: Try loading those resource-heavy things (like models) right when the worker starts using the prewarm function. This way, you won’t have to wait for them every time a job runs.
• Control idle processes: You can set up the worker to keep a few idle processes ready to go using numIdleProcesses in WorkerOptions. For production, having at least 3 is a good rule of thumb.
• Perform network setup early: Do any network-related setup (like API connections) before you call ctx.connect(). This helps avoid delays later on.
• Enable production mode: Running the agent with start instead of dev turns on production optimizations, like better process reuse, which can speed things up.
• Consider migrating to v1.x: The v1.x library comes with improved lifecycle management and better performance. If you’re interested, the LiveKit docs have a handy migration guide from v0.x to v1.x.
If you’ve already tried these or want a hand figuring out what’s going on, just share a bit about your setup—like if you’re using prewarm, running in production mode, or noticing cold starts—and I’ll be happy to help! Sources: Deploying to production | LiveKit Docs | Agents Overview | LiveKit Docs | Worker options | LiveKit Docs | agents/src/worker.ts | Agents v0.x migration guide - Node.js | LiveKit Docshappy-diamond-58115
09/04/2025, 3:52 AMtall-airline-16756
09/04/2025, 3:52 AMentry: async (ctx: JobContext) => {
// Pre-initialize external dependencies
const apiClient = new ExternalAPIClient(process.env.API_KEY);
await apiClient.initialize(); // Do any setup needed here
// Now connect to the LiveKit room
await ctx.connect();
console.log(`Connected to room ${ctx.room.name}`);
// Continue with the rest of your logic
}
By handling the network setup before ctx.connect(), you’ll cut down on waiting time during agent initialization. If you want, I’m here to help you tailor this to your specific situation—just let me know!happy-diamond-58115
09/04/2025, 3:55 AMtall-airline-16756
09/04/2025, 3:55 AMprewarm function is a handy way to load heavy resources or do setup work right when the worker starts up, so everything’s ready to go before the agent kicks in. This helps cut down on any wait time during the entry function.
Here’s a simple way to use prewarm to boost your performance:
1. Set up a prewarm function: This is where you load things like models, API clients, or other stuff you’ll use across different jobs.
prewarm: async (proc: JobProcess) => {
// Load a model or initialize a client
proc.userData.vad = await silero.VAD.load();
proc.userData.apiClient = new ExternalAPIClient(process.env.API_KEY);
}
2. Grab those preloaded resources in the entry function: Just pull them from proc.userData so you don’t have to load them again.
`entry: async (ctx: JobContext) => {
const vad = ctx.proc.userData.vad as silero.VAD;
const apiClient = ctx.proc.userData.apiClient as ExternalAPIClient;
// Use the preloaded resources
await ctx.connect();
console.log(Connected to room ${ctx.room.name});
}`
3. Make sure to deploy with prewarm enabled: This ensures your worker runs the prewarm function when it starts up.
By loading resources upfront, you won’t have to repeat these steps for every job, which really speeds things up during the startAgent phase. If you want, I’m here to help you get this set up smoothly!