Skip to content

fix(spend-logs): keep Anthropic web search encrypted_content whole so previous_response_id follow-ups work - #53838

Open
izzybn wants to merge 3 commits into
BerriAI:mainfrom
izzybn:litellm_responses_keep_web_search_encrypted_content
Open

izzybn wants to merge 3 commits into
BerriAI:mainfrom
izzybn:litellm_responses_keep_web_search_encrypted_content

Conversation

@izzybn

@izzybn izzybn commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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_id follow-ups after Claude web search answered 400
  • Spend logs cut each search result's encrypted_content at 2048 chars

How it solves it:

  • The stored response keeps web_search_result encrypted_content whole
  • Every other stored string keeps today's 2048-char cut
  • Same rule in the default Rust row builder and the legacy Python one

Why whole and not capped. encrypted_content is 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, raising MAX_STRING_LENGTH_PROMPT_IN_DB would 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 its keep_compaction_blocks name, 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 table

User Flow

Before: a developer who continues a Claude web search conversation through the proxy gets a 400 on every follow-up

  1. They send POST https://litellm-domain/v1/responses with a Claude model and the web_search_preview tool, streaming or not
  2. They get 200 with web_search_call items and an answer, and keep its resp_... id
  3. They send a follow-up POST https://litellm-domain/v1/responses with previous_response_id set to that id
  4. The follow-up answers 400 messages.1.content.0: Invalid encrypted_content in search_result block

After: the same follow-up answers 200 with an answer built on the earlier searches

  1. They send POST https://litellm-domain/v1/responses with a Claude model and the web_search_preview tool, streaming or not
  2. They get 200 with web_search_call items and an answer, and keep its resp_... id
  3. They send a follow-up POST https://litellm-domain/v1/responses with previous_response_id set to that id
  4. The follow-up answers 200 with a summary that uses the earlier search results

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays 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

Case Before (7f69ea7) After (f5eb44b)
Non-streaming first turn, then follow-up 400 200
Streaming first turn, then follow-up 400 200

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, and DATABASE_URL

model_list:
  - model_name: claude-sonnet-5-5
    litellm_params:
      model: anthropic/claude-sonnet-5-5
      api_key: os.environ/ANTHROPIC_API_KEY
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  store_prompts_in_spend_logs: true
.venv/bin/python litellm/proxy/proxy_cli.py --config config.yaml --host 127.0.0.1 --port <port> --num_workers 2 --timeout_worker_healthcheck 60 --telemetry False --use_prisma_db_push

The sleep 30 gives the proxy time to write the first turn's spend log, which the follow-up reads its history from

Before (7f69ea7)

Non-streaming first turn, then follow-up

$ curl -sS -o r3/base-stream-false/turn1.json -w 'HTTP %{http_code}\n' http://127.0.0.1:26700/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","tools":[{"type":"web_search_preview"}],"input":[{"role":"user","content":"Who won the 2025 Nobel Prize in Physics, which universities are they at, and who currently leads each of those universities? Search the web for each fact."}]}'
HTTP 200
$ jq -c '{id, status, output_types: [.output[].type]}' r3/base-stream-false/turn1.json
{"id":"resp_NkxjxW3tdlWJMbvo5PRfYj5snuPgo64i1Ui_dglJsTK4mTZy3ZDBvT65UzYyJbIza2x__qBT8rmy_XKxXXZQs4NDfwQNNVAzY5eFCxwA82pdZ1qAG3bdQmHHMjzbnT9wZcdFBy3zw0xGebSCJQ3BgM1N71ZwlXoaIV_m96sDCR8L-10G0Prx7aiP3QBhAYdQMedEtxAYSDmoim9FuvSiMMgN5qnN0IVsiQEyduHSCjmVdhMoYlNdo-Wb0gsrt8mNOJaehsUIa8ktZeF3ukiPce_fWBwcvjnR0nrPv1se1EmT5AKEHxB-wkj0qcPWGokOAFx14Ivo4WkMzGvgk-yhQ1z3HIagEQ4Fu6wIRbyLxeZ3Gp4NC9WgNrYKrb6-wCEpiE8advJq4v250OajBT-Zpx0ZGAdNaQp1LFLZD_zsvVPVaO8xlVm876qT5gczSR1TxPBYJFIL9XxgOg==","status":"completed","output_types":["reasoning","message","web_search_call","web_search_call","web_search_call","web_search_call"]}
$ sleep 30
$ curl -sS -o r3/base-stream-false/turn2.json -w 'HTTP %{http_code}\n' http://127.0.0.1:26700/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","tools":[{"type":"web_search_preview"}],"previous_response_id":"resp_NkxjxW3tdlWJMbvo5PRfYj5snuPgo64i1Ui_dglJsTK4mTZy3ZDBvT65UzYyJbIza2x__qBT8rmy_XKxXXZQs4NDfwQNNVAzY5eFCxwA82pdZ1qAG3bdQmHHMjzbnT9wZcdFBy3zw0xGebSCJQ3BgM1N71ZwlXoaIV_m96sDCR8L-10G0Prx7aiP3QBhAYdQMedEtxAYSDmoim9FuvSiMMgN5qnN0IVsiQEyduHSCjmVdhMoYlNdo-Wb0gsrt8mNOJaehsUIa8ktZeF3ukiPce_fWBwcvjnR0nrPv1se1EmT5AKEHxB-wkj0qcPWGokOAFx14Ivo4WkMzGvgk-yhQ1z3HIagEQ4Fu6wIRbyLxeZ3Gp4NC9WgNrYKrb6-wCEpiE8advJq4v250OajBT-Zpx0ZGAdNaQp1LFLZD_zsvVPVaO8xlVm876qT5gczSR1TxPBYJFIL9XxgOg==","input":[{"role":"user","content":"Thanks. Now summarize the chain in one sentence."}]}'
HTTP 400
$ jq -c 'if .error then {error: .error.message[0:220]} else {status, output_types: [.output[].type], text: ([.output[] | select(.type=="message") | .content[]?.text] | join(" ") | .[0:300])} end' r3/base-stream-false/turn2.json
{"error":"litellm.BadRequestError: AnthropicException - {\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"messages.1.content.0: Invalid `encrypted_content` in `search_result` block\"},\"request_id\":\"req_011ChKTohtKo"}
Streaming first turn, then follow-up (400)
$ curl -sSN -o r3/base-stream-true/turn1.sse -w 'HTTP %{http_code}\n' http://127.0.0.1:26700/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","stream":true,"tools":[{"type":"web_search_preview"}],"input":[{"role":"user","content":"Who won the 2025 Nobel Prize in Physics, which universities are they at, and who currently leads each of those universities? Search the web for each fact."}]}'
HTTP 200
$ sed -n 's/^data: //p' r3/base-stream-true/turn1.sse | jq -c 'select(.type=="response.completed") | .response | {id, status, output_types: [.output[].type]}'
{"id":"resp_fft49u0FLdDoKirHVLNJ-MJtxD_y5RIijmTsXK0jrtNKYiJIFP98Sjvkxx5Tj_3A3krk184B0VQiy987sZ5-PxSzyDc4UTqtrXwzuHSljBBu5oB2jzzcihgqmKn0Wa4C3PmwGQBHsuPrrmmeoDLZxT8Nb04ya46JaonRb7bvn8B94XZbeFi0tqywBo60IlP6odeWbEV9aYaRQ8W9beOhiSPLepxGGzGBAr5SLLLCiRh_XZah5SKR4RPWTflju_mi0YVAIAHVcFxSvuReTwM1QC53vqOoD1xAy4TPHVRzIO6U5FY0K73JfhgvvtFKFegAaXx3E6FF9jyNbK1PE6OWi1WH63MF12fba_-zWg4Oo9XeQsJWcaB1_Dtb3nw1GkewOLssYQ1VZdYBQ9443YhMxtw2khMC2mzTsimz_3MKiRi8QkS08jEimMVY2TVg3d4vPaxCQjH6M3TioQ==","status":"completed","output_types":["web_search_call","web_search_call","web_search_call","web_search_call","message","reasoning"]}
$ sleep 30
$ curl -sS -o r3/base-stream-true/turn2.json -w 'HTTP %{http_code}\n' http://127.0.0.1:26700/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","tools":[{"type":"web_search_preview"}],"previous_response_id":"resp_fft49u0FLdDoKirHVLNJ-MJtxD_y5RIijmTsXK0jrtNKYiJIFP98Sjvkxx5Tj_3A3krk184B0VQiy987sZ5-PxSzyDc4UTqtrXwzuHSljBBu5oB2jzzcihgqmKn0Wa4C3PmwGQBHsuPrrmmeoDLZxT8Nb04ya46JaonRb7bvn8B94XZbeFi0tqywBo60IlP6odeWbEV9aYaRQ8W9beOhiSPLepxGGzGBAr5SLLLCiRh_XZah5SKR4RPWTflju_mi0YVAIAHVcFxSvuReTwM1QC53vqOoD1xAy4TPHVRzIO6U5FY0K73JfhgvvtFKFegAaXx3E6FF9jyNbK1PE6OWi1WH63MF12fba_-zWg4Oo9XeQsJWcaB1_Dtb3nw1GkewOLssYQ1VZdYBQ9443YhMxtw2khMC2mzTsimz_3MKiRi8QkS08jEimMVY2TVg3d4vPaxCQjH6M3TioQ==","input":[{"role":"user","content":"Thanks. Now summarize the chain in one sentence."}]}'
HTTP 400
$ jq -c 'if .error then {error: .error.message[0:220]} else {status, output_types: [.output[].type], text: ([.output[] | select(.type=="message") | .content[]?.text] | join(" ") | .[0:300])} end' r3/base-stream-true/turn2.json
{"error":"litellm.BadRequestError: AnthropicException - {\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"messages.1.content.0: Invalid `encrypted_content` in `search_result` block\"},\"request_id\":\"req_011ChKTrk1ev"}

