TradingAgents/tradingagents/llm_clients
Michael Yang 3d8341c104 fix: report LLM calls, tool calls, and token usage for claude_agent
ChatClaudeAgent is a plain Runnable rather than a BaseChatModel, so
LangChain's callback system never fired on_chat_model_start / on_llm_end
for it — leaving the CLI TUI stuck on "LLM: 0" and "Tokens: --" during
runs. Pop callbacks out of the LLM kwargs, invoke them manually around
each SDK call, and attach usage_metadata extracted from the SDK's
ResultMessage (input, output, total — including cached input) to the
returned AIMessage so downstream handlers pick it up.

Tool callbacks now also fire through the MCP wrapper: forward the
callback list into each wrapped LangChain tool's invocation config so
StatsCallbackHandler sees on_tool_start/on_tool_end when the SDK loop
calls a tool.

Verified via direct StatsCallbackHandler round-trip on both Shape A
(ChatClaudeAgent.invoke) and Shape B (run_sdk_analyst): llm_calls,
tool_calls, tokens_in, and tokens_out all increment as expected.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 17:32:11 -04:00
..
TODO.md fix: pass base_url to Google and Anthropic clients for proxy support (#427) 2026-03-29 17:59:52 +00:00
__init__.py feat: add multi-provider LLM support with factory pattern 2026-02-03 22:27:20 +00:00
anthropic_client.py Merge pull request #464 from CadeYu/sync-validator-models 2026-03-29 11:07:51 -07:00
azure_client.py feat: add DeepSeek, Qwen, GLM, and Azure OpenAI provider support 2026-04-13 07:12:07 +00:00
base_client.py Merge pull request #464 from CadeYu/sync-validator-models 2026-03-29 11:07:51 -07:00
claude_agent_client.py fix: report LLM calls, tool calls, and token usage for claude_agent 2026-04-14 17:32:11 -04:00
factory.py feat: add claude_agent LLM provider backed by Claude Agent SDK (Shape A) 2026-04-14 15:53:26 -04:00
google_client.py Merge pull request #464 from CadeYu/sync-validator-models 2026-03-29 11:07:51 -07:00
mcp_tool_adapter.py fix: report LLM calls, tool calls, and token usage for claude_agent 2026-04-14 17:32:11 -04:00
model_catalog.py chore: surface claude_agent first in CLI and clean up model labels 2026-04-14 17:12:56 -04:00
openai_client.py feat: add DeepSeek, Qwen, GLM, and Azure OpenAI provider support 2026-04-13 07:12:07 +00:00
validators.py sync model validation with cli catalog 2026-03-25 21:23:02 +08:00