Repository navigation
feat(ai): record the Anthropic web search count in the LangChain callback handler - #1097
Open
pcbeingused333 wants to merge 5 commits into
Open
pcbeingused333 wants to merge 5 commits into
pcbeingused333 wants to merge 5 commits into
Conversation
marandaneto
reviewed
Oct 22, 2026
marandaneto
left a comment
Member
There was a problem hiding this comment.
Automated advisory code review.
Member
|
@PostHog/team-ai-observability do we need any backend/frontend change for this? |
…back handler Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # posthog/test/ai/langchain/test_callbacks.py
marandaneto
force-pushed
the
fix/langchain-web-search-count
branch
from
October 23, 2026 09:32
df78690 to
dd3f216
Compare
carlos-marchal-ph
requested changes
Oct 23, 2026
Contributor
No, but there were some issues with the implementation that I surfaced on my review |
…d omit zero Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Contributor
Author
|
Thanks, all four points addressed in the latest commit:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
The
posthog-jsLangChain handler records$ai_web_search_count, and so do the Python OpenAI, Anthropic and Gemini wrappers, but the Python LangChain callback handler never did. When Anthropic reportsusage.server_tool_use.web_search_requests(as a dict or an object),_parse_usage_modelnow reads it into a newweb_search_countfield ofModelUsage, and the handler adds$ai_web_search_countto the event, including a count of 0. Nothing is added when the provider reports no count.langchain-anthropicpasses the raw Anthropicusage(withserver_tool_use) inllm_outputfor non-streamed calls, which is where the handler reads it; streamed calls only carry the standardusage_metadata, so no count is recorded for them. Only this exact count is handled;posthog-jsalso infers a single search from citations or grounding metadata for other providers, which is not ported here.💚 How did you test it?
TestWebSearchCountinposthog/test/ai/langchain/test_callbacks.py: a response with two web search requests, one with zero, and one withoutserver_tool_use. The first two fail onmasterand pass with the change; the third guards the omitted case.posthog/test/ai/langchain/test_callbacks.py: 83 passed, 8 skipped.ruff format --checkpasses on the touched directories. The rest of the suite and a live LangChain call with the Anthropic web search tool were not run.📝 Checklist
If releasing new changes
sampo addto generate a changeset file (added.sampo/changesets/langchain-web-search-count.md)🤖 Agent context
Autonomy: Fully autonomous
🤖 Generated with Claude Code
🤖 Generated with Claude Code