You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "Zhuoxi2000 (via GitHub)" <gi...@apache.org> on 2026/08/04 00:25:52 UTC

[PR] [integration][anthropic][python] Keep token usage on non-tool_use responses [flink-agents]

Zhuoxi2000 opened a new pull request, #959:
URL: https://github.com/apache/flink-agents/pull/959

   <!--
   * Thank you very much for contributing to Flink Agents.
   * Please add the relevant components in the PR title. E.g., [api], [runtime], [java], [python], [hotfix], etc.
   -->
   
   <!-- Please link the PR to the relevant issue(s). Hotfix doesn't need this. -->
   Linked issue: #946
   
   ### Purpose of change
   
   <!-- What is the purpose of this change? -->
   
   `extra_args` (`model_name` / `promptTokens` / `completionTokens`) is built after every Anthropic API call, but was only attached to the returned `ChatMessage` on the `tool_use` branch. Plain-text responses (`stop_reason == "end_turn"`, the common case) returned without `extra_args`, so `_record_token_metrics` never fired for them: in a ReAct loop only the intermediate tool-calling turns were counted and the final answer's tokens were always lost. All sibling connectors (openai / azure / ollama / tongyi) already pass `extra_args` on every path.
   
   Fix: pass `extra_args=extra_args` in the non-tool_use branch. One line; no behavior change on the tool_use path.
   
   ### Tests
   
   <!-- How is this change verified? -->
   
   Two new cases in `test_anthropic_response_parsing.py`: `test_plain_text_response_keeps_token_usage` (regression for the fixed branch) and `test_tool_use_response_keeps_token_usage` (guard for the already-working branch). Both are mock-based with explicit token counts and a `model=` kwarg so the metrics path is actually exercised — the pre-existing `test_plain_text_response` only asserted `content` and never covered this path.
   
   ### API
   
   <!-- Does this change touches any public APIs? -->
   
   None.
   
   ### Documentation
   
   <!-- Do not remove this section. Check the proper box only. -->
   
   - [ ] `doc-needed` <!-- Your PR changes impact docs -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-included` <!-- Your PR already contains the necessary documentation updates -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [integration][anthropic][python] Keep token usage on non-tool_use responses [flink-agents]

Posted by "wenjin272 (via GitHub)" <gi...@apache.org>.
wenjin272 merged PR #959:
URL: https://github.com/apache/flink-agents/pull/959


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [integration][anthropic][python] Keep token usage on non-tool_use responses [flink-agents]

Posted by "Zhuoxi2000 (via GitHub)" <gi...@apache.org>.
Zhuoxi2000 commented on PR #959:
URL: https://github.com/apache/flink-agents/pull/959#issuecomment-5223740058

   > Thanks for taking this on, and for covering the tool_use branch alongside the fixed one.
   > 
   > One thing that might be useful for the issue: the Java connector already attaches these three keys to every response, whatever the stop reason.
   > 
   > Fix and tests check out against main. Nothing further from me, and I'll leave the final call to the maintainers.
   
   Thanks for checking against main! Good catch on the Java side, confirmed that it preserves `model_name`, `promptTokens`, and `completionTokens` on every response path, so this fix brings Python back in line with Java. I’ve noted that on #946 as well.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org