Using generate_reply() function will that cost me ...
# ask-ai
v
Using generate_reply() function will that cost me tokens of base prompt too ? Like for example prompt token = 100 session starts tokens used 100 i used generate reply with some instruction (10 token) : as it uses base prompt + temp instruction so total 110 At last combined session + generate reply , will it cost 210 tokens .
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.
Thanks for reaching out! Just to clarify, the
generate_reply()
function does include the base prompt tokens when calculating token costs. So, if your session begins with 100 tokens for the base prompt and you add 10 tokens for instructions using
generate_reply()
, the total token usage for that step would be 110 tokens. When you add this to the session tokens, the overall total comes to 210 tokens. I hope this helps clear things up! If you have any more questions, feel free to ask—I'm here to help. Sources: Logs, metrics, and telemetry | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real...
v
so it means every time i use generate_reply() it uses the prompt token again unlike session which keeps a KV cache ?
t
I'm happy to explain how the
generate_reply()
function works with prompt tokens! When the
cache_prompt
option is enabled (which it is by default), the function cleverly reuses tokens from previous prompts through a KV (key-value) cache. This means it only processes the new or changed parts of your prompt, making things more efficient and saving time by not recalculating everything from scratch. On the other hand, if you choose to disable
cache_prompt
, the KV cache is cleared, and the function will reprocess all tokens every time. If you’d like, I’m here to help with any questions or guide you through setting up or tweaking this feature to best suit your needs! Sources: tools/server/server.cpp
v
So , we can understand it like , session and generate_Reply both manages a KV cache but diffrently , session have a diffrent cache and generate_reply() have a diffrent cache, so invoking the generate reply first time uses up prompt token but then continues for per querry
t
Hi there! I’d love to help clarify how
session
and
generate_reply()
use KV (key-value) caching, as they do it in slightly different ways. The
generate_reply()
function taps into a KV cache at the LLM backend level, which is pretty neat because it can reuse tokens from previous prompts—especially if you have the
cache_prompt
option turned on. So, after the very first time you call it, it only needs to process the new or changed parts of your prompt, saving you some effort! On the other hand, the session keeps track of its own context and conversation history separately. It’s more about managing the overall flow of your conversation rather than handling token-level caching. So, while both use caching in their own ways, they work independently. Just keep in mind that the first time you call
generate_reply()
, it will use prompt tokens as usual, but after that, you’ll see the benefits of token reuse thanks to the backend’s KV cache. I hope this helps clear things up—feel free to ask if you want to dive deeper! Sources: .github/workflows/bench.yml.disabled | tests/test_agent_session.py