Repository navigation
fix(spend-logs): keep Anthropic web search encrypted_content whole so previous_response_id follow-ups work - #53838
Conversation
… whole so previous_response_id follow-ups can replay them
…p_web_search_encrypted_content # Conflicts: # litellm/proxy/spend_tracking/spend_tracking_utils.py
… the Rust row builder The default spend log row builder now runs in Rust, so the stored response cut the signed encrypted_content of web_search_result blocks and a previous_response_id follow-up answered 400. The Rust sanitize_body now keeps it whole on the stored response, like the Python builder, and the stored request keeps its cap The regression runs through both builders, and its calls are recorded into the crate's goldens so cargo test replays them
PR overviewThe PR preserves Anthropic web-search encrypted_content in spend logs so follow-up requests using previous_response_id can work. One issue remains open: the preservation rule applies beyond Anthropic-owned web-search content in both the Rust and Python implementations. Arbitrary nested response data can therefore retain plaintext API tokens in spend logs despite token redaction being enabled, exposing those tokens to anyone with access to the logs. Open issues (1)
Fixed/addressed: 0 · PR risk: 5/10 Reviews
|
| && is_sensitive_key(name) | ||
| { | ||
| text(REDACTED) | ||
| } else if keep_compaction_blocks && is_web_search_result_ciphertext(body, name, value) { |
There was a problem hiding this comment.
Medium: Plaintext response data can receive the ciphertext exemption
response_json() enables keep_compaction_blocks for every provider and propagates it recursively, so an ordinary A2A data part containing {"type":"web_search_result","encrypted_content":"<plaintext API token>"} now stores that token verbatim even with redact_tokens_in_spend_logs=true; the Python implementation has the same behavior. Restrict the exemption to provider-owned Anthropic web-search locations in both builders, leaving arbitrary nested response dictionaries subject to normal token redaction and size limits.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Handwritten 1-3 sentence description
Follow-up requests that continue a Claude conversation after it searched the web always failed with a 400, because we cut the search results when saving the conversation. This saves those search results whole, so the follow-up gets an answer that builds on the earlier searches
Details
TLDR
Problem this solves:
previous_response_idfollow-ups after Claude web search answered 400encrypted_contentat 2048 charsHow it solves it:
web_search_resultencrypted_contentwholeWhy whole and not capped.
encrypted_contentis a signed blob Anthropic checks byte for byte, so any cut breaks the replay. A 64 KB cap like the compaction summary's would never trigger at about 2 KB per value, raisingMAX_STRING_LENGTH_PROMPT_IN_DBwould grow every stored field, and dropping cut results at replay would throw away the searches the follow-up needs. The blob also skips token masking, since masking a substring breaks it the same way, while credential-key redaction still runs first. The stored request is unchanged. The flag keeps itskeep_compaction_blocksname, since the recorded Rust goldens pass it by that name, and the goldens gain only the three records for the new case instead of a full re-record. #53753 keeps thinking and reasoning signatures whole the same way in the same two files, without web search results, so whichever PR merges second moves the other's rule into one tableUser Flow
Before: a developer who continues a Claude web search conversation through the proxy gets a 400 on every follow-up
web_search_previewtool, streaming or notweb_search_callitems and an answer, and keep itsresp_...idprevious_response_idset to that idmessages.1.content.0: Invalid encrypted_content in search_result blockAfter: the same follow-up answers 200 with an answer built on the earlier searches
web_search_previewtool, streaming or notweb_search_callitems and an answer, and keep itsresp_...idprevious_response_idset to that idPre-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
Shared setup. Both legs ran at the same time on one machine, each proxy with 2 workers, its own empty Postgres database, and the Rust spend-log row builder compiled from its own tree, against the real Anthropic API. Env var names are
ANTHROPIC_API_KEY,LITELLM_MASTER_KEY, andDATABASE_URLThe
sleep 30gives the proxy time to write the first turn's spend log, which the follow-up reads its history fromBefore (7f69ea7)
Non-streaming first turn, then follow-up
Streaming first turn, then follow-up (400)
After (f5eb44b)
Non-streaming first turn, then follow-up
Streaming first turn, then follow-up (200)
Type
🐛 Bug Fix
Final Attestation