i want to update llm instructions in realtime in_e...
# ask-ai
b
i want to update llm instructions in realtime in_enter from api.CreateAgentDispatchRequest metadata
t
Thanks for reaching out, Aleh. 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 update LLM instructions in real-time, just a heads-up: you can't use the
api.CreateAgentDispatchRequest
metadata field because it's static and only set when the agent dispatch is first created. But no worries! You can use the
update_instructions
method from the LiveKit SDKs to make dynamic updates on the fly. Python Implementation:
Copy code
# Assuming 'agent' is an instance of livekit.agents.voice.Agent
try:
    # Update the agent's instructions to a new value
    await agent.update_instructions("Your new instructions here.")
except llm.RealtimeError:
    # Handle potential errors from the LLM service
    logger.exception("failed to update the instructions")
JavaScript/TypeScript Implementation:
Copy code
// Create the RealtimeModel with initial configuration
const model = new livekit.RealtimeModel({
    apiKey: process.env.OPENAI_API_KEY,
    instructions: "Your initial instructions here."
});

// Create a realtime session for the conversation
const session = model.session();

// Update the instructions for the active session
await session.updateInstructions("Your new instructions here.");
These methods make it super easy to send real-time updates to the LLM's instructions while your session is active. If you have any questions or want me to walk you through it, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/llm/realtime.py | agents/src/llm/realtime.ts
b
how to get this instructions in agent runtime
t
Hey there! If you want to check out the LLM instructions while your agent is running, you can simply use the
instructions
property from the
Agent
class. This handy property gives you the main instructions that are steering the agent’s behavior. Here’s a quick example in Python:
Copy code
# Grab the instructions during runtime\ncurrent_instructions = agent.instructions\nprint(f\"Current instructions: {current_instructions}\")
And if you happen to update the instructions on the fly using
update_instructions()
, no worries—the
instructions
property will show the latest version right away. Feel free to ask if you want me to explain anything further or if you need a hand with something else! Sources: livekit-agents/livekit/agents/voice/agent_activity.py
b
not agent instructions, how can i get data from api.CreateAgentDispatchRequest
t
Hey there! To get data from
api.CreateAgentDispatchRequest
, you’ll want to look at the response object you get right after creating a dispatch. The
create_dispatch()
method gives you an
AgentDispatch
object that includes handy details like
id
,
agent_name
,
room
,
metadata
, and
state
. Here’s a quick Python example for you:
Copy code
from livekit import api\n\nasync def create_and_retrieve_dispatch():\n    lkapi = api.LiveKitAPI()\n    dispatch = await lkapi.agent_dispatch.create_dispatch(\n        api.CreateAgentDispatchRequest(\n            agent_name=\"test-agent\",\n            room=\"my-room\",\n            metadata='{\"user_id\": \"12345\"}'\n        )\n    )\n\n    # Now, let’s grab some info from the response\n    print(\"Dispatch ID:\", dispatch.id)\n    print(\"Agent Name:\", dispatch.agent_name)\n    print(\"Room:\", dispatch.room)\n    print(\"Metadata:\", dispatch.metadata)\n    print(\"Created At:\", dispatch.state.created_at if dispatch.state else None)
If you want to fetch dispatches later on, you can use
get_dispatch()
or
list_dispatch()
like this:
Copy code
# Get a specific dispatch by its ID\ndispatch = await lkapi.agent_dispatch.get_dispatch(dispatch_id=\"disp_abc123\", room_name=\"my-room\")\n\n# Or list all dispatches in a room\ndispatches = await lkapi.agent_dispatch.list_dispatch(room_name=\"my-room\")\nfor d in dispatches:\n    print(\"Dispatch:\", d.agent_name, d.id)
If you’d like me to walk you through other SDKs or need any more help, just give me a shout! Sources: Agent dispatch | LiveKit Docs | examples/agent_dispatch.py | packages/livekit-server-sdk/src/AgentDispatchClient.ts | agent_dispatch_client.go | lib/livekit/agent_dispatch_service_client.rb