Skip to content

fix(anthropic): route msgbatch_* IDs to batch results endpoint - #27998

Closed
Rana-Hassan7272 wants to merge 9 commits into
BerriAI:mainfrom
Rana-Hassan7272:fix/batch-cost-v3
Closed

Rana-Hassan7272 wants to merge 9 commits into
BerriAI:mainfrom
Rana-Hassan7272:fix/batch-cost-v3

Conversation

@Rana-Hassan7272

@Rana-Hassan7272 Rana-Hassan7272 commented May 15, 2026 •

Copy link
Copy Markdown

Summary

Fixes #27944

Root Causes Fixed

Root cause 1 — Wrong endpoint routing
transform_file_content_request routed msgbatch_* IDs to /v1/files/{id}/content
(Files API) which returns 404. Fixed by detecting msgbatch_* prefix and routing
to /v1/messages/batches/{id}/results.

Root cause 2 — Anthropic JSONL format not translated
Anthropic batch results use {result.message.usage.input_tokens} but cost/usage
helpers expect OpenAI format {response.body.usage.prompt_tokens}. Added
_translate_anthropic_batch_item_to_openai() to normalize before processing.

Root cause 3 — model_info={} short-circuits cost calculation
deployment_model_info fell back to {} instead of None, causing
batch_cost_calculator to skip global pricing table lookup entirely.

Testing

  • msgbatch_* IDs route to correct batch results endpoint ✅
  • Anthropic JSONL correctly translated to OpenAI format ✅
  • model_info fallback fixed to None ✅
  • Unit tests added for all three fixes ✅

@greptile-apps

greptile-apps Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a routing bug where msgbatch_* batch-result IDs were incorrectly sent to the Files API (/v1/files/{id}/content), producing 404 errors and causing batch cost tracking to silently record 0 tokens and $0. The fix adds a prefix guard that redirects these IDs to the correct endpoint (/v1/messages/batches/{id}/results).

  • transform_file_content_request now checks for the msgbatch_ prefix and returns the batch-results URL; all other file IDs continue to use the existing files endpoint unchanged.
  • A new mock unit test verifies the new branch produces the correct URL and empty params.

Confidence Score: 4/5

The change is narrowly scoped and only affects IDs that previously caused 404 errors, so it introduces no regression risk for normal file IDs.

The routing logic is correct, the URL is constructed consistently with the rest of the file, and the new test exercises the changed branch without real network calls. The only finding is a minor single-quote vs double-quote inconsistency in the new line.

No files require special attention; transformation.py is the only changed production file and the fix is a one-line guard.

Important Files Changed

Filename Overview
litellm/llms/anthropic/files/transformation.py Adds a prefix guard in transform_file_content_request to route msgbatch_* IDs to the batch results endpoint instead of the files endpoint; logic is correct, minor quote-style inconsistency.
tests/test_litellm/llms/anthropic/files/test_anthropic_files_transformation.py Adds a unit test for the new msgbatch_* routing branch; uses mocks only, covers the happy path for the new code.

Reviews (1): Last reviewed commit: "fix(anthropic): route msgbatch_* IDs to ..." | Re-trigger Greptile

# record 0 tokens and $0 cost for completed batches.
# Correct endpoint: /v1/messages/batches/{id}/results
# Ref: https://github.com/BerriAI/litellm/issues/27944
if file_id and file_id.startswith('msgbatch_'):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Minor style inconsistency: the rest of the file uses double-quoted string literals, but the new startswith check uses single quotes. Keeping the style uniform with the surrounding code makes diffs easier to read.

Suggested change
if file_id and file_id.startswith('msgbatch_'):
if file_id and file_id.startswith("msgbatch_"):

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@veria-ai

veria-ai Bot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 2 · PR risk: 0/10

Bytechoreographer and others added 4 commits June 24, 2026 12:45
_get_models_from_db ran find_many() with no ORDER BY, so the model list
came back in Postgres' heap-scan order. Toggling a model rewrites its row
under MVCC and clear_cache() rebuilds the router from a fresh unordered
scan, so the toggled model jumps position and the UI list reshuffles on
every reload; worse when a filter shows only a slice. Order by created_at
then model_id so the order is stable regardless of updates.
Greptile flagged that ModelRepository.find_all() (and the paginated search
fetch) still ran find_many() without an ORDER BY, so the reshuffle could
resurface through those paths. The search fetch is worse: it applies take
(LIMIT) with no order, so which rows land on a filtered page is itself
non-deterministic and changes on every refresh.

Centralize the order spec as MODEL_LIST_ORDER (created_at asc, model_id
tie-break) and apply it to find_all and the paginated search fetch as well.
Resolves the UP006 strict-rule budget overflow that failed the lint CI
check; List[Dict[str, str]] became list[dict[str, str]].
…_list_deterministic_order

fix(proxy): load DB models in a deterministic order
@MishaVernik

Copy link
Copy Markdown

Lets merge this if possible, we also need this PR

…fix model_info fallback

Root cause 2: transform_file_content_response returned raw Anthropic JSONL
but calculate_batch_cost_and_usage expected OpenAI format. Added
_translate_anthropic_batch_item_to_openai() to convert Anthropic
{result.message.usage.input_tokens} to OpenAI {response.body.usage.prompt_tokens}.

Root cause 3: deployment_model_info fell back to {} (falsy) instead of None,
causing batch_cost_calculator to skip the global pricing table lookup.

Fixes BerriAI#27944
@codecov

codecov Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Rana-Hassan7272

Copy link
Copy Markdown
Author

Lets merge this if possible, we also need this PR

Hi, rebased onto litellm_oss_staging_06_06_2026 and all checks are green except OSV Scan (dependency scan on the lockfile; not introduced by this PR). Ready to merge when you have a moment. Fixes #27944

@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_oss_staging_06_06_2026 to main September 23, 2026 15:45
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 15:45
@CLAassistant

CLAassistant commented Nov 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mateo-berri

Copy link
Copy Markdown
Contributor

Fixed on main by #32307, which routes message batch results to the right endpoint and prices them. Reopen if something is still missing

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.

[Bug]: Anthropic batch costs always 0 — transform_file_content_request routes msgbatch_* IDs to wrong endpoint

6 participants