how can i do prompt caching within livekit llm to ...
# ask-ai
p
how can i do prompt caching within livekit llm to reduce ttft. i want to reduce latency caused by the large prompt size. because prompt size can vary from user to user but we need at least consistent benchmark latency no matter how much prompt.
t
Thanks for reaching out, Vaneeza. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you implement prompt caching in LiveKit LLM to reduce latency caused by large prompt sizes. Depending on your deployment type, here are some approaches you can consider: • Local Inference with llama.cpp: If you're working with self-hosted models, LiveKit offers disk-based prompt caching through the
chat-persistent.sh
script. This handy feature saves processed prompt tokens to disk and reuses them across interactions, helping speed things up. You can easily set this up using environment variables like
PROMPT_CACHE_FILE
and
CHAT_SAVE_DIR
. It’s a great option for achieving low-latency with local AI agents. • Cloud Integration with Anthropic: For those using cloud-based models, LiveKit integrates smoothly with Anthropic and supports ephemeral prompt caching. Just set
caching="ephemeral"
in the LLM constructor, and it will automatically cache key parts of the prompt, like the last system and assistant messages, to avoid redundant processing. If you'd like, I’m here to guide you through the detailed steps for either method. Just let me know whether your setup is local or cloud-based, and we can take it from there together! Sources: livekit-plugins/livekit-plugins-anthropic/livekit/plugins/anthropic/utils.p... | tools/server/public_simplechat/readme.md
p
no im working with cloud based llms, not local inference. specifically openai models and groq llama models.
t
For cloud-based LLMs like OpenAI and Groq LLaMA models, you can easily implement prompt caching using the
prompt_cache_key
parameter available in LiveKit's plugins. Let me walk you through how to set it up: • OpenAI Models: You can use the OpenAI plugin and specify a
prompt_cache_key
when initializing the LLM. Here's a simple example:
Copy code
from livekit.plugins import openai

llm = openai.LLM(
    model="gpt-4o",
    prompt_cache_key="my_openai_cached_prompt"
)
• If you're working with Azure OpenAI, you can also include the
azure_endpoint
and
azure_deployment
parameters to make it work smoothly. • Groq LLaMA Models: In a similar way, you can use the Groq plugin and set the
prompt_cache_key
like this:
Copy code
from livekit.plugins import groq

llm = groq.LLM(
    model="llama-3.3-70b-versatile",
    prompt_cache_key="my_cached_prompt"
)
By using consistent cache keys for similar prompt patterns, you'll get the best out of caching, improving performance and reducing latency. If you have any questions or want a hand with the setup, just let me know—I'm here to help! Sources: livekit-plugins/livekit-plugins-anthropic/livekit/plugins/anthropic/llm.py | plugins/openai/src/stt.ts | tools/main/README.md
p
yes please explain with an example. how cache key would look? and how to get best out of cache.
t
To help you get the most out of prompt caching, it’s important that the
prompt_cache_key
uniquely captures the structure of your prompt while staying consistent for similar prompts. Let me walk you through an example and some helpful tips: Example: OpenAI Model with Prompt Caching
Copy code
from livekit.plugins import openai

# Define a consistent cache key based on the prompt structure
cache_key = "user123_session1_prompt_v1"

llm = openai.LLM(
    model="gpt-4o",
    prompt_cache_key=cache_key
)

# Send a prompt to the model
response = llm.generate(
    prompt="What is the capital of France?"
)
Best Practices for Cache Keys • Consistency: Try to use the same cache key for prompts that share the same structure. For example, if your prompt always includes a system message and user input, make sure the cache key reflects that. • Uniqueness: It’s helpful to include identifiers like user IDs, session IDs, or version numbers so different prompts don’t conflict. • Granularity: Avoid making the cache key too detailed (like including the entire prompt text) because that can limit how often the cache is reused. Example Cache Key Strategy For instance, in a chatbot application, you might organize your cache keys like this: • `"chatbot_v1_user123_session1"`: This is for a specific user and session. • `"chatbot_v1_system_prompt"`: This could be for a shared system prompt used across sessions. Following these guidelines can really help your cache work efficiently, making your application faster and smoother. If anything isn’t clear or you want to dive deeper, just let me know—I’m here to help!
p
is prompt cache key available in livekit? or deprecated? bcz openai handles caching prompt tokens automatically
t
I'm happy to let you know that the
prompt_cache_key
parameter is still available and fully supported in LiveKit for OpenAI models. It hasn’t been deprecated at all. While OpenAI does manage some caching behind the scenes, using
prompt_cache_key
gives you that extra control to fine-tune caching for specific prompt patterns, which can really help boost performance in certain cases. Feel free to keep using
prompt_cache_key
in your LiveKit setups without any worries about it going away. If you have any more questions or need a hand with anything else, I’m here to help! Sources: livekit-plugins/livekit-plugins-baseten/livekit/plugins/baseten/llm.py
p
how to create a cache key?
t
Creating an effective cache key is a great way to keep things running smoothly, and I’m here to help you through it! Think of a cache key as a unique identifier that captures the essence of your prompt while staying consistent for similar requests. Let’s walk through how you can create one: Steps to Create a Cache Key: 1. Identify Key Components: Start by figuring out which parts of your prompt stay the same across requests — like system messages, user roles, or specific instructions. 2. Include Unique Identifiers: Then, add details that make each context unique, such as user IDs, session IDs, or timestamps. This helps keep things organized and distinct. 3. Use a Clear Format: Finally, combine these pieces into a neat, structured string. Using underscores or hyphens to separate elements works really well. Example Cache Key: • For a chatbot application:
chatbot_v1_user123_session1
• For a shared system prompt:
chatbot_v1_system_prompt
Best Practices: • Keep the key concise but descriptive, so it’s easy to understand. • Make sure similar prompts use consistent keys to get the most out of your cache. • Try to avoid including data that changes a lot (like the full prompt text) to prevent unnecessary cache misses. If you’d like, I’d be happy to help you design a cache key tailored to your specific needs — just let me know!
Thank you for sharing your configuration! Here’s a personalized cache key design that should help keep things consistent for your call flow, business hours, and knowledge base, while also including unique identifiers to keep everything organized: Cache Key Structure:
Copy code
{agentName}_{businessType}_{callFlowVersion}_{businessHoursVersion}_{knowledgeBaseVersion}
Example Cache Key:
Copy code
real-estate_real-estate_v1_v1_v1
Explanation: • agentName: This represents the agent, like "real-estate". • businessType: Specifies your type of business, for example, "real-estate". • callFlowVersion: A version number for your call flow setup. You’d increase this whenever the call flow changes. • businessHoursVersion: A version number for your business hours. Update this if your schedule changes. • knowledgeBaseVersion: A version number for your knowledge base. Increment this when you update the content. How to Use: 1. Start by assigning version numbers to your call flow, business hours, and knowledge base configurations. 2. Then, combine these parts into one string as shown above. 3. Finally, use this cache key in your
prompt_cache_key
parameter when you initialize the LLM. If you have any questions or need a hand with implementing this, please don’t hesitate to reach out—I’m here to help!
p
i meant when to create cache key? and how to use within agent? which folder does the
Copy code
prompt_cache_key
parameter looks inside?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
p
tell me how it works. the llm cache key contains content which would be present inside prompts (agent instructions) frequently?