fix(rag): correctly attribute and track query embedding usage for active sessions#3603
Open
Piyush0049 wants to merge 1 commit into
Open
fix(rag): correctly attribute and track query embedding usage for active sessions#3603Piyush0049 wants to merge 1 commit into
Piyush0049 wants to merge 1 commit into
Conversation
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.
Description
This PR resolves a long-standing TODO in the RAG
Managerto properly track, aggregate, and emit embedding token usage for agent search queries, ensuring they are accurately billed and attributed to the active session telemetry.Previously,
VectorStorerelied on a hidden globalusageHandlercallback attached to theEmbedder. This blindly fired during any embedding generation and logged the tokens as Indexing usage. As a result, tokens consumed by active RAG queries during a chat session were miscategorized as background file-indexing operations and completely missed by the session token tracker.Changes Made
Embedderfor Purity: Removed theusageHandlercallback side-effect.Embed()andEmbedBatch()now explicitly return(tokens, cost)alongside the vectors.VectorStorenow manually triggersrecordUsage()only duringindexFile(). Query tokens are cleanly returned up the stack.Strategy.Query()was updated to return a newtypes.Usagestruct, allowingManager.Query()to cleanly aggregate usage across parallel search strategies (e.g., in a hybrid RAG configuration).handleQueryRAG) to capture the aggregated usage and emit it viatelemetry.RecordTokenUsage(ctx, ...). Because the tool execution context inherently carries thetelemetry.Client, this seamlessly solves the missing session attribution.Testing
go build ./...passes without errors.task test ./pkg/rag/...to ensure all strategy mocks, implementations, and fallback batches aggregate token counts safely without regressions.