Skip to content

Commit fb9c0f9

Browse files
authored
Merge pull request #210 from scaleapi/declan-scale/add-up-usage
Add concat functionality for usage
2 parents 6596c96 + 80e3547 commit fb9c0f9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/agentex/lib/utils/completions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from agentex.lib.types.llm_messages import (
88
Delta,
9+
Usage,
910
Choice,
1011
ToolCall,
1112
Completion,
@@ -21,6 +22,8 @@ def _concat_chunks(_a: None, b: Any):
2122
@_concat_chunks.register
2223
def _(a: Completion, b: Completion) -> Completion:
2324
a.choices = [_concat_chunks(*c) for c in zip(a.choices, b.choices, strict=False)]
25+
a.usage = _concat_chunks(a.usage, b.usage)
26+
2427
return a
2528

2629

@@ -35,6 +38,17 @@ def _(a: Choice, b: Choice) -> Choice:
3538
a.finish_reason = a.finish_reason or b.finish_reason
3639
return a
3740

41+
@_concat_chunks.register
42+
def _(a: Usage | None, b: Usage | None) -> Usage | None:
43+
if a is not None and b is not None:
44+
return Usage(
45+
prompt_tokens=a.prompt_tokens + b.prompt_tokens,
46+
completion_tokens=a.completion_tokens + b.completion_tokens,
47+
total_tokens=a.total_tokens + b.total_tokens,
48+
)
49+
else:
50+
return a or b
51+
3852

3953
@_concat_chunks.register
4054
def _(a: Delta, b: Delta) -> Delta:

0 commit comments

Comments
 (0)