-
Couldn't load subscription status.
- Fork 149
feat(haystack): add token count tracking for embeddings #2323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat(haystack): add token count tracking for embeddings #2323
Conversation
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
| **dict(_get_embedding_attributes(arguments, response)), | ||
| **dict(_get_embedding_token_count_attributes(response)), | ||
| } | ||
| ) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
|
@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. |
|
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 |
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
_get_embedding_token_count_attributes()function to extract token counts from embedder responses_ComponentRunWrappertest_openai_document_embedder_embedding_span_has_expected_attributesto verify token count attributes_get_llm_token_count_attributes()Implementation Details
The new function supports two response formats:
response["usage"]response["meta"]["usage"]Token counts tracked:
LLM_TOKEN_COUNT_PROMPT- Input tokens consumedLLM_TOKEN_COUNT_TOTAL- Total tokens usedLLM_TOKEN_COUNT_COMPLETIONis 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
EMBEDDERspans, with tests.EMBEDDERspans now include token usage and model details._get_embedding_token_count_attributes()extractsprompt_tokens,total_tokens, and model fromresponse["usage"]orresponse["meta"]["usage"]._ComponentRunWrapperforComponentType.EMBEDDER, merging with existing embedding attributes and setting bothLLM_MODEL_NAMEandEMBEDDING_MODEL_NAMEwhen available.test_openai_document_embedder_embedding_span_has_expected_attributesto assertLLM_MODEL_NAME,LLM_TOKEN_COUNT_PROMPT, andLLM_TOKEN_COUNT_TOTALfor OpenAI embeddings.Written by Cursor Bugbot for commit 835cec0. This will update automatically on new commits. Configure here.