Skip to main content

Optimize inside your LiteLLM proxy

A plug-in inside your LiteLLM proxy sends each request to Anyray for optimization. LiteLLM still calls the provider with its own keys.

Use it when nothing may sit in front of LiteLLM. If something may, put the gateway in front of LiteLLM instead, which can also read trimmed content back.

PropertyValue
Runs inThe LiteLLM proxy (1.111), as a callback
CredentialAn Anyray service key (ark_svc_…)
OptimizesAnthropic Messages and OpenAI Chat Completions requests
NetworkOutbound from the proxy to your Anyray gateway
If Anyray is slow or downLiteLLM sends the original request

Set it up​

1
Mint a service key

Console Users page > Agents card > Add agent, pick LiteLLM. Store the ark_svc_… key in the proxy's secret store. Step 2 of the dialog shows the blocks below with your gateway filled in.

2
Set two variables on the proxy

Set them in the proxy's environment, however the proxy is deployed. From a shell that then starts LiteLLM:

export ANYRAY_GATEWAY_URL='https://<your-anyray-gateway>'
export ANYRAY_API_KEY='<the ark_svc_ key>'
3
Download the plug-in

Run this in the directory that holds your config.yaml. LiteLLM imports the file as the module anyray_optimizer, so it must sit beside config.yaml or on PYTHONPATH.

curl -fsSL -o anyray_optimizer.py --config - <<EOF
url = "$ANYRAY_GATEWAY_URL/connect/litellm-plugin"
header = "Authorization: Bearer $ANYRAY_API_KEY"
EOF

The key goes to curl on stdin, so it never shows in the process list.

It needs nothing beyond litellm[proxy].

4
Add the callback
config.yaml
litellm_settings:
callbacks: anyray_optimizer.proxy_handler_instance

If config.yaml already has litellm_settings, add the callbacks line under it. A second litellm_settings: key replaces the first. If it already lists callbacks, keep them and add this one: callbacks: ["<your existing callback>", "anyray_optimizer.proxy_handler_instance"].

5
Restart the proxy

Set LITELLM_MASTER_KEY first: LiteLLM 1.111 refuses to start without it. Then restart LiteLLM.

6
Send one request and check the card

Send one request through LiteLLM. The agent's row on the Agents card reads Active once both its optimize calls and its usage reports arrive.

Card showsBeside itFix
WaitingThe agent has not sent a request yetThe proxy never called Anyray. Check both variables are set in the process that runs LiteLLM, and the callback line.
Half wiredusage reports not arrivingOptimize calls arrive, usage reports do not. A response LiteLLM serves from its own cache sends no report, so a repeated test request reads this: send a new one. Otherwise allow the proxy outbound access to the gateway's /v1/hook/usage, and look for a warning from anyray_optimizer in the proxy log.
Activeretrieval not available on this lane yetDone. Read-back is off; see below.
LiteLLM is optimized

Each Messages or Chat Completions request now goes through Anyray before the provider.

Let Claude Code seats read trims back​

By default Anyray applies only optimizations that need no read-back (anyray_retrieve). Turned on, it can also replace content with a handle that the person's own Claude Code reads back. That changes what the model sees, which is why it starts off.

LiteLLM user ids must be sign-in emails

With this on, LiteLLM's user id decides whose retrieval a trim belongs to. Turn it on only if your LiteLLM user ids are your people's Anyray sign-in emails.

  1. On the console Optimizer page, turn on LiteLLM seat retrieval (byoSeatRetrieval).
  2. Enroll each person with Anyray Connect. Connect leaves Claude Code pointed at LiteLLM and adds only the retrieval server.

The agent's row then reads Active with retrieval connected for N of M seats. Users that match no enrolled seat are listed on the key's Info tab (matching rules).

LiteLLM logs a warning from anyray_optimizer

The plug-in logs only an error class, such as ConnectTimeout or HTTPStatusError, and the request goes out unoptimized. A 403 with hook_requires_service_key means ANYRAY_API_KEY holds a personal key: mint a service key instead.

Already running the optimizer-only attach mode

That older install keeps working. It is described in the plug-in reference.

Details: LiteLLM plug-in reference.