Commit graph

19670 commits

Author SHA1 Message Date
Kerry Lu
dbbebd07bf fix(proxy): flush the spend-log queue only for Responses turns the bridge reads back (#47614)
Every /v1/responses spend log asked its worker to flush the queue right away so a
chained previous_response_id could find the row. Only the chat-completions bridge
reads that row. Native Responses providers hold the conversation upstream, so their
rows now wait for the batch write like chat rows do

Backport of #47614 to stable/1.104.x
Cherry-picked from 423362bab2 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
de1e76bd13 feat(proxy): opt-in free-tier allowance for Google Search grounding spend (#47494)
* feat(proxy): opt-in free-tier allowance for Google Search grounding spend

* fix(proxy): declare the search free tier counter's service target

* test(cost_map): allow search_free_requests in the cost map schema

* fix(proxy): skip cache hits and never free more than the billed cost in the search free tier

---------

Backport of #47494 to stable/1.104.x
Cherry-picked from e63aec029a (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
223690801c feat(proxy): write one spend row per billed timed-out retry attempt (#47416)
_failure_spend_rows yields the final row plus one re-keyed row per lifted billed_failed_attempts entry (<request_id>-attempt-<position>, the attempt's usage, cost, retry count and deployment id), each written through DBSpendUpdateWriter.update_database so it also deducts from key, team and user spend

Backport of #47416 to stable/1.104.x
Cherry-picked from ad8ca533ca (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
23902fea6a fix(proxy): requeue spend logs when a Postgres restart tears down the prisma engine mid-write (#47571)
Backport of #47571 to stable/1.104.x
Cherry-picked from ccd0d4ea49 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
23b420d513 feat(proxy): lift every earlier billed timed-out retry attempt onto the failed request (#47490)
* feat(proxy): lift every earlier billed timed-out retry attempt onto the failed request

_billed_earlier_attempts reads the router's previous_models records, keeps the Timeout ones other than the final attempt (matched on deployment_id plus attempted_retries, since the count restarts after a fallback hop and the fallback handler re-logs the exhausting attempt) and prices each at the deployment that served it. post_call_failure_hook lifts the tuple as billed_failed_attempts; nothing writes it yet. previous_models joins the router-reserved metadata fields so a client-planted record never reaches it

* fix(proxy): price earlier timed-out attempts from the prompt estimate only

The final row may lift a mid-stream recovered usage (prompt plus delivered chunks) or a failed_attempts_usage sum; earlier attempts never produced output, so they are priced from _estimated_prompt_usage, and when failed_attempts_usage already bills them into the final row none is lifted again

---------

Backport of #47490 to stable/1.104.x
Cherry-picked from 7a4a255d1d (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
e2af1d25fe fix(decisions): log non-UTF-8 bytes as base64 so the request reaches validation and gets a 400 (#47564)
Backport of #47564 to stable/1.104.x
Cherry-picked from ce63f48ed2 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
bd0b38b4aa fix(litellm_proxy): forward tool_config cache_control_injection_points to the downstream proxy (#47570)
PR #47394 withheld cache_control_injection_points from the OpenAI SDK extra_body for
every provider on that path, which also covers litellm_proxy. The downstream LiteLLM
proxy applies the point itself, so litellm_proxy keeps forwarding it while OpenAI and the
other OpenAI-compatible providers still never receive it

Backport of #47570 to stable/1.104.x
Cherry-picked from 04e9ed4c52 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
b462563d14 fix(logging): report the pay-as-you-go entry billing used on Azure PTU spillover (#47567)
get_standard_logging_object_payload decided custom_pricing from
litellm_params alone, so on an Azure PTU spillover response
model_map_information kept the deployment's zeroed PTU entry while
response_cost already used the standard model rate. Reuse the billing
side LitellmLogging._custom_pricing_for so both read the same decision

Backport of #47567 to stable/1.104.x
Cherry-picked from 4222523261 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
e71d121736 fix(proxy): price a billed failure row at the deployment's own rate like the success path (#47437)
Backport of #47437 to stable/1.104.x
Cherry-picked from fb555b00dc (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:11 +00:00
Kerry Lu
3b9892eb1d fix(proxy): price the estimated input tokens on a client-cancelled (499) request's spend row (#47436)
* fix(proxy): price the estimated input tokens on a client-cancelled (499) request's spend row

* fix(proxy): read the provider handoff before the proxy-only error handler stamps it so a pre-handoff 499 is not priced

* fix(proxy): drop the pre-logging handoff read now that pre_call no longer stamps proxy-only failures

---------

Backport of #47436 to stable/1.104.x
Cherry-picked from 0e7c4908bb (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 01:14:10 +00:00
Kerry Lu
f585b558e9 fix(batches): apply the Vertex regional endpoint uplift to batch cost (#47466)
* fix(batches): apply the Vertex regional endpoint uplift to batch cost

batch_cost_calculator takes vertex_location and multiplies by the model's
regional_endpoint_uplift_multiplier through get_vertex_regional_endpoint_uplift,
and every batch cost path (aretrieve_batch pricing, completed-batch logging,
native Vertex batch rows, the proxy batch cost poll and its per-line items)
resolves the deployment's location and passes it down

Co-authored-by: Mrun25 <165297479+Mrun25@users.noreply.github.com>

* test(logging): point the resolver tests at resolve_vertex_location_for_cost

---------

Backport of #47466 to stable/1.104.x
Cherry-picked from c6c303a618 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Mrun25 <165297479+Mrun25@users.noreply.github.com>
2026-10-21 01:14:10 +00:00
Kerry Lu
96684a2570 fix(vertex_ai): bill per-character text embeddings on Google's billableCharacterCount (#47460)
Vertex text embedding predict responses report metadata.billableCharacterCount, the
unit Google bills text-embedding-005 and text-multilingual-embedding-002 on. Carry it
into usage.prompt_tokens_details.character_count and, when the model carries
input_cost_per_character, bill that count instead of the token count so the prompt is
charged under one unit. Per-token embeddings (gemini-embedding-001) are unchanged.

Backport of #47460 to stable/1.104.x
Cherry-picked from d29200fa05 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 00:05:41 +00:00
Kerry Lu
744f63b8db fix(logging): do not stamp a provider handoff when pre_call runs for a proxy-only failure (#47456)
Backport of #47456 to stable/1.104.x
Cherry-picked from 3fa1587f43 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 00:04:36 +00:00
Kerry Lu
ee8a1a5abd fix(proxy): price the estimated input tokens on a read-timed-out request's spend row (#47435)
Backport of #47435 to stable/1.104.x
Cherry-picked from 7b76b2b15f (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 00:03:28 +00:00
Kerry Lu
811cecdc3b fix(anthropic): apply tool_config cache-control points on chat and /v1/messages (#47394)
* fix(anthropic): apply tool_config cache-control points on chat and /v1/messages

A cache_control_injection_points entry with location tool_config was spread into
the Anthropic request body on /v1/chat/completions, which Anthropic rejected with
400 Extra inputs are not permitted, and the native /v1/messages path dropped the
point silently. Both paths now put the point's control on the last tool Anthropic
caches and never forward the key.

Co-authored-by: Tan Nguyen <tan@pressingly.net>

* refactor(anthropic): validate tool_config inputs instead of casting them

Keeps the lint gates flat: the helper takes untyped tools and points and
narrows them with the existing object validators, the chat transform builds
the body with the marked tools instead of writing into optional_params, and
the handler reads a typed view of kwargs.

* fix(openai): keep tool_config cache points out of extra_body, add integration test

The cache hook hands a tool_config point on for the provider step, and for
providers that call through the OpenAI SDK every non-OpenAI param rides in
extra_body, so OpenAI answered 400 Unrecognized request argument supplied.
The new integration file drives a live proxy against scripted Anthropic,
Bedrock Converse and OpenAI upstreams for /v1/chat/completions and
/v1/messages.

* test(integration): cover tool_config cache points on bridged /v1/messages

Three new cases on /v1/messages for models without a native Anthropic
messages path: OpenAI through the Responses bridge and Gemini through the
chat bridge never see cache_control_injection_points in the body, and
Bedrock Nova through the chat bridge gets the cachePoint on toolConfig

---------

Backport of #47394 to stable/1.104.x
Cherry-picked from 93b79d2587 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Tan Nguyen <tan@pressingly.net>
2026-10-21 00:01:31 +00:00
Kerry Lu
f0fb999131 fix(cost): bill Gemini audio/image/video prompt tokens at the resolved input rate when no modality rate exists (#47373)
* fix(cost): bill Gemini audio/image/video prompt tokens at the resolved input rate when no modality rate exists

Co-authored-by: shivam <shivam@berri.ai>

* style(cost): ruff format the modality cost calls

---------

Backport of #47373 to stable/1.104.x
Cherry-picked from 09e9193514 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: shivam <shivam@berri.ai>
2026-10-20 23:57:26 +00:00
Kerry Lu
d64ac3998c feat(router): let a deployment set a timeout per service_tier (#47406)
* feat(router): let a deployment set a timeout per service_tier

Adds timeout_by_service_tier and stream_timeout_by_service_tier to a deployment's
litellm_params. A request whose service_tier matches a key uses that value in place
of the deployment's timeout or stream_timeout. A timeout the client sends still wins
and requests without a matching tier are unchanged

Co-authored-by: adc77 <amandogra2016@gmail.com>

* style(router): sort the request_timeout_resolver import

* fix(router): read the service tier maps without the untyped params get

* fix(router): look the service tier timeout up through a typed get

---------

Backport of #47406 to stable/1.104.x
Cherry-picked from 62ce515d15 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: adc77 <amandogra2016@gmail.com>
2026-10-20 23:56:28 +00:00
Kerry Lu
826f404735 fix(decisions): log every request field the caller sent on /v1/systemone and /v1/decisions (#47183)
* fix(decisions): log every request field the caller sent on /v1/systemone and /v1/decisions

* fix(decisions): serialize the logged request with model_dump_json so no mutable dict is built

* refactor(decisions): drop the state-only messages pick in function_setup, prepare_call logs the request

* fix(decisions): return the single logged message so no mutable list type is annotated

* fix(decisions): log the caller's raw request fields before validation so a rejected request is still recorded

* fix(decisions): serialize the raw request fields with pydantic_core so question objects and mapping proxies log as JSON

* fix(decisions): log the caller request at the SDK entry so requests rejected before prepare_call are recorded too

* test(decisions): assert the exact BadRequestError for the rejected-questions case

---------

Backport of #47183 to stable/1.104.x
Cherry-picked from 53821f6654 (main)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-20 23:54:07 +00:00
Joshua Valluru
e109f8977d fix(proxy): restore legacy token count input precedence 2026-10-19 17:50:09 -07:00
mateo-berri
5e71466355 fix(count_tokens): normalize Google system parts and null tools 2026-10-19 16:53:38 -07:00
mateo-berri
3f3fb2ac60 fix(count_tokens): omit local PDF parsing from stable backport 2026-10-19 15:20:52 -07:00
mateo-berri
704a15720a test(count_tokens): type PDF budget regression cases 2026-10-19 15:14:43 -07:00
mateo-berri
c881f7cfc1 fix(count_tokens): bound PDF parsing and preserve legacy fallback 2026-10-19 15:10:51 -07:00
mateo-berri
498095ee1b fix(count_tokens): retain legacy translated content fallback 2026-10-19 14:38:30 -07:00
mateo-berri
e9e7e32142 test(count_tokens): make strict error envelope coverage explicit 2026-10-19 14:30:57 -07:00
mateo-berri
d522e2e09b fix(count_tokens): preserve legacy provider error fallback on stable
Defer the breaking error policy from #46121. Adapt the #46777 count settings flow to attempt a labelled local estimate for provider failures across providers and configured count models.

Keep provider errors when local counting is disabled or cannot estimate the request, and test the shared behavior across HTTP error statuses.
2026-10-19 14:28:17 -07:00
mateo-berri
828ddf0c37 fix(count_tokens): narrow stable backport to the October 19 patch
Keep #43900, #45301, #46121, #46784, #46759, #46767, #46772, #46815, #47057 and #46777. Port only the Gemini prerequisites from #46430.

Remove the deferred Bedrock Mantle behavior and unrelated picks from the final tree. Restore the PDF test provider fixture and deployment, and retain required test imports.
2026-10-19 14:03:42 -07:00
mateo-berri
94828be8b0 test(integration): add the openai_wire helper the backported chat template wire test imports
Hand-ported verbatim to stable/1.104.x from main.
2026-10-19 12:35:34 -07:00
Mateo Wang
c3d07e70d6 fix(count_tokens)!: count tools the way DeepSeek V4 and Qwen3.5 chat templates render them (#47173)
Backport of #47173 to stable/1.104.x.
Cherry-picked from c82e8244a5 (main).
2026-10-19 12:35:33 -07:00
Mateo Wang
b6f1a033de feat(count_tokens)!: configurable token_count_framing and token_count_model, count source in the response (#46777)
Backport of #46777 to stable/1.104.x.
Cherry-picked from 75faaeab64 (main).
2026-10-19 12:35:33 -07:00
mateo-berri
52de3b0a29 test(integration): add the support helpers the backported count_tokens wire tests import
Hand-ported to stable/1.104.x from main (bedrock_runtime_peer, provider, _cache_control_marks_support verbatim, graceful_stop_seconds at the line's 30s stop wait).
2026-10-19 12:35:32 -07:00
Mateo Wang
748e03088d fix(count_tokens)!: answer 400 for Vertex partner models without a count API and OpenAI-shaped provider errors on /v1/responses/input_tokens (#47163)
Backport of #47163 to stable/1.104.x.
Cherry-picked from ef1d3959a0 (main).
2026-10-19 12:35:32 -07:00
Mateo Wang
da5a9ffd2e ci(cost map): remove the generated model prices JSON schema (#47064)
Backport of #47064 to stable/1.104.x.
Cherry-picked from ae6f7f6613 (main).

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-19 12:35:32 -07:00
Mateo Wang
a4be1c1d13 ci(cost map): classify token_count_model as an object key in the schema generator (#47057)
Backport of #47057 to stable/1.104.x.
Cherry-picked from 6e191a58f1 (main).
2026-10-19 12:35:32 -07:00
Mateo Wang
b1037d2121 fix(proxy)!: count prompt-only token_counter requests with the provider when call_endpoint is set (#46839)
Backport of #46839 to stable/1.104.x.
Cherry-picked from 2dc6aafa6b (main).
2026-10-19 12:35:32 -07:00
Mateo Wang
4d5a6577c2 fix(proxy)!: count endpoints return errors in their source API shape instead of silent local counts (#46815)
Backport of #46815 to stable/1.104.x.
Cherry-picked from b21e659c24 (main).

Co-authored-by: Mrinal Chanshetty <mrinal@berri.ai>
2026-10-19 12:35:32 -07:00
Mateo Wang
13b4537c81 fix(count_tokens): return a 400 naming Anthropic blocks the OpenAI and Gemini count can't convert (#46772)
Backport of #46772 to stable/1.104.x.
Cherry-picked from 79ca820933 (main).

Co-authored-by: Mrinal Chanshetty <mrinal@berri.ai>
2026-10-19 12:35:32 -07:00
Mateo Wang
350def8ccf fix(count_tokens): count Anthropic-format requests for Gemini the way /v1/messages inference converts them (#46767)
Backport of #46767 to stable/1.104.x.
Cherry-picked from dba86bec28 (main).

Co-authored-by: Mrinal Chanshetty <mrinal@berri.ai>
2026-10-19 12:35:32 -07:00
Mateo Wang
531b66719a fix(count_tokens): count Anthropic-format requests for OpenAI the way /v1/messages inference converts them (#46759)
Backport of #46759 to stable/1.104.x.
Cherry-picked from d25b58f744 (main).

Co-authored-by: Mrinal Chanshetty <mrinal@berri.ai>
2026-10-19 12:35:32 -07:00
Mateo Wang
c99e4b86db ci(cost map): classify token_count_framing and token_count_fallback_model in the schema generator (#46784)
Backport of #46784 to stable/1.104.x.
Cherry-picked from c6a4437e98 (main).
2026-10-19 12:20:03 -07:00
mrinal-berri
fe9e6381a3 fix(count_tokens): count system and tools on Vertex Gemini, fall back to Mantle on Bedrock 404 (#46430)
Backport of #46430 to stable/1.104.x.
Cherry-picked from 08a24d391a (main).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-19 12:20:03 -07:00
Krrish Dholakia
c703543b30 fix(proxy): raise on provider count_tokens errors instead of falling back to tiktoken (#46121)
Backport of #46121 to stable/1.104.x.
Cherry-picked from 719c44db3f (main).

Co-authored-by: Krrish Dholakia <krrishdholakia@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: yuneng-jiang <yuneng@berri.ai>
2026-10-19 12:20:03 -07:00
devin-ai-integration[bot]
095c4eaa41 fix(anthropic): count a leading system run through count_tokens' system parameter (#45463)
Backport of #45463 to stable/1.104.x.
Cherry-picked from d34ad35281 (main).

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-19 12:20:03 -07:00
devin-ai-integration[bot]
0b148c95ee fix(token_counter): price a base64 PDF document per page instead of as one image (#45301)
Backport of #45301 to stable/1.104.x.
Cherry-picked from 50ab9a3211 (main).

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-19 12:20:03 -07:00
devin-ai-integration[bot]
8222299f1d fix(bedrock): count tokens on bedrock-mantle when bedrock-runtime cannot count a Claude model (#45317)
Backport of #45317 to stable/1.104.x.
Cherry-picked from 577d74c1ce (main).
2026-10-19 12:20:03 -07:00
tin-berri
2f968f566b fix(anthropic): normalize images for provider token counting (#45185)
Backport of #45185 to stable/1.104.x.
Cherry-picked from 6befb9ad7d (main).
2026-10-19 11:36:30 -07:00
devin-ai-integration[bot]
a6a1956a17 fix(vertex_ai): forward system and tools to partner model count_tokens (#43900)
Backport of #43900 to stable/1.104.x.
Cherry-picked from 6d73fa6b49 (main).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: jesus <jesus@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-19 11:36:30 -07:00
devin-ai-integration[bot]
9d0cca7ed0 fix(token_counter): count Gemini function_declarations tools (#43417)
Backport of #43417 to stable/1.104.x.
Cherry-picked from 8e6d99d74a (main).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-19 11:36:30 -07:00
mateo-berri
c47b760253 test(proxy): import Final in the line's batch endpoint tests
The tests picked from #47067 annotate module constants with Final, which main's copy imports and this line's did not, so the module failed to import on Python 3.12.
2026-10-19 09:10:10 -07:00
Mrunmayee Daware
e87e661118 fix(proxy): authorize the public name of a team-scoped model on batch retrieve/cancel (#47067)
Backport of #47067 to stable/1.104.x.
Cherry-picked from e979bd0ecf (main).
Conflict: main renamed the model access helper to check_model_access_helper, while stable/1.104.x still names it _check_model_access_helper, so the new owning-team check calls it by the line's name.
Line adaptation: the line's test_auth_utils.py had no module-level Router import, which main's copy has and the picked helper uses, so it is added.
2026-10-19 08:57:08 -07:00