Skip to content

🐛 Fix proxy caching between requests in aiohttp transport - #17122

Merged
ishaan-jaff merged 4 commits into
BerriAI:mainfrom
lcfyi:lcfyi/fix-caching-between-calls
Dec 2, 2025
Merged

ishaan-jaff merged 4 commits into
BerriAI:mainfrom
lcfyi:lcfyi/fix-caching-between-calls

Conversation

@lcfyi

@lcfyi lcfyi commented Nov 26, 2025 •

Copy link
Copy Markdown
Contributor

Title

Fix proxy caching between requests in aiohttp transport

Relevant issues

Couldn't find one at a glance, this may have just been an edge case we encountered and nobody else has.

Pre-Submission checklist

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

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Will let CI pass for (2) and (3). My change may have hit a linter whammy so there are a fair number of whitespace / formatting changes from ruff.

Type

🐛 Bug Fix
✅ Test

Changes

Background

This PR fixes a bug in the LiteLLMAiohttpTransport proxy cache when multiple endpoints are sent through the same session. A concrete example of where this can happen is in the /v1/messages Anthropic adapter, which fetches an anthropic client from the cache here:

async_httpx_client = get_async_httpx_client(
llm_provider=litellm.LlmProviders.ANTHROPIC
)

If we have multiple backends that have mixed proxy settings (e.g. amazonaws.com and azure.com, NO_PROXY=amazonaws.com HTTP_PROXY=foo.com), we'll get unwanted behaviour depending on when the requests land on the process:

  1. If an amazonaws.com request lands first, we'll cache the NO_PROXY, and subsequent requests will not pass in a proxy configuration (there's some extra weird behaviour here because we do pass in trust_env to aiohttp, meaning it'll will actually use the HTTP_PROXY variable if it's properly formed)
  2. If an azure.com request lands first, we'll cache the proxy configuration, and subsequent requests will use foo.com

The fix

This PR basically makes the proxy cache depend on the request URL rather than caching it once ~forever. I chose the full URL as the cache key since I don't think there are any request URLs that have constantly rotating query params that'd bust the cache, but correct me if I'm wrong.

I ended up caching with the host as key, see the last 2 commits on this PR. I wrote a test to reflect this behaviour.

@vercel

vercel Bot commented Nov 26, 2025

Copy link
Copy Markdown

@lcfyi is attempting to deploy a commit to the CLERKIEAI Team on Vercel.

A member of the Team first needs to authorize it.

@ishaan-jaff

Copy link
Copy Markdown
Contributor

@AlexsanderHamir please take a look when possible

@lcfyi
lcfyi force-pushed the lcfyi/fix-caching-between-calls branch from 6ab2b76 to e96ed00 Compare November 27, 2025 19:57
@lcfyi

lcfyi commented Nov 27, 2025

Copy link
Copy Markdown
Contributor Author

I rebased on main, looks like CI is running and not dying on lockfile issues anymore.

@AlexsanderHamir

Copy link
Copy Markdown
Contributor

I benchmarked the behavior before and after this PR, and found no performance regressions.

@ishaan-jaff
ishaan-jaff merged commit de4ff12 into BerriAI:main Dec 2, 2025
4 of 6 checks passed
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
)

* write a regression test

* impl fix

* add test for host case

* use the host as cache key
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.

3 participants