Skip to content

fix: claude code req traces on langfuse - #23259

Merged
Harshit28j merged 4 commits into
mainfrom
litellm_claude_missing_reqs
Mar 10, 2026
Merged

fix: claude code req traces on langfuse#23259
Harshit28j merged 4 commits into
mainfrom
litellm_claude_missing_reqs

Conversation

@Harshit28j

@Harshit28j Harshit28j commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

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 the user_api_key_hash, user_api_key_alias, and team IDs).

Root Cause:
To avoid conflicting with the provider's native metadata fields for these specific endpoints, LiteLLM proxy stores its internal tracking data inside a different key named litellm_metadata instead of the standard metadata key. However, observability callbacks like Langfuse strictly read from litellm_params["metadata"]. Because this dictionary was None for /v1/messages, the tracking data never reached Langfuse, causing the requests to look anonymous in the logs.

Changes Implemented:

  • Updated function_setup in litellm/utils.py to explicitly check for litellm_metadata.
  • If litellm_params.get("metadata") is empty/None, it now automatically populates it with the contents of kwargs["litellm_metadata"].
  • Added two comprehensive unit tests in test_litellm_logging.py:
    1. test_function_setup_litellm_metadata_populates_metadata: To ensure Langfuse callbacks can correctly read the injected metadata.
    2. test_function_setup_metadata_takes_precedence_over_litellm_metadata: To verify that if a user manually sets explicit metadata, it is preserved and prioritized properly.
image

@vercel

vercel Bot commented Mar 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Error Error Mar 10, 2026 1:16pm

Request Review

@greptile-apps

greptile-apps Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes missing API-key tracking fields in Langfuse traces for Anthropic-native endpoints like /v1/messages. The root cause was that LiteLLM stores internal tracking data in litellm_metadata (to avoid conflicts with provider metadata), but observability callbacks read from litellm_params["metadata"].

Changes implemented:

  • Updated function_setup in litellm/utils.py to explicitly check for litellm_metadata with an isinstance type guard (line 1146)
  • When litellm_params["metadata"] is absent or falsy (truthiness check, line 1152), it is populated with a copy of kwargs["litellm_metadata"] (line 1153)
  • Both assignments use .copy() to prevent shared-reference aliasing
  • Three comprehensive unit tests added in test_litellm_logging.py:
    1. Primary scenario: no metadata, only litellm_metadata
    2. Precedence scenario: explicit metadata takes priority, litellm_metadata stored separately
    3. Empty dict fallback: metadata={} correctly triggers fallback to litellm_metadata

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

  • Safe to merge. Fix is narrowly scoped, all defensive checks are in place, and test coverage is solid.
  • All four previously-flagged concerns from earlier review threads (type guards, truthiness checks, .copy() on both sides, reference aliasing prevention) are properly implemented in the current code. Unit tests verify the core scenarios and edge cases with mock-only coverage. The fix is focused and well-tested, with no regressions or safety issues detected.
  • No files require special attention.

Sequence Diagram

sequenceDiagram
    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
Loading

Last reviewed commit: 17804ed

Comment thread litellm/utils.py Outdated
Comment thread litellm/utils.py Outdated
Comment thread litellm/utils.py Outdated
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Comment thread litellm/utils.py Outdated
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@Harshit28j
Harshit28j merged commit e1c2c36 into main Mar 10, 2026
73 of 98 checks passed
@ishaan-berri
ishaan-berri deleted the litellm_claude_missing_reqs branch March 26, 2026 22:29
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
…reqs

fix: claude code req traces on langfuse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant