Zhuoxi2000 opened a new issue, #946:
URL: https://github.com/apache/flink-agents/issues/946

   ### Search before asking
   
   - [x] I searched in the 
[issues](https://github.com/apache/flink-agents/issues) and found nothing 
similar.
   
   ### Description
   
   In the Python Anthropic connection, `extra_args` (carrying `model_name` / 
`promptTokens` / `completionTokens`) is built after every API call, but it is 
only attached to the returned `ChatMessage` on the `tool_use` branch. The 
plain-text branch (`stop_reason == "end_turn"`, the common case) returns 
`ChatMessage(role, content=text)` without `extra_args`, so token metrics are 
dropped for every normal response.
   
   Where it happens 
(`python/flink_agents/integrations/chat_models/anthropic/anthropic_chat_model.py`):
 `extra_args` is populated right after `client.messages.create(...)`, the 
`tool_use` branch passes `extra_args=extra_args`, and the `else` branch does 
not.
   
   **Expected**: `promptTokens` / `completionTokens` counters increase for 
every Anthropic response that carries `usage`, matching the other connectors — 
the consumer in `chat_model_action.py` reads exactly these keys before calling 
`_record_token_metrics`.
   
   **Actual**: only tool-calling turns are counted. In a ReAct loop, the 
intermediate tool-use turns are recorded but the final answer's tokens are 
always lost, so Anthropic token metrics are systematically under-reported.
   
   All sibling connectors (openai / azure / ollama / tongyi) pass `extra_args` 
on every path, so this looks like an omission rather than a design choice. 
Related in spirit to the non-happy-path parity audit in #936.
   
   Fix is one line (pass `extra_args=extra_args` in the `else` branch) plus a 
regression test — the existing `test_plain_text_response` only asserts 
`content` and calls `chat()` without a `model=` kwarg, so this path was never 
covered.
   
   ### How to reproduce
   
   Unit-level (no network):
   
   1. Build an `AnthropicChatModelConnection` with a mocked client returning
      `Message(stop_reason="end_turn", content=[TextBlock("Hello!")], 
usage=Usage(input_tokens=7, output_tokens=3))`.
   2. Call `connection.chat([ChatMessage(role=USER, content="hi")], 
model="claude-sonnet-4-5")`.
   3. `response.extra_args` is `{}` — expected `{"model_name": 
"claude-sonnet-4-5", "promptTokens": 7, "completionTokens": 3}`.
   
   Repeat the same call with a `tool_use` response: `extra_args` is populated. 
Same request shape, divergent metrics behavior.
   
   End-to-end: run any Python agent with an `AnthropicChatModelSetup`, send 
plain chats (no tools), and watch the `promptTokens` / `completionTokens` 
counters stay at 0 while equivalent tool-calling runs do count.
   
   ### Version and environment
   
   Current main (4d99567, 2026-07-25). The drop is in the connector code path 
itself, so it affects
   all deployment modes. Verified on macOS arm64 / Python 3.12 with a 
mock-based unit test.
   
   ### Are you willing to submit a PR?
   
   - [x] I'm willing to submit a PR!


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to