about summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2026-11-04 00:02:26 +0000
committerArun Isaac2026-11-04 00:02:26 +0000
commit8423d578cffdef869d323d4e46c41f347b2268cb (patch)
tree12ad8f55eb1b00924a75f8a42f2a320f7f363476
parent38350701c4130a3b3124477ac5f4410eedd32f88 (diff)
downloadkaagum-8423d578cffdef869d323d4e46c41f347b2268cb.tar.gz
kaagum-8423d578cffdef869d323d4e46c41f347b2268cb.tar.lz
kaagum-8423d578cffdef869d323d4e46c41f347b2268cb.zip
Handle LLM responses without the "cached_tokens" field. HEAD main
Do not crash when the "cached_tokens" usage field is absent. Some LLM
providers don't provide this field.
-rw-r--r--kaagum/tea.scm9
-rw-r--r--tests/tea.scm24
2 files changed, 29 insertions, 4 deletions
diff --git a/kaagum/tea.scm b/kaagum/tea.scm
index 749abb9..f3b2397 100644
--- a/kaagum/tea.scm
+++ b/kaagum/tea.scm
@@ -843,10 +843,11 @@ in @code{tea-loop}."
                                        0)
                                    <>)
                               (add (state-session-cache-read-tokens session-id)
-                                   (focus (in-json "usage"
-                                                   "prompt_tokens_details"
-                                                   "cached_tokens")
-                                          (llm-response-json response))
+                                   (or (focus (in-json "usage"
+                                                       "prompt_tokens_details"
+                                                       "cached_tokens")
+                                              (llm-response-json response))
+                                       0)
                                    <>)
                               (add (state-session-cache-write-tokens session-id)
                                    (or (focus (in-json "usage"
diff --git a/tests/tea.scm b/tests/tea.scm
index 44915c3..d7f3c03 100644
--- a/tests/tea.scm
+++ b/tests/tea.scm
@@ -63,4 +63,28 @@
    (state #f 0 1 '()
           `((0 . ,(session (getcwd) "foo"))))))
 
+(test-assert "handle LLM response without \"cached_tokens\" field in usage"
+  (run-with-state
+   (acp-effects (llm-response 0
+                              `(("id" . "gen-1793743825")
+                                ("model" . "anthropic/claude-haiku-5.5")
+                                ("choices"
+                                 .
+                                 #((("message"
+                                     ("role" . "assistant")
+                                     ("content" . "Hello! How can I help you today?")))))
+                                ("usage"
+                                 ("prompt_tokens" . 1770)
+                                 ("completion_tokens" . 13)
+                                 ("total_tokens" . 1783)
+                                 ("cost" . 3.601e-4)
+                                 ("prompt_tokens_details"
+                                  ("cache_write_tokens" . 1766))
+                                 ("completion_tokens_details"
+                                  ("reasoning_tokens" . 0)))))
+                %models
+                '())
+   (state #f 0 1 '()
+          `((0 . ,(session (getcwd) "foo"))))))
+
 (test-end "tea")