Does the llm used in the agent cache the system pr...
# ask-ai
c
Does the llm used in the agent cache the system prompt by default? How to ensure that it is cached and not passed multiple times?
t
Thanks for reaching out, Subham. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! I wanted to share some helpful info about how the LLM in LiveKit handles system prompt caching. By default, it doesn’t cache the system prompt, but you can easily enable this feature by setting the
caching
parameter to
"ephemeral"
when you create your LLM instance. Here’s a quick example to guide you:
Copy code
from livekit.plugins import anthropic

llm_instance = anthropic.LLM(
    model="claude-3-5-sonnet-20241022",
    caching="ephemeral"  # Enable prompt caching
)
Once caching is turned on, the system prompt, tools, and chat history are stored temporarily. This helps avoid sending the same information repeatedly across requests, making things more efficient. Behind the scenes, this works by adding cache control metadata to both the system prompt and chat history. If you want to check that caching is working, you can look at metrics like
prompt_cached_tokens
,
cache_creation_tokens
, and
cache_read_tokens
in the
CompletionUsage
object you get back after an inference call. If you have any questions or want to dive deeper into this, I’m here to help! Sources: examples/voice_agents/realtime_joke_teller.py
c
how can I do this without using llm_instance?
t
I'd be happy to help you enable caching for the system prompt without using
llm_instance
. You can do this by initializing the LLM directly with the
caching
parameter set to
"ephemeral"
. This option is specific to the Anthropic plugin in LiveKit. Here's a simple example to guide you:
Copy code
from livekit.plugins.anthropic import LLM

llm = LLM(
    model="claude-3-5-sonnet-20241022",
    caching="ephemeral"  # Enables caching for system prompt, tools, and chat history
)
At the moment, this is the recommended way to enable caching for system prompts in LiveKit. If you're working with a different LLM provider like OpenAI or Google Gemini, caching might not be available out of the box. Please feel free to reach out if you'd like assistance with other options, such as setting up manual caching within your application—I’m here to help! Sources: livekit-agents/livekit/agents/inference/llm.py | tools/server/public_legacy/index-new.html | Large language models (LLM) | LiveKit Docs
c
does gemini has this option?
t
Thanks for reaching out! I wanted to let you know that Google Gemini in LiveKit doesn’t currently support caching for system prompts. You can set your system instructions using the
instructions
parameter when you initialize, but there isn’t a built-in way to cache or reuse these prompts across different sessions without specifying them again. If you’re aiming to optimize how prompts are handled or want to reduce latency, a good approach is to preload your instructions during agent initialization or manage the state externally. I’m here to help if you’d like some guidance on how to set this up—just let me know! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/realtime/api_... | web/src/data/presets.ts | Gemini Live API plugin | LiveKit Docs
c
what are the llms that currently support system prompt caching? does realtime gpt have system prompt caching?
t
Hi there! I wanted to share some helpful information about system prompt caching with the LLMs supported in LiveKit: • Anthropic LLM: You can enable caching by using the
caching="ephemeral"
parameter during initialization. This will cache the system prompt, tools, and chat history, making things smoother for you. • llama.cpp: It supports caching through the
cache_prompt=true
flag, which is actually on by default to help optimize prompt reuse. For Realtime GPT (OpenAI), I want to be transparent—there’s no clear indication that system prompt caching is currently supported in the LiveKit integration. Although the Realtime API offers features like chat context management and session state control, caching isn’t specifically mentioned. If caching is really important for your work with Realtime GPT, a good workaround might be to use longer session durations to keep context or handle caching on your end externally. Please feel free to reach out if you'd like to explore this further or need any help—I’m here for you! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | tools/server/public_simplechat/simplechat.js