how do Chatcontext work ? Like with every new user...
# ask-ai
v
how do Chatcontext work ? Like with every new user message do the token increase like this example prompt token : 100 so with every message 100 100 +2 100 + 2 +4
t
Thanks for reaching out, Ravi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you asked about how ChatContext in LiveKit manages conversation history! It works by adding new messages, function calls, or outputs to the context in a way that’s really thoughtful. Instead of counting tokens directly, it uses a size-based approach. When a new user message comes in, LiveKit tries to combine it with the previous one—if they’re from the same role—without going over a 1kB size limit. If that’s not possible, it simply adds a new message. The system also keeps things running smoothly by enforcing size and count limits, like a 40-message cap, using UTF-8 encoding checks and message count limits. It doesn’t track token counts explicitly but focuses on managing byte-size and message volume efficiently to keep everything balanced. If you’d like me to explain anything further or if you have a specific scenario you’re thinking about, please don’t hesitate to ask! I’m here to help. Sources: agents/src/llm/__snapshots__/chat_context.test.ts.snap | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Agents v0.x migration guide - Python | LiveKit Docs | Agents v0.x migration guide - Node.js | LiveKit Docs
v
How do chatcontext work ? do the livekit send a array or messages adding every new message or is it the llm who manages it ? like first api call to llm [prompt] 2nd call [prompt , user message] 3rd call [prompt , user , ai , user]
t
Thanks for reaching out! I’d love to help clarify how LiveKit handles the chat context. Essentially, LiveKit manages the chat context externally and sends an updated array of messages to the LLM with each API call. For example: • First call: [prompt] • Second call: [prompt, user message] • Third call: [prompt, user, AI, user] It’s important to know that the LLM itself doesn’t manage the chat context. Instead, LiveKit keeps track of the conversation history in a
ChatContext
object, updating it dynamically with new messages, function calls, or outputs. This full or processed context is then sent to the LLM for each inference request. I hope this helps clear things up! If you have any more questions, feel free to ask. Sources: agents/src/llm/chat_context.ts | Agents v0.x migration guide - Node.js | LiveKit Docs | livekit-agents/livekit/agents/inference/llm.py | Sending text | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs
v
so with every call to the LLM the Tokens for prompt are used again
t
In LiveKit, whether tokens for the prompt are reused with every call to the LLM depends on the
cache_prompt
setting: • When
cache_prompt
is enabled
(which is the default): Tokens from the shared part of the prompt are reused through a key-value (KV) cache. This helps avoid reprocessing common tokens and makes things faster. • When
cache_prompt
is disabled
: Tokens aren’t reused, so the entire prompt is processed from scratch each time. If you have any specific needs or want me to explain anything further, please feel free to ask—I’m here to help! Sources: tools/server/server.cpp | livekit-agents/livekit/agents/metrics/usage_collector.py | Logs, metrics, and telemetry | LiveKit Docs
v
how do this cache prompt work ?
t
I'm happy to explain how the
cache_prompt
feature in LiveKit works! It’s designed to make things faster by storing and reusing tokens from earlier prompts, so you don’t have to reprocess the entire prompt every time. Here’s a simple breakdown: • It finds the longest common prefix of tokens between your current prompt and what’s already cached. • Then, it only processes the new or different tokens, while reusing the cached key-value (KV) state for the shared parts. • This really helps speed up response times, especially when working with long prompts or repeated questions. You can also manage the cache by saving, restoring, or erasing the cache state. For instance, clearing the cache means the next request will process the whole prompt from scratch. If you’d like, I’m here to provide more details or walk you through some examples—just let me know! Sources: ggml/src/ggml-cann/Doxyfile
v
how do i works with LLM then ?
t
I'm glad you're interested in the
cache_prompt
feature in LiveKit! It’s designed to make your interactions with LLMs smoother by reusing tokens from earlier prompts. Here’s a friendly rundown of how it works: • When you enable it, LiveKit finds the longest common prefix of tokens shared between your current prompt and what’s cached. • It then reuses the cached Key-Value (KV) state for those shared tokens, so you don’t have to process them again. • This means only the new or different tokens get processed, which helps reduce both the waiting time and the computational effort. If you want to keep the cache around for a longer time, you can use the
--prompt-cache
parameter to save the model state after the first prompt. This makes future interactions even faster! There are also options like
--prompt-cache-all
and
--prompt-cache-ro
that give you more ways to customize how caching behaves. This feature really helps make your LLM usage more efficient, especially if you’re working with repeated or similar prompts. Feel free to ask if you’d like me to walk you through more details or share some examples—I’m here to help! Sources: tools/main/README.md | livekit-plugins/livekit-plugins-baseten/livekit/plugins/baseten/llm.py
v
then how do llm know what was the prompt , what is diffrence between caching and sending prompt again , how is it done
t
Thanks for your question! Let me help clarify the difference between caching a prompt and sending the prompt again, so it feels a bit easier to understand: • With caching: LiveKit kindly remembers the tokens it has already processed along with their key-value (KV) cache. When you send a new prompt, it cleverly finds the part that’s the same as before (the shared portion) and reuses those cached tokens. This means it only works on the new or changed parts, saving time and effort. • Without caching: The system has to start fresh every time—it processes the whole prompt again because the KV cache isn’t kept. This can take more computing power since everything is recomputed. The LLM understands the prompt because LiveKit always sends the full or updated prompt context with each request. When caching is on, smart algorithms help match and reuse tokens from the cache, making sure everything runs smoothly and the LLM keeps the right context. If you’d like, I’m here to walk you through more details or examples—just let me know! Sources: examples/parallel/parallel.cpp