Skip to content

Commit 528face

Browse files
authored
fix: cot agent token usage is empty (langgenius#4474)
1 parent c2a8fa9 commit 528face

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

api/core/agent/cot_agent_runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def increase_usage(final_llm_usage_dict: dict[str, LLMUsage], usage: LLMUsage):
121121
raise ValueError("failed to invoke llm")
122122

123123
usage_dict = {}
124-
react_chunks = CotAgentOutputParser.handle_react_stream_output(chunks)
124+
react_chunks = CotAgentOutputParser.handle_react_stream_output(chunks, usage_dict)
125125
scratchpad = AgentScratchpadUnit(
126126
agent_response='',
127127
thought='',

api/core/agent/output_parser/cot_output_parser.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class CotAgentOutputParser:
1111
@classmethod
12-
def handle_react_stream_output(cls, llm_response: Generator[LLMResultChunk, None, None]) -> \
12+
def handle_react_stream_output(cls, llm_response: Generator[LLMResultChunk, None, None], usage_dict: dict) -> \
1313
Generator[Union[str, AgentScratchpadUnit.Action], None, None]:
1414
def parse_action(json_str):
1515
try:
@@ -58,6 +58,8 @@ def extra_json_from_code_block(code_block) -> Generator[Union[dict, str], None,
5858
thought_idx = 0
5959

6060
for response in llm_response:
61+
if response.delta.usage:
62+
usage_dict['usage'] = response.delta.usage
6163
response = response.delta.message.content
6264
if not isinstance(response, str):
6365
continue

0 commit comments

Comments
 (0)