Skip to content

Add callback for token usage#999

Open
saisakul wants to merge 7 commits into
devfrom
feature/datadog_tokens
Open

Add callback for token usage#999
saisakul wants to merge 7 commits into
devfrom
feature/datadog_tokens

Conversation

@saisakul
Copy link
Copy Markdown
Collaborator

@saisakul saisakul commented Apr 24, 2026

Context

We need to provide token usage to Datadog so that it can calculate the LLM cost.

What

  • Add a callback to add annotation to the LLM span
  • Change from auto instrument to manual instrument. Need to remove botocore from auto otherwise it will overwrite the token usage that we annotated.
  • Add tests for new callback function

Have you written unit tests?

  • Yes
  • [] No (add why you have not)

tested locally and in dev

Are there any specific instructions on how to test this change?

  • Yes (if so provide more detail)
  • No

run some prompts in dev and then check that DataDog traces calculate token costs

Relevant links

@saisakul saisakul requested a review from a team as a code owner April 24, 2026 08:04
@github-actions
Copy link
Copy Markdown
Contributor

Your PR has commits that are missing the Signed-off-by trailer. This is likely due to the pre-commit hook not being configured on your local machine. The usual fix for this issue is to run pre-commit install --install-hooks --overwrite -t commit-msg -t pre-commit, however for more detailed help in setting up the pre-commit hooks, follow the instructions at https://github.com/uktrade/github-standards/blob/main/README.md#usage

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

Dependency Review

✅ No vulnerabilities or OpenSSF Scorecard issues found.

Scanned Files

None

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.04%. Comparing base (635ec71) to head (1a55f0f).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #999      +/-   ##
==========================================
- Coverage   82.06%   82.04%   -0.02%     
==========================================
  Files         198      199       +1     
  Lines        8208     8239      +31     
==========================================
+ Hits         6736     6760      +24     
- Misses       1472     1479       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

return model_name

def on_llm_end(self, response: LLMResult, **kwargs) -> None:
if response.generations[0]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NIT: may be worth checking list is non-empty before checking first element, ie.

if respoonse.generations and response.generations[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants