Skip to content

Commit c5b13ce

Browse files
committed
Bring back cached token counts in stream accumulator
1 parent c66899d commit c5b13ce

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

lib/ruby_llm/stream_accumulator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def initialize
1010
@tool_calls = {}
1111
@input_tokens = 0
1212
@output_tokens = 0
13+
@cached_tokens = 0
14+
@cache_creation_tokens = 0
1315
@latest_tool_call_id = nil
1416
end
1517

@@ -35,6 +37,8 @@ def to_message(response)
3537
tool_calls: tool_calls_from_stream,
3638
input_tokens: @input_tokens.positive? ? @input_tokens : nil,
3739
output_tokens: @output_tokens.positive? ? @output_tokens : nil,
40+
cached_tokens: @cached_tokens.positive? ? @cached_tokens : nil,
41+
cache_creation_tokens: @cache_creation_tokens.positive? ? @cache_creation_tokens : nil,
3842
raw: response
3943
)
4044
end
@@ -137,6 +141,8 @@ def find_tool_call(tool_call_id)
137141
def count_tokens(chunk)
138142
@input_tokens = chunk.input_tokens if chunk.input_tokens
139143
@output_tokens = chunk.output_tokens if chunk.output_tokens
144+
@cached_tokens = chunk.cached_tokens if chunk.cached_tokens
145+
@cache_creation_tokens = chunk.cache_creation_tokens if chunk.cache_creation_tokens
140146
end
141147
end
142148
end

0 commit comments

Comments
 (0)