<#C088ZNU7QQ5> i am using realtime model of google...
# ask-ai
s
#C088ZNU7QQ5 i am using realtime model of google. i want to collect the user complelted turn to record the data like how many questions did the user ask. is there any way so that i can collect those?
t
Thanks for reaching out, Karthik. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you’re asking how to count user “completed turns” (for example, how many questions a user asked) when using Google’s realtime model in LiveKit Agents. You can do this by listening for the user_input_transcribed event and incrementing a counter when a final transcription is emitted. Quick summary of the approach • Listen for the
user_input_transcribed
event on the AgentSession. • Only count events where the transcription is final (i.e., the user finished their turn). • Increment and persist a counter (in memory, DB, or analytics) each time a final transcription appears. • Optionally record the transcript text, timestamp, and any metadata (e.g., session id, user id, question type). Example (Python)
Copy code
# Example handler sketch
user_question_count = 0

@session.on("user_input_transcribed")
def on_user_input_transcribed(event):
    global user_question_count
    if event.is_final:
        user_question_count += 1
        # persist or log as needed
        <http://logger.info|logger.info>(f"User question #{user_question_count}: {event.transcript}")
        # e.g., save to DB or analytics service
Implementation notes and options • Ensure transcription is enabled for your RealtimeModel (transcription is enabled by default). • Define what counts as a “question”: you can simply count every final turn, or apply heuristics (e.g., check if transcript ends with a question mark, run a quick intent classifier, or check for question words). • Persistence: for short sessions you can keep the counter in memory. For long-term tracking or cross-session metrics, write each increment or completed-turn record to a database or analytics pipeline. • Additional metadata: consider storing session id, user id, timestamp, and the transcript text for later analysis (e.g., to detect follow-up questions). Follow-up questions to make this concrete • Do you want to count every final user turn, or only turns that are questions (e.g., ends with a question mark or classified as a question)? • Where would you like to store these counts (in-memory, your database, or an analytics service)? • Which SDK/language are you using in your agent (so I can provide a more tailored code snippet)? If you tell me which of the above you prefer, I’ll provide a short, specific code example and a recommended persistence pattern. Sources: Gemini Live API plugin | LiveKit Documentation | OpenAI Realtime API plugin guide | LiveKit Documentation | Pipeline nodes and hooks | LiveKit Documentation
s
i am using python. what i want is when user ask questions and the agent replies, then i want this to count as 1 so that based on this i can keep track of the questions asked.
t
Thanks — I’ve got what you want: using Python, count one interaction when a user asks a question and the agent finishes replying. Below is a simple, reliable pattern you can use with LiveKit Agents. Summary of the approach • Listen for the
user_input_transcribed
event and only react to final transcripts. • When a final transcript arrives, generate the agent reply and wait for the reply to finish playing. • Increment the interaction counter only after the agent reply completes. Minimal Python example
Copy code
import asyncio
from livekit.agents import AgentSession

# Create your session (example placeholders for STT/LLM/TTS/VAD)
session = AgentSession(
    # supply your configured stt/llm/tts/vad plugins here
)

interaction_count = 0  # counts user question -> agent reply interactions

async def handle_user_transcript(event):
    global interaction_count

    # Only count when transcript is final (user finished speaking)
    if not event.is_final:
        return

    user_text = event.transcript

    # Ask the agent to generate a reply and get a handle that resolves when playback completes
    # Depending on your SDK, this may be session.generate_reply or session.reply; adapt accordingly
    handle = await session.generate_reply(user_input=user_text)

    # Wait for the agent's reply (speech/playback) to finish
    await handle

    # Now count this as one completed interaction
    interaction_count += 1
    print(f"Completed interactions: {interaction_count}")

# Register the event listener
@session.on("user_input_transcribed")
def on_user_input_transcribed(event):
    # Run async handler without blocking the event loop
    asyncio.create_task(handle_user_transcript(event))
Implementation notes and suggestions • Ensure transcription is enabled so
user_input_transcribed
fires (transcription is enabled by default). • Decide what “question” means for you: ◦ Simple: count every final user turn like the snippet above. ◦ Strict: run a lightweight rule (ends with “?” or contains question words) or a small intent classifier and only call generate_reply/count when it’s a question. • Persistence: ◦ For short sessions, in-memory
interaction_count
may be fine. ◦ For long-term analytics or cross-session totals, write each increment (or the full event record: transcript, timestamp, session_id, user_id) to a database or analytics service inside the handler. • Concurrency: ◦ Using
asyncio.create_task
avoids blocking the event loop so multiple user turns or other events remain responsive. • Edge cases: ◦ If your agent can interrupt or produce multiple reply segments, ensure your chosen API handle resolves only after the whole reply is complete. ◦ If a user asks multiple short questions in one turn, decide whether that counts as 1 or multiple — you’d need extra NLP logic to split/count questions inside the transcript. If you’d like, I can: • Provide a version that only counts transcripts recognized as questions (with a short heuristic). • Show how to persist counts to a specific database (Postgres, Redis, etc.) — tell me which you use. • Adapt the example to match the exact LiveKit Python SDK method names you’re using if you share those. Sources: Interrupt User | LiveKit Documentation