From 7c5e16b153d8672182fe91e02e71102c3e4b9198 Mon Sep 17 00:00:00 2001 From: Vladimir Blagojevic Date: Tue, 17 Sep 2024 09:17:28 +0200 Subject: [PATCH] Improve UX for prompt caching example --- integrations/anthropic/example/prompt_caching.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/integrations/anthropic/example/prompt_caching.py b/integrations/anthropic/example/prompt_caching.py index c7b073938..d8cc0f0e8 100644 --- a/integrations/anthropic/example/prompt_caching.py +++ b/integrations/anthropic/example/prompt_caching.py @@ -91,5 +91,12 @@ def stream_callback(chunk: StreamingChunk) -> None: # tokens used to create the prompt cache # on first subsequent cache hit we'll see a usage key 'cache_read_input_tokens' having a value of the number of # tokens read from the cache - print(f"Cache usage: {result['llm']['replies'][0].meta.get('usage')}") + token_stats = result["llm"]["replies"][0].meta.get("usage") + if token_stats.get("cache_creation_input_tokens", 0) > 0: + print("Cache created! ", end="") + elif token_stats.get("cache_read_input_tokens", 0) > 0: + print("Cache hit! ", end="") + else: + print("Cache not used, something is wrong with the prompt caching setup. ", end="") + print(f"Cache usage details: {token_stats}") print("\n" + "=" * 100)