After (f5eb44b)

Non-streaming first turn, then follow-up

$ curl -sS -o r3/head-stream-false/turn1.json -w 'HTTP %{http_code}\n' http://127.0.0.1:32143/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","tools":[{"type":"web_search_preview"}],"input":[{"role":"user","content":"Who won the 2025 Nobel Prize in Physics, which universities are they at, and who currently leads each of those universities? Search the web for each fact."}]}'
HTTP 200
$ jq -c '{id, status, output_types: [.output[].type]}' r3/head-stream-false/turn1.json
{"id":"resp_Hdkg-nzXFrRoWYUPq3UcLm0RmnwW0OiZycg28s2v_Vy84JOtZwaTLAH6hc1Y004fiYQEDO5XuXSUfmf3nbo8RRVqJ7J29pMNZx6om0xPGyjXHTMObv1Afu39Y4GsmYSXA1GWdCuwuYumIzAqjP04GLunkyTZIOuwfjB4FFnT-rZ20cdMpyt15Nsavojgc7dTBmtBJ0aOOuoPgT4AGYX6aMOYeVxqAgo9BdCvkMIO9Vosc-YskK4TKFIwtYJlqHkD-lbeQ0YFEio2mXBznVS5Ptit3LKcNNCYQJ1MwI-5RAsqFpq9UQZtXgMwomJ_hZ__V7CZu9nmzgtCtF64jnxUmVsCHgORdSnE8GGqIwKyN_OtD6Xn4rw6qeBJIw1924EKOQKFfSfFLqu0P0fRVshsOVoBYHQVZGmeImoWf7UyV96k3rIpbYoyRbq1ZcvVjtfA6cL2NUaq81-TTQ==","status":"completed","output_types":["reasoning","message","web_search_call","web_search_call","web_search_call","web_search_call"]}
$ sleep 30
$ curl -sS -o r3/head-stream-false/turn2.json -w 'HTTP %{http_code}\n' http://127.0.0.1:32143/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","tools":[{"type":"web_search_preview"}],"previous_response_id":"resp_Hdkg-nzXFrRoWYUPq3UcLm0RmnwW0OiZycg28s2v_Vy84JOtZwaTLAH6hc1Y004fiYQEDO5XuXSUfmf3nbo8RRVqJ7J29pMNZx6om0xPGyjXHTMObv1Afu39Y4GsmYSXA1GWdCuwuYumIzAqjP04GLunkyTZIOuwfjB4FFnT-rZ20cdMpyt15Nsavojgc7dTBmtBJ0aOOuoPgT4AGYX6aMOYeVxqAgo9BdCvkMIO9Vosc-YskK4TKFIwtYJlqHkD-lbeQ0YFEio2mXBznVS5Ptit3LKcNNCYQJ1MwI-5RAsqFpq9UQZtXgMwomJ_hZ__V7CZu9nmzgtCtF64jnxUmVsCHgORdSnE8GGqIwKyN_OtD6Xn4rw6qeBJIw1924EKOQKFfSfFLqu0P0fRVshsOVoBYHQVZGmeImoWf7UyV96k3rIpbYoyRbq1ZcvVjtfA6cL2NUaq81-TTQ==","input":[{"role":"user","content":"Thanks. Now summarize the chain in one sentence."}]}'
HTTP 200
$ jq -c 'if .error then {error: .error.message[0:220]} else {status, output_types: [.output[].type], text: ([.output[] | select(.type=="message") | .content[]?.text] | join(" ") | .[0:300])} end' r3/head-stream-false/turn2.json
{"status":"completed","output_types":["message"],"text":"The 2025 Nobel Prize in Physics went to John Clarke (UC Berkeley, led by Chancellor Rich Lyons), Michel Devoret (Yale, led by President Maurie McInnis, and UC Santa Barbara) and John Martinis (UC Santa Barbara, led by Chancellor Dennis Assanis)."}
Streaming first turn, then follow-up (200)
$ curl -sSN -o r3/head-stream-true/turn1.sse -w 'HTTP %{http_code}\n' http://127.0.0.1:32143/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","stream":true,"tools":[{"type":"web_search_preview"}],"input":[{"role":"user","content":"Who won the 2025 Nobel Prize in Physics, which universities are they at, and who currently leads each of those universities? Search the web for each fact."}]}'
HTTP 200
$ sed -n 's/^data: //p' r3/head-stream-true/turn1.sse | jq -c 'select(.type=="response.completed") | .response | {id, status, output_types: [.output[].type]}'
{"id":"resp_9Fe9WjH2tCrbv0kkngwOGRz8PJ3DryX0yBKu3hrofA3BfrpnQbog35ve6A3Uv9o4_zZ05ntal0WllURFf2_d4UxQno2Uk2zimM9UfomxxJIrRbwULoBCGsdCqwnNWHBvYg9AYnP5VeuORFtVNfNJqaDQK6vswU6NApzEKMK_t8x-Zenb1dSTX32DrjzgOrtYZ291rriH1fGMe2ldxUOH9xpfwuPMPldwsH07ovQxMDB2fPMUckS7zAMtff8Y_G9skEjosUK21M8gzVfQg_zZRLlikUfK6gwDFdTv5HDBI_jJBKXnTJm42FIEVh5KpT7JAN0-lXWHJCdZTRb8FIMNZqA4VuE94TblrHcyce6u4BttzwOnjwQvqaAgP7-kuFNdkUOr6I9V1l64dhheUUtzmaPIOufbtIzRd4dXojNwmZtKAr9DdXwVMMzYJfVm97HEz1T5485vG-augg==","status":"completed","output_types":["web_search_call","web_search_call","web_search_call","web_search_call","message","reasoning"]}
$ sleep 30
$ curl -sS -o r3/head-stream-true/turn2.json -w 'HTTP %{http_code}\n' http://127.0.0.1:32143/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model":"claude-sonnet-5-5","tools":[{"type":"web_search_preview"}],"previous_response_id":"resp_9Fe9WjH2tCrbv0kkngwOGRz8PJ3DryX0yBKu3hrofA3BfrpnQbog35ve6A3Uv9o4_zZ05ntal0WllURFf2_d4UxQno2Uk2zimM9UfomxxJIrRbwULoBCGsdCqwnNWHBvYg9AYnP5VeuORFtVNfNJqaDQK6vswU6NApzEKMK_t8x-Zenb1dSTX32DrjzgOrtYZ291rriH1fGMe2ldxUOH9xpfwuPMPldwsH07ovQxMDB2fPMUckS7zAMtff8Y_G9skEjosUK21M8gzVfQg_zZRLlikUfK6gwDFdTv5HDBI_jJBKXnTJm42FIEVh5KpT7JAN0-lXWHJCdZTRb8FIMNZqA4VuE94TblrHcyce6u4BttzwOnjwQvqaAgP7-kuFNdkUOr6I9V1l64dhheUUtzmaPIOufbtIzRd4dXojNwmZtKAr9DdXwVMMzYJfVm97HEz1T5485vG-augg==","input":[{"role":"user","content":"Thanks. Now summarize the chain in one sentence."}]}'
HTTP 200
$ jq -c 'if .error then {error: .error.message[0:220]} else {status, output_types: [.output[].type], text: ([.output[] | select(.type=="message") | .content[]?.text] | join(" ") | .[0:300])} end' r3/head-stream-true/turn2.json
{"status":"completed","output_types":["reasoning","message"],"text":"John Clarke, Michel Devoret and John Martinis won the 2025 Nobel Prize in Physics, with affiliations at UC Berkeley (Clarke), Yale and UC Santa Barbara (Devoret), and UC Santa Barbara (Martinis). Those schools are now led by Chancellor Rich Lyons at Berkeley, Chancellor Dennis Assanis at UC Santa Ba"}

Type

🐛 Bug Fix

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

… 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
@veria-ai

veria-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

The 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) {

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.

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

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

2 participants