is there a way to commit to the LLM chat context t...
# ask-ai
w
is there a way to commit to the LLM chat context that the agent was interrupted? My agent gets confused when it thinks it said a full LLM completion to the user, but really that completion was interrupted halfway through and half of it wasn't said.
t
Thanks for reaching out, Drew. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit has some handy ways to manage interrupted LLM chat completions and keep the context spot-on: • If something gets interrupted, the system grabs the partially spoken text and updates the chat context with the
interrupted=True
flag. This way, incomplete responses aren’t mistaken for finished ones. • For real-time models, interruptions are handled right on the server side by cutting off the message at the playback point and marking it as interrupted in the context. • You can also stop speech programmatically using the
interrupt()
method, which makes sure the context is updated before moving forward. These features help the LLM stay aware of any incomplete messages and respond just right. If you want, I’m happy to share more details or examples! Sources: livekit-agents/livekit/agents/voice/speech_handle.py
w
in non-realtime model, can the LLM know exactly where in a completion it was interrupted (i.e know how many of that completion it actually verbalized before getting interrupted)?
t
Hey there! Just wanted to share a bit about how LiveKit handles interruptions in non-realtime models. It uses the
playback_position
property to figure out when an interruption happened—like how many seconds into the response it occurred. This way, it can trim the conversation history to include only the part that was actually spoken before the interruption. That said, non-realtime models don’t sync context down to the millisecond or roll back exactly to the interruption point like realtime models do. The timing info helps keep things accurate, but it’s not quite as precise as what you’d get with realtime syncing. If you want me to walk you through it more or give some examples, just let me know—I’m happy to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Turn detection and interruptions | LiveKit Docs
w
what timing info are you referring to?
t
Here’s the timing info I mentioned: • Playback Position: The
playback_position
property in events like
playback_finished
shows how many seconds of the agent's audio played before it got interrupted. • Interruption Metrics: Things like
clientAudioSubscriberInterruptionCount
(how many interruptions happened) and
clientAudioSubscriberTotalInterruptionDuration
(how long those interruptions lasted) give you extra details. • Event Timestamps: Events such as
agent_state_changed
and
playback_finished
come with
created_at
timestamps, which help you figure out exactly when interruptions happened compared to the session start. These details should help you understand when and how interruptions occur. If you want me to walk you through it more or share some examples, just let me know—I’m happy to help! Sources: Sources/LiveKit/Protos/livekit_metrics.pb.swift | protobufs/livekit_metrics.proto | livekit-agents/livekit/agents/voice/agent_activity.py | Turn detection and interruptions | LiveKit Docs
w
what does use_tts_aligned_transcript do? does it solve the problem of the LLM not knowing it was interrupted during its previous speech?
t
I'm glad you asked about the
use_tts_aligned_transcript
setting! This feature helps align the transcription perfectly with the actual speech timing from the TTS engine. It makes sure that what you see in the transcription matches the audio playback, supporting word-level alignment for providers like Cartesia and ElevenLabs, and sentence-level alignment for others. This really helps improve the accuracy of the transcriptions when displayed. Now, regarding interruptions, this setting doesn’t directly notify the LLM about them. Instead, LiveKit takes care of interruptions by cutting off the ongoing message and marking it as
interrupted
in the chat context. This way, the LLM knows an interruption happened and can adjust its responses accordingly. If you'd like, I’m here to provide more details or examples—just let me know! Sources: Text and transcriptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py | Cartesia TTS integration guide | LiveKit Docs | ElevenLabs TTS integration guide | LiveKit Docs
w
yea, but even if the LLM sees that a completion was interrupted, it does not know where it was interrupted. How can I make the chat context include where the LLM was interrupted in a past completion
t
I completely understand how interruptions during an LLM completion can be a bit tricky. When this happens, LiveKit thoughtfully captures the exact part of the message that was spoken before the interruption. It uses the
synchronizedTranscript
from the playback event, which holds the text right up to where the interruption took place. Then, this shortened transcript is added to the chat context with an
interrupted=true
flag. This helps the LLM recognize both that an interruption occurred and the precise moment it happened, so it can tailor its responses more effectively. If you have any questions or want me to walk you through this in more detail, please don’t hesitate to ask—I’m here to help! Sources: agents/src/voice/agent_activity.ts | livekit-agents/livekit/agents/voice/agent_activity.py
w
explain to me the Content object, and the Part object
t
In LiveKit, the Content and Part objects play unique and important roles in managing multimodal interactions and LLM responses, and I’d love to help you understand them better: • Content Object: This represents the payload of a message and supports different types such as:Each message can include a
content
array with multiple Content objects, which allows for rich multimodal inputs and outputs. ◦ `InputTextContent`: Text input from the user. ◦ `InputAudioContent`: Audio input from the user. ◦ `TextContent`: Text output from the assistant. ◦ `AudioContent`: Audio output from the assistant, including a transcript. • Part Object: This is used in a couple of ways: ◦ ContentPart: Helps manage real-time updates as a message is being generated, showing text or audio parts as they come in. ◦ types.Part: Represents segments of the LLM’s output, including text or function calls, helping to organize and process the responses. These objects really help keep the interactions dynamic and real-time in LiveKit. If you’d like, I’m here to walk you through more details or provide examples—just let me know! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/llm.py | agents/src/llm/__snapshots__/chat_context.test.ts.snap
w
explain to me the flow of LLM output to the chat context. Include how interruptions are handled
t
Let me walk you through how the flow of LLM output to the chat context works in LiveKit, including how interruptions are handled. I want to make sure you feel comfortable with each step: 1. LLM Generation: The LLM creates responses in a streaming way. As it generates text, that text is sent to transcription, and if text-to-speech (TTS) is enabled, the audio is synthesized simultaneously. 2. Real-time Output: Both audio and text are played or shown to you in real-time. The system keeps an eye on playback events to know exactly what has been delivered. 3. Context Update: When the response finishes, a
ChatMessage
containing the full content is added to the chat context. 4. Interruption Detection: If you interrupt—whether by speaking or typing—or if a programmatic trigger occurs, the system immediately cancels tasks like TTS and LLM generation to respect your input. 5. Partial Message Handling: If the assistant is interrupted mid-response, it captures the part that was spoken using the
synchronizedTranscript
from playback events. This partial message is then added to the chat context with
interrupted=true
, so nothing gets lost. 6. Truncation for Realtime Models: For realtime models, the server-side message is trimmed to match the actual playback position, keeping everything consistent. 7. State Update: After an interruption, the agent smoothly switches from "speaking" to "listening," ready to hear your next input. This approach helps make sure the chat context truly reflects what you experienced, even if interruptions happen. If you’d like me to clarify anything or provide examples, just let me know—I’m here to help! Sources: Bringing AI avatars to voice agents | VoicePipelineAgent | LiveKit Docs | tests/test_agent_session.py | agents/src/voice/agent_activity.ts