can I send messages to agent mid call with the jav...
# ask-ai
d
can I send messages to agent mid call with the javascript sdk
t
Thanks for reaching out, Theo. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! You can totally send messages to an agent mid-call using the JavaScript SDK in LiveKit. How you do it depends on the type of agent you're working with: • For MultimodalAgents (like the OpenAI Realtime API): Just use the
session.conversation.item.create()
method to pop messages right into the conversation. Here’s a quick example:
Copy code
session.conversation.item.create(
  llm.ChatMessage.create({
    role: llm.ChatRole.USER,
    text: 'Say "How can I help you today?"',
  }),
);
• For VoicePipelineAgent: You’ll want to use the
addExtraChatMessage()
method inside
AgentCallContext
to add a message to the LLM stream. Check this out:
Copy code
const context = new AgentCallContext(agent, llmStream);
context.addExtraChatMessage(
  llm.ChatMessage.create({
    role: llm.ChatRole.USER,
    text: 'This is a mid-call injected message.',
  })
);
Also, if you want to have real-time control from a client, you can use Remote Procedure Calls (RPC). This lets you call a method in the agent to inject messages on the fly. If you'd like, I’m happy to share more details or examples—just let me know! Sources: agents/src/multimodal/multimodal_agent.ts | AgentCallContext | LiveKit Agents | ChatMessage | LiveKit Agents | README.md
d
what about with the api not the sdk
t
If you want to send messages to an agent mid-call using the API instead of the SDK, you can use the RoomService.SendData RPC method from the LiveKit Server API. This lets you send data messages straight into a LiveKit room, and the agent will be able to pick them up. Here’s a quick example in Python:
Copy code
from livekit import api

lk_api = api.LiveKitAPI(
    url="your-livekit-url",
    api_key="your-key",
    api_secret="your-secret"
)

# Send a text message to everyone in the room
await lk_api.room.send_data(
    room="your-room-name",
    data="Hello Agent!",
    topic="lk.chat",  # Optional: pick a topic the agent listens to
    reliable=True     # Or False if you want faster, but less guaranteed delivery
)

await lk_api.aclose()
When you send messages through the API, you can specify a topic like
"lk.chat"
that the agent is tuned into. Plus, you get to choose between reliable delivery (messages arrive in order) or lossy delivery (faster but no guarantees). If you want me to walk you through it or have any questions, just let me know—I’m here to help! Sources: Data messages | LiveKit Docs | VoiceAgent/Chat/Send/LocalMessageSender.swift | AgentCallContext | LiveKit Agents