Add response token count logic to LLM instrumentations#1583
Draft
umaannamalai wants to merge 153 commits intomainfrom
Draft
Add response token count logic to LLM instrumentations#1583umaannamalai wants to merge 153 commits intomainfrom
umaannamalai wants to merge 153 commits intomainfrom
Conversation
* Add response token count logic to Gemini instrumentation. * Update token counting util functions. * Linting * Add response token count logic to Gemini instrumentation. * Update token counting util functions. * [MegaLinter] Apply linters fixes * Bump tests. --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * Linting * Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * [MegaLinter] Apply linters fixes --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking
* Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking
…-agent into develop-strands
* Add bedrock token counting. * [MegaLinter] Apply linters fixes * Add bedrock token counting. * Add safeguards when grabbing token counts. * Remove extra None defaults. * Cleanup default None checks. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Add baseline instrumentation. * Add tool and agent instrumentation. * Add tests file. * Cleanup instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * [MegaLinter] Apply linters fixes * Strands Mock Model (#1551) * Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking * Add baseline instrumentation. * Add tool and agent instrumentation. * Add tests file. * Cleanup instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * Handle additional args in mock model. * Add test to force exception and exercise _handle_tool_streaming_completion_error. * Strands Mock Model (#1551) * Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking * Add baseline instrumentation. * Add tool and agent instrumentation. * Add tests file. * Cleanup instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * Handle additional args in mock model. * Strands Mock Model (#1551) * Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking * Add baseline instrumentation. * Add tool and agent instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * [MegaLinter] Apply linters fixes * Add test to force exception and exercise _handle_tool_streaming_completion_error. * Implement strands context passing instrumentation. * Address review feedback. * [MegaLinter] Apply linters fixes * Remove test_simple.py file. --------- Co-authored-by: Tim Pansino <tpansino@newrelic.com> Co-authored-by: Timothy Pansino <11214426+TimPansino@users.noreply.github.com> Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add response token count logic to Gemini instrumentation. * Update token counting util functions. * Linting * Add response token count logic to Gemini instrumentation. * Update token counting util functions. * [MegaLinter] Apply linters fixes * Bump tests. --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * Linting * Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * [MegaLinter] Apply linters fixes --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
…ic-python-agent into feature-llm-token-counts
✅MegaLinter analysis: Success
See detailed reports in MegaLinter artifacts |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1583 +/- ##
==========================================
- Coverage 81.82% 76.27% -5.55%
==========================================
Files 207 215 +8
Lines 23953 25989 +2036
Branches 3799 4121 +322
==========================================
+ Hits 19599 19823 +224
- Misses 3086 4848 +1762
- Partials 1268 1318 +50 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bumps the github_actions group with 5 updates in the / directory: | Package | From | To | | --- | --- | --- | | [actions/checkout](https://github.com/actions/checkout) | `5.0.0` | `5.0.1` | | [docker/metadata-action](https://github.com/docker/metadata-action) | `5.8.0` | `5.9.0` | | [docker/setup-qemu-action](https://github.com/docker/setup-qemu-action) | `3.6.0` | `3.7.0` | | [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) | `7.1.2` | `7.1.3` | | [github/codeql-action](https://github.com/github/codeql-action) | `4.31.2` | `4.31.3` | Updates `actions/checkout` from 5.0.0 to 5.0.1 - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](actions/checkout@08c6903...93cb6ef) Updates `docker/metadata-action` from 5.8.0 to 5.9.0 - [Release notes](https://github.com/docker/metadata-action/releases) - [Commits](docker/metadata-action@c1e5197...318604b) Updates `docker/setup-qemu-action` from 3.6.0 to 3.7.0 - [Release notes](https://github.com/docker/setup-qemu-action/releases) - [Commits](docker/setup-qemu-action@2910929...c7c5346) Updates `astral-sh/setup-uv` from 7.1.2 to 7.1.3 - [Release notes](https://github.com/astral-sh/setup-uv/releases) - [Commits](astral-sh/setup-uv@8585678...5a7eac6) Updates `github/codeql-action` from 4.31.2 to 4.31.3 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](github/codeql-action@0499de3...014f16e) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: 5.0.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions - dependency-name: docker/metadata-action dependency-version: 5.9.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github_actions - dependency-name: docker/setup-qemu-action dependency-version: 3.7.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github_actions - dependency-name: astral-sh/setup-uv dependency-version: 7.1.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions - dependency-name: github/codeql-action dependency-version: 4.31.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Runner instrumentation in asyncio * Clean up asyncio instrumentation * Add asyncio tests for loop_factory * Modify uvicorn test for loop_factory * Fix linter errors * [MegaLinter] Apply linters fixes * Apply suggestions from code review --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Correct code for Sanic instrumentation * Correct handling of headers in ASGIWebTransaction * Correct handling of headers in ASGIBrowserMiddleware * Add regression test for ASGI headers issues --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Add more formatting to custom event validatators * Add streamed responses to converse mock server * Add streaming fixtures for testing for converse * Rename other bedrock test files * Add tests for converse streaming * Instrument converse streaming * Move GeneratorProxy adjacent functions to mixin * Fix checking of supported models * Reorganize converse error tests * Port new converse botocore tests to aiobotocore * Instrument response streaming in aiobotocore converse * Fix suggestions from code review * Port in converse changes from strands PR * Delete commented code --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Skip hypercorn tests for v0.18 (#1579) * Skip hypercorn tests for v0.18 * Remove tornadomaster for 3.14 * Fix instability in CI caused by health check tests (#1584) * Bump the github_actions group across 1 directory with 5 updates (#1582) Bumps the github_actions group with 5 updates in the / directory: | Package | From | To | | --- | --- | --- | | [actions/checkout](https://github.com/actions/checkout) | `5.0.0` | `5.0.1` | | [docker/metadata-action](https://github.com/docker/metadata-action) | `5.8.0` | `5.9.0` | | [docker/setup-qemu-action](https://github.com/docker/setup-qemu-action) | `3.6.0` | `3.7.0` | | [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) | `7.1.2` | `7.1.3` | | [github/codeql-action](https://github.com/github/codeql-action) | `4.31.2` | `4.31.3` | Updates `actions/checkout` from 5.0.0 to 5.0.1 - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](actions/checkout@08c6903...93cb6ef) Updates `docker/metadata-action` from 5.8.0 to 5.9.0 - [Release notes](https://github.com/docker/metadata-action/releases) - [Commits](docker/metadata-action@c1e5197...318604b) Updates `docker/setup-qemu-action` from 3.6.0 to 3.7.0 - [Release notes](https://github.com/docker/setup-qemu-action/releases) - [Commits](docker/setup-qemu-action@2910929...c7c5346) Updates `astral-sh/setup-uv` from 7.1.2 to 7.1.3 - [Release notes](https://github.com/astral-sh/setup-uv/releases) - [Commits](astral-sh/setup-uv@8585678...5a7eac6) Updates `github/codeql-action` from 4.31.2 to 4.31.3 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](github/codeql-action@0499de3...014f16e) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: 5.0.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions - dependency-name: docker/metadata-action dependency-version: 5.9.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github_actions - dependency-name: docker/setup-qemu-action dependency-version: 3.7.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github_actions - dependency-name: astral-sh/setup-uv dependency-version: 7.1.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions - dependency-name: github/codeql-action dependency-version: 4.31.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Lalleh Rafeei <84813886+lrafeei@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Add new Redis methods * Add RedisCluster methods to ignore list
Bumps the github_actions group with 3 updates: [actions/checkout](https://github.com/actions/checkout), [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) and [github/codeql-action](https://github.com/github/codeql-action). Updates `actions/checkout` from 5.0.1 to 6.0.0 - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](actions/checkout@93cb6ef...1af3b93) Updates `astral-sh/setup-uv` from 7.1.3 to 7.1.4 - [Release notes](https://github.com/astral-sh/setup-uv/releases) - [Commits](astral-sh/setup-uv@5a7eac6...1e862df) Updates `github/codeql-action` from 4.31.3 to 4.31.5 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](github/codeql-action@014f16e...fdbfb4d) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: 6.0.0 dependency-type: direct:production update-type: version-update:semver-major dependency-group: github_actions - dependency-name: astral-sh/setup-uv dependency-version: 7.1.4 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions - dependency-name: github/codeql-action dependency-version: 4.31.5 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…Bedrock support (#1580) * Record the request message as the time the request started for LangChain. * Tracking the original timestamp of the request for input messages that are recorded as LlmChatCompletionMessage event types. * First pass at preserving LlmChatCompletionMessage timestamp for the request with Bedrock methods. * the `kwargs` was being mapped directly to the OpenAI client and having timestamp in there caused a problem. As a quick test, only add the request timestamp after the wrapped function has been invoked. * Moved the request timestamp to its own variable instead of part of kwargs. * OpenAI async request messages were not being assigned the correct timestamp. * Trying to improve the passing of the request timestamp through for Bedrock. * Passing too many parameters. * Set a default role on input/output messages within LangChain. * [MegaLinter] Apply linters fixes * Fix request_timestamp for LlmChatCompletionSummary table * Fix request_timestamp for LlmChatCompletionSummary table * [MegaLinter] Apply linters fixes * Bedrock Converse Streaming Support (#1565) * Add more formatting to custom event validatators * Add streamed responses to converse mock server * Add streaming fixtures for testing for converse * Rename other bedrock test files * Add tests for converse streaming * Instrument converse streaming * Move GeneratorProxy adjacent functions to mixin * Fix checking of supported models * Reorganize converse error tests * Port new converse botocore tests to aiobotocore * Instrument response streaming in aiobotocore converse * Fix suggestions from code review * Port in converse changes from strands PR * Delete commented code --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> * Bedrock Converse Streaming Support (#1565) * Add more formatting to custom event validatators * Add streamed responses to converse mock server * Add streaming fixtures for testing for converse * Rename other bedrock test files * Add tests for converse streaming * Instrument converse streaming * Move GeneratorProxy adjacent functions to mixin * Fix checking of supported models * Reorganize converse error tests * Port new converse botocore tests to aiobotocore * Instrument response streaming in aiobotocore converse * Fix suggestions from code review * Port in converse changes from strands PR * Delete commented code --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> * [MegaLinter] Apply linters fixes * request_timestamp is now passed across different method * Fixed gemini model kwargs issue * [MegaLinter] Apply linters fixes * Update tests to validate presence of timestamp/ role and fix bugs in instrumentation. * Update aiobotocore instrumentation to receive request timestamp. --------- Co-authored-by: Josh Bonczkowski <jbonczkowski@newrelic.com> Co-authored-by: sgoel-nr <236423107+sgoel-nr@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Timothy Pansino <11214426+TimPansino@users.noreply.github.com> Co-authored-by: Uma Annamalai <uannamalai@newrelic.com>
Bumps the github_actions group with 4 updates: [actions/setup-python](https://github.com/actions/setup-python), [docker/metadata-action](https://github.com/docker/metadata-action), [oxsecurity/megalinter](https://github.com/oxsecurity/megalinter) and [github/codeql-action](https://github.com/github/codeql-action). Updates `actions/setup-python` from 6.0.0 to 6.1.0 - [Release notes](https://github.com/actions/setup-python/releases) - [Commits](actions/setup-python@e797f83...83679a8) Updates `docker/metadata-action` from 5.9.0 to 5.10.0 - [Release notes](https://github.com/docker/metadata-action/releases) - [Commits](docker/metadata-action@318604b...c299e40) Updates `oxsecurity/megalinter` from 9.1.0 to 9.2.0 - [Release notes](https://github.com/oxsecurity/megalinter/releases) - [Changelog](https://github.com/oxsecurity/megalinter/blob/main/CHANGELOG.md) - [Commits](oxsecurity/megalinter@62c799d...55a59b2) Updates `github/codeql-action` from 4.31.5 to 4.31.6 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](github/codeql-action@fdbfb4d...fe4161a) --- updated-dependencies: - dependency-name: actions/setup-python dependency-version: 6.1.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github_actions - dependency-name: docker/metadata-action dependency-version: 5.10.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github_actions - dependency-name: oxsecurity/megalinter dependency-version: 9.2.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github_actions - dependency-name: github/codeql-action dependency-version: 4.31.6 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: github_actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Rename strands instrument functions * Add instrumentation for strands multiagent * Reorganize strands tests * Strands multiagent tests * Remove timestamp from test expected events. --------- Co-authored-by: Uma Annamalai <uannamalai@newrelic.com>
* Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking
* Add baseline instrumentation. * Add tool and agent instrumentation. * Add tests file. * Cleanup instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * [MegaLinter] Apply linters fixes * Strands Mock Model (#1551) * Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking * Add baseline instrumentation. * Add tool and agent instrumentation. * Add tests file. * Cleanup instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * Handle additional args in mock model. * Add test to force exception and exercise _handle_tool_streaming_completion_error. * Strands Mock Model (#1551) * Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking * Add baseline instrumentation. * Add tool and agent instrumentation. * Add tests file. * Cleanup instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * Handle additional args in mock model. * Strands Mock Model (#1551) * Add strands to tox.ini * Add mock models for strands testing * Add simple test file to validate strands mocking * Add baseline instrumentation. * Add tool and agent instrumentation. * Cleanup. Co-authored-by: Tim Pansino <tpansino@newrelic.com> * [MegaLinter] Apply linters fixes * Add test to force exception and exercise _handle_tool_streaming_completion_error. * Implement strands context passing instrumentation. * Address review feedback. * [MegaLinter] Apply linters fixes * Remove test_simple.py file. --------- Co-authored-by: Tim Pansino <tpansino@newrelic.com> Co-authored-by: Timothy Pansino <11214426+TimPansino@users.noreply.github.com> Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add response token count logic to Gemini instrumentation. * Update token counting util functions. * Linting * Add response token count logic to Gemini instrumentation. * Update token counting util functions. * [MegaLinter] Apply linters fixes * Bump tests. --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * Linting * Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * [MegaLinter] Apply linters fixes --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * Linting * Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * [MegaLinter] Apply linters fixes --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add bedrock token counting. * [MegaLinter] Apply linters fixes * Add bedrock token counting. * Add safeguards when grabbing token counts. * Remove extra None defaults. * Cleanup default None checks. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…ic-python-agent into feature-llm-token-counts
* Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * Linting * Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * [MegaLinter] Apply linters fixes --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add bedrock token counting. * [MegaLinter] Apply linters fixes * Add bedrock token counting. * Add safeguards when grabbing token counts. * Remove extra None defaults. * Cleanup default None checks. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…ic-python-agent into feature-llm-token-counts
* Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * Linting * Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * [MegaLinter] Apply linters fixes --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add bedrock token counting. * [MegaLinter] Apply linters fixes * Add bedrock token counting. * Add safeguards when grabbing token counts. * Remove extra None defaults. * Cleanup default None checks. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
* Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * Linting * Add OpenAI token counts. * Add token counts to langchain + openai tests. * Remove unused expected events. * [MegaLinter] Apply linters fixes --------- Co-authored-by: Tim Pansino <timpansino@gmail.com>
* Add bedrock token counting. * [MegaLinter] Apply linters fixes * Add bedrock token counting. * Add safeguards when grabbing token counts. * Remove extra None defaults. * Cleanup default None checks. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…ic-python-agent into feature-llm-token-counts
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.

This PR adds new logic to capture token counts directly from LLM response objects in OpenAI, Bedrock, and Gemini.