Skip to content

Conversation

@Hansehart
Copy link

@Hansehart Hansehart commented Oct 18, 2025

Fixes #2322

Summary

Adds token usage tracking for embedding operations in the Haystack instrumentation package. This enables cost monitoring and usage analytics for embedding API calls when using observability platforms like Langfuse.

Changes

  • Added _get_embedding_token_count_attributes() function to extract token counts from embedder responses
  • Integrated token tracking into the EMBEDDER component handler in _ComponentRunWrapper
  • Added test assertions in test_openai_document_embedder_embedding_span_has_expected_attributes to verify token count attributes
  • Follows the same implementation pattern as existing _get_llm_token_count_attributes()

Implementation Details

The new function supports two response formats:

  1. Standard embedding API format: response["usage"]
  2. Custom components format: response["meta"]["usage"]

Token counts tracked:

  • LLM_TOKEN_COUNT_PROMPT - Input tokens consumed
  • LLM_TOKEN_COUNT_TOTAL - Total tokens used
  • Note: LLM_TOKEN_COUNT_COMPLETION is not tracked for embeddings (no output generation)

Test Coverage

Updated existing test to verify token count attributes are correctly captured from OpenAI embedding responses.


Note

Adds token usage extraction for embedding responses and records model/tokens on EMBEDDER spans, with tests.

  • Instrumentation (Haystack):
    • EMBEDDER spans now include token usage and model details.
      • New _get_embedding_token_count_attributes() extracts prompt_tokens, total_tokens, and model from response["usage"] or response["meta"]["usage"].
      • Integrated into _ComponentRunWrapper for ComponentType.EMBEDDER, merging with existing embedding attributes and setting both LLM_MODEL_NAME and EMBEDDING_MODEL_NAME when available.
  • Tests:
    • Update test_openai_document_embedder_embedding_span_has_expected_attributes to assert LLM_MODEL_NAME, LLM_TOKEN_COUNT_PROMPT, and LLM_TOKEN_COUNT_TOTAL for OpenAI embeddings.

Written by Cursor Bugbot for commit 835cec0. This will update automatically on new commits. Configure here.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 18, 2025

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@Hansehart
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

@Hansehart Hansehart marked this pull request as ready for review October 18, 2025 15:06
@Hansehart Hansehart requested a review from a team as a code owner October 18, 2025 15:06
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 18, 2025
github-actions bot added a commit that referenced this pull request Oct 18, 2025
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

**dict(_get_embedding_attributes(arguments, response)),
**dict(_get_embedding_token_count_attributes(response)),
}
)
Copy link

Choose a reason for hiding this comment

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

Bug: Model Name Overwritten by Metadata

The EMBEDDING_MODEL_NAME attribute is set twice for embedder components: once from the component's configuration and again from the response metadata. The response metadata value overwrites the component's configured value, potentially causing inconsistent model name reporting.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Author

Choose a reason for hiding this comment

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

I configured response metadata as primary and component config as fallback! Is This acceptable?

@axiomofjoy axiomofjoy self-requested a review October 21, 2025 15:49
@nate-mar
Copy link
Contributor

@Hansehart Thanks so much for this! We very much appreciate it. Let us know if you need help resolving the failed CI checks. My colleague @axiomofjoy will take a look at this when he gets a chance.

@Hansehart
Copy link
Author

I’ll try to get this fixed in the next few days. Hints or pointers are very welcome though.

@caroger
Copy link
Collaborator

caroger commented Oct 23, 2025

I’ll try to get this fixed in the next few days. Hints or pointers are very welcome though.

Hi @Hansehart , you can see details of the CI/testing result by clicking into the checks. For instance - https://github.com/Arize-ai/openinference/actions/runs/18636229373/job/53302442562?pr=2323

It seems the CI failed at the mypy type checking steps.

For fast iterations - you can re-run the CI locally with uvx --with tox-uv tox run -e py313-ci-haystack-latest

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

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[feature request] Add token usage tracking for Haystack embedder components

3 participants