Skip to content

fix(sdk): stop ChatAnthropic emitting a duplicate event per LLM call - #86

Merged
gladstomych-sa merged 1 commit into
mainfrom
fix/chatanthropic-duplicate-events
Aug 18, 2026
Merged

fix(sdk): stop ChatAnthropic emitting a duplicate event per LLM call#86
gladstomych-sa merged 1 commit into
mainfrom
fix/chatanthropic-duplicate-events

Conversation

@yanny-sec

Copy link
Copy Markdown
Contributor

Summary

ChatAnthropic goes through the same Anthropic SDK methods Adrian patches, so every LangChain call produced two LLM events instead of one, which double-counts events and tokens. The fix is a chat_model_scope() contextvar set across the four patched BaseChatModel entry points. The Anthropic wrappers check it and skip emitting when LangChain already owns the call, verified at 1 event per call for ainvoke, astream and the raw SDK, with 5 new tests and 394 passing.

Test plan

  • Tested that the events do not get duplicated and wrote unit tests for it.

Checklist

  • CLA signed (see CLA.md)
  • Tests pass locally
  • Docs updated where needed
  • British English; no em-dashes; no marketing fluff

@yanny-sec yanny-sec self-assigned this Aug 17, 2026
@yanny-sec yanny-sec added the bug Something isn't working label Aug 17, 2026
@yanny-sec
yanny-sec force-pushed the fix/chatanthropic-duplicate-events branch from aba1317 to 58e3dcc Compare August 17, 2026 13:29
@gladstomych-sa
gladstomych-sa self-requested a review August 18, 2026 08:03

@gladstomych-sa gladstomych-sa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gladstomych-sa
gladstomych-sa merged commit 7929189 into main Aug 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants