Repository navigation
Conversation
…onse_id replays it The spend log writer cut every stored string over 2048 characters, which included Claude thinking signatures and Gemini thought signatures. The bridge replays those rows for previous_response_id, so the provider refused the follow-up with a 400 Response rows now keep every field a provider verifies whole, in both the Rust and Python writers. Stored requests stay bounded. Replay leaves out signed reasoning that a stored row holds cut or masked, so rows written before this change answer instead of failing
Message redaction leaves Gemini thought signatures, tool call ids that carry one and Responses encrypted reasoning in the row. Keeping them whole let a follow-up recover content that redaction had removed, so those rows keep the bound every other string has and replay leaves the cut state out
…d truncation marker A pre-upgrade Gemini row holds its signature a second time inside the tool call id, so replay now trims a cut id to the part before the signature. A stored string counts as cut only where the marker sits at the cut point, so thinking that quotes the marker stays. A list-typed `type` in stored input no longer stops the replay, and a tool result pairs by id prefix only when one side carries no signature
… signature is lost Releases through v1.82.0-stable cut stored strings with a marker that has no safeguard note, and replay did not recognize it, so follow-ups on those rows still answered 400. The warning also fired when only a tool output's own id was trimmed, which loses nothing
A worker that still remembered a turn's tool calls put one assistant message per call in front of the tool outputs, after the stored turn that already held them all. Claude refused the doubled calls with a 400, so a thinking turn with parallel calls could not be continued
…and leave marked calls alone Dropping a repeated call lost the only whole copy of its arguments when the stored call held them cut or redacted, so the kept call now takes them back. A turn with signed thinking keeps its stored arguments, so every worker replays the same turn and the signature stays valid A call the client marked with cache_control or other fields, a call with an empty id and a stored turn that reuses an id are no longer dropped, and an id that lost its Gemini signature still matches its call The marker with no safeguard note, from v1.77.3-stable through v1.82.0-stable, is now read only where a stored value is checked, so the shared marker matches what this release writes
…n Claude continues the turn A worker that remembered a call sent its whole arguments while a worker that did not sent the stored, cut ones. Claude answers 400 when the content before its signed thinking changed, so the turn after could not be continued on another worker. Claude and Bedrock Converse targets now always get what storage holds, as they did before, and every other target gets the whole arguments
…orage cut them The process remembers tool calls by id alone, so a call from another conversation with the same id could fill in a stored empty argument object. A kept call now takes the whole arguments only when the function names match and the stored text has the start, the end and the skipped count the writer would have cut from them A resent call that carries more than the bare call no longer keeps the other repeats of its turn in the request, and a continued request resolves the target's provider only when some arguments would change
…re and drop calls resent together A resent call that carries text, reasoning or a cache mark becomes the nearest assistant message, so dropping the bare repeats after it left their outputs with no call, and the request got an invented call to the first declared tool. A repeat is now dropped only when the nearest kept assistant message still makes its call A client that sends a turn's calls again in one block got every call twice, which Gemini, Claude with several calls and strict OpenAI-compatible providers refuse with a 400. A block whose calls are all repeats is now dropped, and a kept call takes its whole arguments back by what storage cut, whatever its function is named
…e the stored turn made once A resent call counted as a repeat when its id only started like the stored one, so a call resent under another signature was dropped, its output matched nothing, and the request got an invented call to the first declared tool. A resent call is now the stored one only when the ids are equal, or when one of them lost its signature A stored turn that makes one id twice is left as stored, since the repeat could be either call. Two ids that are one without their signatures count as one id Two annotations in the bridge change no behavior and keep the lint and type counts where they were
…the resent one Calls resent after a system message are sent as before, the same as after a user or developer message, and only the last two had a row. Two lists are also built without a second for clause
…re the stored ones A call sent again under a stored id was dropped whatever it said, so an edited call never reached the model. It is now dropped only when its tool and its JSON arguments are the stored ones, when storage cut the copy, or when the stored call holds the redaction placeholder, which takes the resent arguments. A namespaced name is the same tool as its bare name, and a custom tool input that storage cut is recognized inside its JSON wrapper Each stored call is read once however many copies a turn sends, and stored text longer than any cut of the resent arguments is skipped before the search for a cut
…nt calls with a place or no name Main's lint rule flags a type variable declared Final, so the two in the replay module are bare here. The tests now cover a call resent with its place in the turn, one resent without a name, an id reused after an empty turn, and a copy whose signature is missing between calls signed otherwise. The timing test is gone because unit tests on main must not depend on execution speed
[Medium impact] The behavior changes reach Responses API follow-ups that replay spend-log history. Fix the literal redaction-marker check before merging, because it can delete valid signed thinking during replay. Findings
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…ep signed turns' stored arguments Signed fields on Responses API spend rows are now kept up to 1 MiB instead of whole, and chat and messages rows keep the 2,048 cut. Compaction blocks are stored as before. A replayed turn made with signed thinking keeps the call arguments storage holds, and the Rust row writer reports the same skipped count as Python for a negative limit
…signed turns match on every pod Claude checks a signed thinking block against the whole conversation before it. A call replayed with the whole arguments one process remembers gave later turns a history another pod rebuilds from the cut copy in storage, so a third turn on another pod answered 400. Routes that build Anthropic Messages or Bedrock Converse requests keep the stored arguments again, decided by the provider config instead of the model name, and a turn made with signed thinking keeps them on any route
…lays and make its limit configurable
…v var names The env-key docs check only sees literal os.getenv names, so passing the name into the helper hid MAX_STRING_LENGTH_PROMPT_IN_DB and MAX_STRING_LENGTH_PROVIDER_SIGNED_IN_DB from it
|
bugbot run |
|
This PR is too large for Bugbot to review. It changes 1,361 lines and 4,180,891 characters. Split the change into smaller pull requests to get a review. |
|
bugbot run |
1 similar comment
|
bugbot run |
Pass the provider-signed limit to the Rust row builder again after BerriAI#53599 spelled out its arguments, and record BerriAI#53405's two new golden rows with that config key
|
bugbot run |
1 similar comment
|
bugbot run |
|
bugbot run |
1 similar comment
|
bugbot run |
|
bugbot run |
|
|
||
|
|
||
| def _is_altered_text(value: object) -> bool: | ||
| return isinstance(value, str) and (was_truncated_for_db(value) or REDACTED_BY_LITELM_STRING in value) |
There was a problem hiding this comment.
_is_altered_text treats a literal REDACTED_BY_LITELM in signed thinking as proof that storage changed it. A model discussing LiteLLM logs can return that text with a valid signature, even with token redaction off. Storage keeps the block intact, but the next previous_response_id replay deletes it. This loses valid reasoning and can break Claude tool-result turns.
Record whether storage actually changed the signed field instead of relying on this substring alone.
Knowledge Base Used: Restore signed thinking blocks and binding errors
There was a problem hiding this comment.
This is rare, since it needs the internal marker inside signed reasoning. The merge did not touch this file, so we will weigh it separately
There was a problem hiding this comment.
Agreed that this is rare and should be weighed separately from the merge. The merge not changing this file leaves the behavior unchanged; the concern is still a real edge case: at provider_signed_state.py:43, any signed field containing the marker is classified as altered, and replay can then drop an otherwise-valid thinking block even when token redaction did not insert it. I’d treat it as a low-priority correctness issue, not a blocker; recording actual storage alteration would avoid the false positive.
|
bugbot run |
1 similar comment
|
bugbot run |
Handwritten 1-3 sentence description
Following up on a previous response with Claude or Gemini thinking used to fail with a 400, because the gateway cut the long signature the provider needs when it saved the first response. Now it saves those signatures whole so the follow-up works
/walkthrough: What the PR adds
/walkthrough: What breaks
Details
TLDR
Problem this solves:
previous_response_idfollow-ups on Claude or Gemini thinking answer 400How it solves it:
User Flow
Before: a developer who chains Claude or Gemini thinking turns through the Responses API gets a 400 on the second turn
"reasoning": {"effort": "high"}to a Claude or Gemini modelreasoningitem, the answer, and anidlikeresp_...previous_response_idset to thatidand a new question or tool outputsInvalid `signature` in `thinking` blockfrom Claude orInvalid value at 'contents[1].parts[1].thought_signature'from GeminiAfter: the same follow-up answers 200 and builds on the first turn
Decisions
What is saved whole. On the rows of
/v1/responsescalls that LiteLLM serves through chat completions, signed Claude thinking blocks (text included, since Claude checks the signature against it),redacted_thinking.data,reasoning.encrypted_content, Geminithought_signature,thought_signaturesandresponse_thought_signature, and tool call ids that carry__thought__are cut at 1 MiB instead of 2,048 characters. The newMAX_STRING_LENGTH_PROVIDER_SIGNED_IN_DBenv var sets that limit, and a largerMAX_STRING_LENGTH_PROMPT_IN_DBwins. A smaller one does not lower it, so an operator who lowered the prompt limit to keep rows small sets the new variable too. Letting any set prompt limit bound signed fields was ruled out, because an operator who raised it would then have every signature past their limit cut again, and the follow-up would lose the earlier turn's reasoning. Letting only a lowered one bound them would make limits of 2,047 and 2,048 behave differently. Rows grow by the size of those fields. Token redaction still applies. I ruled out only dropping cut reasoning at replay (the model loses context and Claude wants the signed block on tool turns), raisingMAX_STRING_LENGTH_PROMPT_IN_DB(grows every field of every row), no cap at all (one row could hold any amount of thinking), and a separate store (a schema change for a few fields)Severe caveat, for the owner to sign off. An operator who lowered
MAX_STRING_LENGTH_PROMPT_IN_DBto keep spend-log rows small gets bigger rows for/v1/responsescalls served through chat completions after upgrading. SettingMAX_STRING_LENGTH_PROVIDER_SIGNED_IN_DBto the same value keeps today's rows, and those follow-ups answer 400 again. At a 500-character limit a stored Claude thinking response grew from 4,939 to 8,102 characters and its follow-up went from 400 to 200. GCS Pub/Sub withgcs_pub_sub_use_v1ships the same row, not verified because the QA project has no Pub/Sub access.SPEND_LOGS_URLexports nothing on either side, since its client is never set. The options are to ship as is, to let a setMAX_STRING_LENGTH_PROMPT_IN_DBbound signed fields unlessMAX_STRING_LENGTH_PROVIDER_SIGNED_IN_DBis also set, or to not ship. I recommend shipping as is, for the reason aboveOnly rows LiteLLM replays. Chat completions and
/v1/messagesrows keep the 2,048 cut, because onlyprevious_response_idreads rows back and those routes never answer with a response id to continue from. Native Responses API routes such as OpenAI and Azure keep it too, because the provider stores those conversations and LiteLLM never replays their rows. A row counts as served through chat completions when its logged response is chat-shaped, the same test thelatest_turnspend-log mode already uses to keep a replayed turn's whole request. A session that mixes routes replays a chat or messages turn without its signed reasoning, the same as today. Keeping it whole on every route would grow rows that nothing reads back and that exporters such as GCS Pub/Sub ship as they areWhat stays bounded. The saved request. Replay rebuilds the assistant turn from the saved response, so reasoning a client resends in its own input and then continues by id is left out of the replay, and no longer answers 400. With message logging off, rows keep today's storage, and compaction blocks are stored as before
Rows from before this change. A signed field that was cut, or masked by token redaction, is left out of the replay with one warning, and a cut signed tool call id is trimmed to the part before
__thought__. A value counts as cut only where the cut marker sits exactly where the writer cuts. Rows from v1.77.3-stable to v1.82.0-stable carry an older marker, which is recognised tooTool outputs. Gemini puts its signature inside the tool call id, so an output is matched to its call by the exact id first, then by the part before
__thought__. The same match finds the call in an earlier turn when a compaction item starts the replayRepeated calls. Before, replay could make a stored call a second time, once from storage and once from the process memory or the client. A message that holds only calls the nearest assistant message made once, and that are still unanswered, is now left out when every call in it is a repeat, unless a user, developer or system message comes in between. A call the client sends back is a repeat when it has the same tool and the same arguments, read as JSON, or arguments storage no longer holds. A copy that storage itself cut is a repeat by its id and tool. A call the worker remembers is a repeat by its id alone, because that memory is keyed by the call id and may hold another conversation's call. A call sent back with other arguments or another tool is sent as the client sent it, as before. Ids name the same call when they are equal or when one has lost its
__thought__part, a tool name may have lost its namespace before__, and calls of one turn that share the part before__thought__are sent as before. A message that says anything more (text, reasoning,cache_control, a new call) stays as sent, and what follows it in that turn is what it was before this change. When storage cut a call's arguments, the kept call gets them back from a repeat, the client's or the remembered one, but only if the stored text is exactly what the writer would have cut from them. When storage holds{}because message logging is off, only a call the client sends back fills it. On a route that builds Anthropic Messages or Bedrock Converse requests, every call keeps the arguments storage holds, because Claude checks a signed turn against the whole conversation before it, and whole arguments that only one worker remembers would make the next turn on another worker answer 400. On other routes, a turn whose stored thinking carries a signature (Databricks Claude, say) keeps them too, because the provider checks that turn against what it signed. Gemini signs the call instead of its thinking and accepts arguments that differ from the signed call, so a Gemini turn takes whole arguments from a repeat, as before this change. The route is read from its provider config, not the model name, so every Bedrock Converse model keeps them, Nova and Llama included. A Converse deployment often names Claude by an inference profile ARN or an alias that no name check can read, and a missed Claude answers 400. A non-Claude Converse model then sees the stored cut copy, as any model does when neither the worker nor the client has the whole call. Storing arguments whole was ruled out, since it has no size bound. Left as before: a repeat of a cut call that the client spelled another way, arguments that token redaction masked, and a custom tool call whose input does not survive litellm's own wrappingKnown gaps. With a first prompt over 2,048 characters, 1 of 5 routes (Vertex Claude) still answers 400, before and after, because the stored prompt is cut and Vertex binds the thinking to it. A Claude route, or a turn made with signed thinking, sees call arguments over 2,048 characters cut. With message logging off, a Claude tool output on the third turn answers 400, before and after, and so does a Claude turn after a compaction item, which before failed one turn earlier. Gemini answers 400, before and after, when arguments were cut inside a JSON escape and the worker does not remember the call. A Claude turn whose stored arguments were cut inside a JSON escape answers 400 too, as 1 of 8 Vertex Claude runs did. Web search
encrypted_content, long visible answers and very small limits are not touched hereNo PR to build on. None of the open PRs on this symptom fixes the spend-log cut. #44137 checks base64 on Gemini replay, #43435 keeps Gemini text signatures, #41590 shortens long item ids, #38337 maps ids across providers and #38333 rejects a mangled signature
This is an LLM translation fix, so a paired PR takes it to
lts/1.108.xRelevant issues
Fixes #42201
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Last updated: d5cf8d2. QA ran on e4562b9. Changes since QA: the limit getters name their env vars literally, and a test pins its clock. Proof remains valid because the getters read the same variables and defaults. Verification: spend tracking and responses unit tests, env-key docs check
Both legs ran the same curl scripts on real providers (
claude-sonnet-5-5on Anthropic, Vertex and Bedrock,gemini-3.8-flashon Google AI Studio and Vertex) with 2 uvicorn workers, one Postgres and no Redis. Every log is in this gist.More setups
Storage limits
Each row is one turn's spend log, read back through
/spend/logs, then aprevious_response_idfollow-up. A cut signature is left out of the replay, so that follow-up still answers 200MAX_STRING_LENGTH_PROMPT_IN_DB=1000, ClaudeMerged with main, and input tokens
Merged into main at 3e27fc2, the first six cases on all four Claude and Gemini routes answered 200 on 74 of 75 turns (the
main-merged-*logs). The other follows up a turn Claude refused, which fails on main tooClaude's follow-up carried 626 input tokens, as the same turns sent straight to Anthropic did (134 without thinking), and its tool output turn carried 1392, again the same as direct
Shared setup with the env vars, config.yaml, proxy start and matrix.zsh
The env vars are
ANTHROPIC_API_KEY,GEMINI_API_KEY,VERTEXAI_PROJECT,GOOGLE_APPLICATION_CREDENTIALS,AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN,LITELLM_MASTER_KEY,LITELLM_SALT_KEYandDATABASE_URL, names onlyconfig.yaml
Proxy start, from each tree, port 24311 for Before and 26417 for After
Cases run with
zsh matrix.zsh http://127.0.0.1:<port> <out dir> <model> "text stream tool parallel bigargs resend resent mixed long". The other scripts are in the gistBefore (768ea1b)
Case 1: Claude reasoning, then a
previous_response_idfollow-upreasoningitemprevious_response_idand answers 400 on the signatureCommands and output
Case 2: every route and turn shape
Results per route
Each cell lists a conversation's answers in order, with input tokens
After (e4562b9)
Case 1: Claude reasoning, then a
previous_response_idfollow-upRan again at
a70ae31022with 2,368 and 2,752-character signaturesreasoningitemprevious_response_idand answers 200 with the first turn's reasoning in its input tokensCommands and output
Case 2: every route and turn shape
Results per route
Each cell lists a conversation's answers in order, with input tokens
Also seen, left alone by this PR
Type
🐛 Bug Fix
Caveats (if any)
Severe
MAX_STRING_LENGTH_PROMPT_IN_DBget bigger Responses rows served through chat completions, since signed fields stay whole up to 1 MiBMAX_STRING_LENGTH_PROVIDER_SIGNED_IN_DBto the same value keeps today's rows and today's 400sgcs_pub_sub_use_v1ships these rows too, unverified without Pub/Sub accessMedium
Low
target_refuses_blank_textFinal Attestation