is it possible to log the user input and realtime ...
# ask-ai
c
is it possible to log the user input and realtime model output in Livekit?
t
Thanks for reaching out, Meng. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can definitely log user input and real-time model output in LiveKit. Let me walk you through how you can do this: Logging User Input: • You can use the
user_input_transcribed
event to capture and log what the user says. This event gives you a transcript object that you can easily save to a file or database. • For instance, you might write the user input to a file along with timestamps by using event-driven handlers. Capturing Real-Time Model Output: • The model’s outputs, both text and audio, are streamed through specific channels like
text_ch
and
audio_ch
. These channels send partial text tokens and audio frames as they happen. • You can log these outputs to files, databases, or even external systems while they’re being generated. Metrics and Telemetry: • LiveKit also offers structured metrics for things like input/output token usage, latency, and overall performance through events such as
usageEvent
and
AgentSession
metrics. • Plus, if you want to get deeper insights, OpenTelemetry integration can help you trace the entire execution flow and keep an eye on performance. If you’d like, I’m here to provide more details or specific examples to help you implement this smoothly—just let me know! Sources: basics/interrupts_user.py | livekit-agents/livekit/agents/metrics/base.py | Logs, metrics, and telemetry | LiveKit Docs
c
show me the example code for logging user_input_transcribed and text_ch
t
I'm happy to share some example code snippets that show how you can log
user_input_transcribed
and `text_ch`: Logging `user_input_transcribed`:
Copy code
async def on_transcription(transcript: str):
    with open("user_speech_log.txt", "a") as f:
        timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
        f.write(f"{timestamp} {transcript}\n")
This function helps you save the transcribed user input into a file, along with a timestamp to keep everything organized. Logging
text_ch
(Model Output):
Copy code
async def log_model_output(gen):
    async for text in gen.text_ch:
        with open("model_output_log.txt", "a") as f:
            timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
            f.write(f"{timestamp} {text}\n")
This function captures the model’s real-time text output and logs it with timestamps, so you can easily track what’s happening. Please feel free to reach out if you'd like me to walk you through this further or provide more examples—I’m here to help!
c
where is the file stored?
t
Just so you know, the file is saved in the same directory where you run your script. So, if you’re running the script from the folder
/home/user/project
, you’ll find the files
user_speech_log.txt
and
model_output_log.txt
created right there in that folder. If you'd like to save these files somewhere else, no worries! You can simply provide an absolute path in the file name. For example:
Copy code
with open("/path/to/your/directory/user_speech_log.txt", "a") as f:
Please feel free to reach out if you'd like a hand with setting up a custom file path—I’m here to help!
c
my agent is deployed to LiveKit, how to find the file?
t
Hi there! For agents deployed to LiveKit, I wanted to share that log files aren’t stored directly on the platform. But don’t worry—you can easily access runtime logs using the LiveKit CLI or even forward them to external monitoring services for your convenience. Accessing Logs via LiveKit CLI: • If you’d like to stream runtime logs in real time, just use this command:
lk agent logs --id <agent-id> --log-type deploy
• And to check out the build logs for your deployed version, try:
lk agent logs --id <agent-id> --log-type build
Forwarding Logs to External Services: • You can set up integrations with services like Datadog, CloudWatch, Sentry, or New Relic by adding the right secrets (for example,
DATADOG_TOKEN
,
AWS_ACCESS_KEY_ID
). • Once that’s all set, your runtime logs will be forwarded automatically for easy long-term storage and analysis. If you’d like, I’m here to help you with setting up log forwarding or navigating the CLI—just let me know! Sources: Log collection | LiveKit Docs