fix: claude code req traces on langfuse - #23259
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Greptile SummaryThis PR fixes missing API-key tracking fields in Langfuse traces for Anthropic-native endpoints like Changes implemented:
Verification: All four concerns raised in earlier review rounds are properly resolved: type guards protect against non-dict types, truthiness checks handle empty dicts, both dictionaries are copied to avoid aliasing, and metadata precedence is correctly maintained. Confidence Score: 5/5
Sequence DiagramsequenceDiagram
participant CC as Claude Code
participant Proxy as LiteLLM Proxy
participant FS as function_setup
participant LP as litellm_params
participant LF as Langfuse Callback
CC->>Proxy: POST /v1/messages (no metadata key)
Proxy->>Proxy: Store tracking in kwargs litellm_metadata
Proxy->>FS: function_setup(**kwargs)
FS->>FS: Check isinstance(kwargs litellm_metadata, dict)
FS->>LP: litellm_params litellm_metadata = copy
alt metadata is absent or empty - primary fix
FS->>LP: litellm_params metadata = copy of litellm_metadata
else metadata is non-empty - takes precedence
FS->>LP: litellm_params metadata = existing metadata
Note over LP: litellm_metadata stored separately for merge_litellm_metadata
end
FS-->>Proxy: logging_obj, kwargs
Proxy->>LF: callback fires
LF->>LP: reads litellm_params metadata
LP-->>LF: user_api_key_hash, team_id visible
Last reviewed commit: 17804ed |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
7 tasks
fzowl
pushed a commit
to fzowl/litellm
that referenced
this pull request
Jun 24, 2026
…reqs fix: claude code req traces on langfuse
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.
fix: claude code req traces on langfuse (propagate litellm_metadata)
Changes
Previously, requests made to Anthropic-specific endpoints (like
/v1/messages) via tools like "Claude Code" were missing crucial API tracking information in their Langfuse traces (such as theuser_api_key_hash,user_api_key_alias, and team IDs).Root Cause:
To avoid conflicting with the provider's native
metadatafields for these specific endpoints, LiteLLM proxy stores its internal tracking data inside a different key namedlitellm_metadatainstead of the standardmetadatakey. However, observability callbacks like Langfuse strictly read fromlitellm_params["metadata"]. Because this dictionary wasNonefor/v1/messages, the tracking data never reached Langfuse, causing the requests to look anonymous in the logs.Changes Implemented:
function_setupin litellm/utils.py to explicitly check forlitellm_metadata.litellm_params.get("metadata")is empty/None, it now automatically populates it with the contents ofkwargs["litellm_metadata"].test_litellm_logging.py:test_function_setup_litellm_metadata_populates_metadata: To ensure Langfuse callbacks can correctly read the injected metadata.test_function_setup_metadata_takes_precedence_over_litellm_metadata: To verify that if a user manually sets explicitmetadata, it is preserved and prioritized properly.