CCM-33842: Improve LiteLLM trace quality for cost attribution#9
Merged
t-santoshsahu merged 4 commits intoJul 2, 2026
Merged
Conversation
Fix duplicate litellm_request spans and enrich token-bearing spans so downstream cost attribution can reliably resolve model identity and usage. - Deduplicate spans: guard against LiteLLM async entry points (acompletion/aembedding) re-dispatching to their sync counterparts via a copied context, which previously produced a second empty nested span. - Guarantee gen_ai.response.model on token-bearing spans, falling back to the request model when the provider omits the response model. - Capture the actual executing Bedrock model id (x-amzn-bedrock-model-id) from LiteLLM _hidden_params into aws.bedrock.execution_model_id so inference-profile requests can be distinguished from the executing model. - Add opt-in, bounded, sanitized raw usage capture (HA_GEN_AI_RAW_CAPTURE_ENABLED) as a resilience fallback against LiteLLM response shape/label changes. - Add regression tests for single-span emission, dict/object embedding usage, response-model fallback, Bedrock execution model, and raw capture. Co-authored-by: Cursor <cursoragent@cursor.com>
|
|
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
t-santoshsahu
approved these changes
Jul 2, 2026
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.
Summary
Improves
liteLLMGenAI trace quality so token-bearin spans reliably expose model identity, token usage, and enough raw data to survive provider/library shape changes.Verified against an exported trace set (
data3.json, collected from llm-model-service in QA): every LLM trace carried twolitellm_requestspans (one enriched, one empty), and only ~50% of spans hadgen_ai.response.model+ input tokens.data3.json
Changes
acompletion/aembedding) internally re-dispatch to their sync counterparts (completion/embedding) viarun_in_executorwith a copied context. Since all four functions are wrapped, the nested sync call created a second, emptylitellm_requestspan. Added acontextvarsre-entrancy guard so each provider call yields exactly one span.gen_ai.response.model. Token-bearing spans now always set the response model, falling back to the request model when the provider omits it, so spans can join rate cards by provider + model.x-amzn-bedrock-model-idfrom LiteLLM_hidden_params(additional headers /ResponseMetadata.HTTPHeaders) intoaws.bedrock.execution_model_id, distinguishing the requested inference profile from the actual executing model.HA_GEN_AI_RAW_CAPTURE_ENABLED(default off). Serializes the usage object intogen_ai.response.usage.raw(+..._truncated), bounded byHA_GEN_AI_RAW_CAPTURE_MAX_BYTES(default 8192) and sanitized to omit prompt/content/header/credential fields. Provides resilience if LiteLLM changes response labels.Tests
Added regression tests in
test/instrumentation/litellm/:litellm_requestspan._hidden_params.All 14 LiteLLM instrumentation tests pass.
Config
HA_GEN_AI_RAW_CAPTURE_ENABLED(defaultfalse)HA_GEN_AI_RAW_CAPTURE_MAX_BYTES(default8192)Expected impact
litellm_requestspan per provider call (was two).gen_ai.response.model+ input token usage present on all token-bearing spans.Made with Cursor