how do I determine if it's a user typed/transcribe...
# ask-ai
n
how do I determine if it's a user typed/transcribed message vs agent message using the python sdk?
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you figure out whether a message is user-typed or agent-generated using the Python SDK, you can use the
conversation_item_added
event. This event gives you a
ChatMessage
object that includes helpful metadata to identify where the message came from. Here are the key fields you’ll want to check: •
role
: This tells you who sent the message. If
role == "user"
, it means the message was typed by the user. If
role == "assistant"
, then it was generated by the agent. •
generated
: This is a boolean that shows if the message was created by an agent from user transcription. Here’s a quick example to illustrate:
Copy code
from livekit.agents import ConversationItemAddedEvent

@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
    msg = event.item
    if msg.role == "user":
        print("Message is user-typed.")
    elif msg.role == "assistant":
        print("Message is agent-generated.")
    print(f"Generated from transcription: {msg.generated}")
    print(f"Message ID: {msg.id}, Timestamp: {msg.timestamp}")
    print(f"Content: {msg.message}")
Using this method, you can confidently distinguish between messages from users and agents. If you have any questions or need further help, feel free to ask—I’m here to support you! Sources: Events and error handling | LiveKit Docs | livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | livekit-protocol/livekit/protocol/models.pyi | livekit-protocol/src/livekit.rs | Turn detection and interruptions | LiveKit Docs
n
how do I set roles for messages
t
I'd be happy to help you set roles for messages using the Python SDK! You can do this easily with the
add_message()
function inside a
ChatContext
. This lets you specify roles like
"user"
,
"assistant"
,
"system"
, or
"tool"
. Here's a quick example to guide you:
Copy code
from livekit.agents import ChatContext

async def on_user_turn_completed(self, turn_ctx: ChatContext):
    turn_ctx.add_message(
        role="assistant",
        content="Here is the response from the assistant."
    )
    await self.update_chat_ctx(turn_ctx)  # Persist the message in chat history
Some important things to keep in mind: • Use
add_message()
whenever you want to add messages with specific roles. • Don't forget to call
update_chat_ctx()
so your message gets saved in the chat history. • Roles like
"user"
,
"assistant"
, and
"system"
are usually supported, but just a heads-up—this might vary depending on your LLM integration. If you'd like me to walk you through more examples or if anything feels unclear, please feel free to ask. I'm here to help! Sources: Pipeline nodes and hooks | LiveKit Docs | models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja | Docs::Agents | Testing and evaluation | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs
n
if I'm chatting via text with a livekit agent, how much does it charge me?
t
Just to clarify, text-based chats with a LiveKit agent are counted within your agent session minutes. The cost is $0.01 per minute once you go beyond the free tier of 1,000 agent session minutes per month. This includes all agent sessions, even if they’re text-only. If you have any questions or want me to explain anything further, please don’t hesitate to ask—I’m here to help! Sources: Text and transcriptions | LiveKit Docs | Agents 0.x migration guide | LiveKit Docs | Text and transcriptions | LiveKit Docs | Billing | LiveKit Docs | LiveKit