freezing-jelly-85538
08/11/2025, 10:16 PMtall-airline-16756
08/11/2025, 10:16 PMtall-airline-16756
08/11/2025, 10:16 PMagent_name
in WorkerOptions
. This turns off automatic dispatch and gives you full control.
agents.cli.run_app(agents.WorkerOptions(
entrypoint_fnc=entrypoint,
agent_name="my-agent"
))
2. Dispatch via API: Next, use the AgentDispatchService
to send your agent to a room explicitly. Here’s how it looks:
Node.js:
`const { AgentDispatchClient } = require('livekit-server-sdk');
const agentDispatchClient = new AgentDispatchClient(process.env.LIVEKIT_URL);
const dispatch = await agentDispatchClient.createDispatch(
"my-room",
"my-agent",
{ metadata: '{"mydata": "myvalue"}' }
);`*Python:*
import asyncio
from livekit import api
lkapi = api.LiveKitAPI()
dispatch = await lkapi.agent_dispatch.create_dispatch(
api.CreateAgentDispatchRequest(
agent_name="test-agent",
room="my-room",
metadata="my_metadata"
)
)
3. Dispatch on Participant Connection: You can also set up a participant’s access token to automatically dispatch an agent when they join a room. Here’s an example:
Node.js:
`const { AccessToken, RoomConfiguration, RoomAgentDispatch } = require('livekit-server-sdk');
const at = new AccessToken();
at.identity = 'my-participant';
at.addGrant({ roomJoin: true, room: 'my-room' });
at.roomConfig = new RoomConfiguration({
agents: [
new RoomAgentDispatch({
agentName: 'test-agent',
metadata: '{"mydata": "myvalue"}'
})
]
});
const token = await at.toJwt();`*Python:*
from livekit.api import RoomAgentDispatch, RoomConfiguration
token = (
AccessToken()
.with_identity("my_participant")
.with_room_config(
RoomConfiguration(
agents=[
RoomAgentDispatch(agent_name="test-agent", metadata="my_metadata")
],
),
)
.to_jwt()
)
4. Job Metadata: Any metadata you pass during dispatch will be available in the agent’s JobContext
. This way, you can tailor your agent’s behavior based on different scenarios.
async def entrypoint(ctx: JobContext):
<http://logger.info|logger.info>(f"Job metadata: {ctx.job.metadata}")
if ctx.job.metadata == '{"type": "customer-support"}':
await handle_customer_support(ctx)
else:
await handle_default_case(ctx)
If you have any questions or need a hand with the implementation, just let me know — I’m happy to help!freezing-jelly-85538
08/11/2025, 10:22 PMtall-airline-16756
08/11/2025, 10:23 PMAgent
class to add your own custom behavior, and then make an instance of it inside an entrypoint
function. Here’s a simple way to get you started:
1. Extend the Agent
Class: Create a new class that inherits from Agent
and define how it should behave.
from livekit.agents import Agent
class DynamicAgent(Agent):
def __init__(self, instructions: str):
super().__init__(instructions=instructions)
2. Create an Instance of the Agent: Make your agent with the instructions you want it to follow.
agent_instance = DynamicAgent(
instructions="You are a helpful voice AI assistant."
)
3. Start the Agent Session: Use the entrypoint
function to kick off the agent session.
from livekit.agents import AgentSession
from livekit.plugins import openai, silero
async def entrypoint(ctx: JobContext):
session = AgentSession(
vad=silero.VAD.load(),
stt=openai.STT(),
llm=openai.LLM(),
tts=openai.TTS(),
)
await session.start(
agent=agent_instance,
room=ctx.room
)
This approach gives you the flexibility to define and spin up agents on the fly, which is super handy if you want to handle different scenarios or specific needs. If you have any questions or want me to walk you through it, just let me know—I’m here to help!
Sources: README.md