litellm - 💡(How to fix) Fix [Bug]: [Bug] Anthropic Messages Adapter: response.id doesn't match spend logs request_id for non-Anthropic backends [2 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

Root Cause Analysis

Fix Action

Fixed

RAW_BUFFERClick to expand / collapse

Check for existing issues

  • I have searched the existing issues and checked that my issue is not a duplicate.

What happened?

Description

When a client uses the Anthropic /v1/messages protocol to call LiteLLM, and the backend is a non-Anthropic provider (e.g., OpenAI), the id field in the response body (or streaming chunks) does not match the request_id stored in the spend logs. This makes it impossible for clients to correlate responses IDs with billing/usage data.

Scenario

  • Client request: Anthropic /v1/messages protocol
  • Backend model: OpenAI (or any non-Anthropic provider)
  • Code path: LiteLLMMessagesToCompletionTransformationHandler (adapter path)
  • Affected modes: Both streaming and non-streaming

Steps to Reproduce

Reproduction Steps

  1. Configure LiteLLM proxy with a non-Anthropic backend (e.g., OpenAI) exposed via the Anthropic /v1/messages endpoint
  2. Send a request to the /v1/messages endpoint:

curl -X POST http://localhost:4000/v1/messages
-H "Content-Type: application/json"
-H "Authorization: Bearer $API_KEY"
-d '{ "model": "gpt-4o", "messages": [{"role": "user", "content": "Hello"}], "max_tokens": 100 }'

  1. Observe the response body: - Non-streaming: Response contains "id": "chatcmpl-..." (the OpenAI response ID) - Streaming: First SSE chunk contains "id": "msg_<uuid>" (newly generated UUID)
  2. Query spend logs via /spend/logs?request_id=...
  3. Result: The request_id in spend logs is a UUID (litellm_call_id), which does not match either: - The non-streaming response id (chatcmpl-...) - The streaming response id (msg_<uuid>)

Expected Behavior

The response id should match the request_id in spend logs, so clients can reliably correlate responses with usage/billing records.

Actual Behavior

The response id and spend log request_id are completely different values:

ModeResponse idSpend Log request_id
Non-streamingBackend provider ID (e.g., chatcmpl-abc123)litellm_call_id (UUID)
Streamingmsg_<uuid> (newly generated)litellm_call_id (UUID)

Root Cause Analysis

Non-streaming path (litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py:1379): translated_obj = AnthropicMessagesResponse( id=response.id, # <-- Uses backend provider's ID directly ... )

Streaming path (litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py:93 and :233):

In next and anext:

"id": "msg_{}".format(uuid.uuid4()), # <-- New UUID generated here

Spend logging (litellm/proxy/spend_tracking/spend_tracking_utils.py:161-171): def get_spend_logs_id(call_type: str, response_obj: dict, kwargs: dict) -> Optional[str]: id = response_obj.get("id") or kwargs.get("litellm_call_id") return id

The spend log uses litellm_call_id (a UUID generated at request start), but the adapter layer:

  1. For non-streaming: uses the backend provider's response ID
  2. For streaming: generates a brand new msg_<uuid> ID

Neither path propagates litellm_call_id to be used as the response ID.

Suggested Fix

Propagate litellm_call_id through the adapter layer and use it as the id in the Anthropic-format response:

  1. Non-streaming: In translate_openai_response_to_anthropic(), use the litellm_call_id (with or without msg_ prefix) instead of response.id
  2. Streaming: In AnthropicStreamWrapper, use litellm_call_id instead of generating a new UUID

This ensures the response ID matches the request_id in spend logs, enabling proper correlation.

Environment

  • LiteLLM version: 1.84.0rc2
  • Backend: OpenAI (or any non-Anthropic provider via adapter)
  • Protocol: Anthropic /v1/messages

Relevant log output

What part of LiteLLM is this about?

Proxy

What LiteLLM version are you on ?

v1.84.0

Twitter / LinkedIn details

No response

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

litellm - 💡(How to fix) Fix [Bug]: [Bug] Anthropic Messages Adapter: response.id doesn't match spend logs request_id for non-Anthropic backends [2 pull requests]