LiteLLM plug-in reference
Depth behind the LiteLLM plug-in guide.
Which requests it optimizes
| LiteLLM call | Sent to Anyray as |
|---|---|
/v1/messages (Anthropic Messages) | anthropic-messages |
/v1/chat/completions, sync or async | openai-chat |
Every other call type (embeddings, Responses, images, audio) goes to the provider unchanged. LiteLLM still routes on the model name your caller sent, whatever the optimized request says.
What it sends
Each turn makes two calls to your gateway, both authenticated with ANYRAY_API_KEY. The gateway
accepts a service key only; a personal key gets 403 with hook_requires_service_key.
| Call | When | Carries |
|---|---|---|
POST /v1/hook/optimize | Before LiteLLM calls the provider | The request (model, messages, system, tools and sampling fields), its wire, and an x-anyray-metadata header with tool: "litellm-plugin" plus the LiteLLM key's user_id and team_id when set. A Claude Code session id (x-claude-code-session-id) is passed along when the caller sent one. A model prefixed anthropic/, bedrock/ or openai/ names the provider the row is filed under. |
POST /v1/hook/usage | After the provider answers or the request finally fails | The hookToken from the optimize answer, the upstream status, token counts with the cache split, and latency, with an x-anyray-metadata header of tool: "litellm-plugin". No content. |
The usage report is sent in the background, so the response to your caller never waits for it. It has a 5 second budget. A turn whose report never arrives stays marked unreported; it never reads as zero tokens. A response LiteLLM serves from its own cache made no provider call and reports nothing, so a repeated test request can leave the card at Half wired; send a new one. A failed attempt that LiteLLM retries or falls back from reports nothing either, so the later success reports its usage.
The plug-in is also served by your gateway at GET /connect/litellm-plugin
(any enrolled client key in Authorization: Bearer), so the file you download matches your
gateway's version. The plug-in itself needs a service key.
Failing open
The optimize call has a 1.5 second budget. If the gateway errors or times out, LiteLLM sends the request anyway:
- When the request extends a conversation this worker optimized for the same LiteLLM key, the plug-in replays the last optimized prefix with the new turn appended, so the provider's prompt cache keeps matching.
- Otherwise it sends the original request.
Replay memory lives in each LiteLLM worker: up to 512 conversations, each for one hour. A request that lands on another worker, or after its conversation was evicted, gets the original request. Errors are logged as their class name only, never a message or a body.
What Anyray keeps
| Store | What it keeps |
|---|---|
| Spend | One metadata row per optimize call, under the agent's key. What the plug-in reports (status, tokens, cost, latency) is kept apart from the row's own numbers, with a SHA-256 of the hookToken, never the token (data boundary). |
| Traces | The request before and after optimization, under your org's content mode, like any other request (content mode). |
Retrieval
The plug-in has no read path of its own. With the byoSeatRetrieval setting off (the default),
the gateway never applies an optimization that would need content read back, and the Agents card
shows Active with retrieval not available on this lane yet, never Needs one fix. All
card states: status vocabulary.
With it on, a call made for a person whose Claude Code is enrolled with Connect can use read-back:
their own Claude Code reads the trim back from the gateway's /mcp. It is off by default because it changes what the model sees. Turn it on with
PUT /admin/v1/settings ({"byoSeatRetrieval": true}) or on the console Optimizer page.
With byoSeatRetrieval on, the user the plug-in sends (the LiteLLM key's user_id) decides
whose retrieval a trim belongs to, and the gateway trusts it as sent. Turn it on only if your
LiteLLM user ids are your people's Anyray sign-in emails. A user id that names the wrong person
lets that person read back the trimmed content.
How a seat is matched
A call may use read-back only when all five rules hold. The gateway checks them in this order on every call, so a revoked key stops matching at once.
| Rule | Reason when it fails |
|---|---|
byoSeatRetrieval is on for your org. | seat_retrieval_off |
The plug-in sent a valid user id (LiteLLM's user_id). | seat_no_user |
| That user id equals the user of a personal key in your org that is neither revoked nor expired. Letter case of A to Z is ignored. | seat_unmatched |
| One of those keys reached the gateway's retrieval server in the last 6 hours. | seat_unlatched |
The request declares anyray_retrieve. | seat_tool_undeclared |
A sixth reason, seat_lookup_failed, means the gateway could not read its key store in time.
When every rule holds, the spend row's retrieval evidence class is seat_latched and the trim is
stored under the matched key's user, spelled as that key stores it. When one fails, the call is
optimized as if the setting were off, and the row's retrieval.evidenceReason names the first
rule that failed. Either way, spend is attributed as it is with the setting off.
The person's machine needs Connect's retrieval-only lane for Claude Code: behind your own LLM gateway.
Seats on the Agents card
While the setting is on, GET /admin/v1/keys/:id adds hookLane.seats to the plug-in key, over
its last 24 hours of calls:
| Field | Meaning |
|---|---|
total | Distinct users the calls named. |
retrieving | Distinct users with at least one seat_latched call. |
unmatched | Up to 20 users whose newest call failed with seat_unmatched. |
The card reads Active with retrieval connected for N of M seats, including 0 of M. With no
calls in the last day it reads retrieval on, no seats in the last day. The
key's Info tab lists the unmatched users: their LiteLLM user id matches no enrolled seat.
With the setting off, seats is absent. When the gateway cannot read the seats, the key carries
seatsUnreadable: true instead and the card reads seat count unavailable right now.
Attach mode (v1, optimizer only)
New installs use the plug-in. Attach mode runs only the optimizer beside LiteLLM, with no gateway, so there are no service keys, no Agents card and no usage reports.
Prerequisites: Docker Engine (v24+) and Docker Compose v2, git, and openssl on the Anyray
host; Python 3.9+ and httpx in the LiteLLM environment; port 3000 (the Anyray console)
reachable from your org network or VPN and port 8088 (the optimizer) reachable only by
LiteLLM.
git clone https://github.com/anyrayHQ/install anyray && cd anyray
./setup.sh --attach --host <anyray-host>
--attach selects the gateway-less install, which needs no Billing token. setup.sh writes
.env, including the admin key, optimizer token, the trace store DB URL
(ANYRAY_OBSERVABILITY_DB_URL), and content-encryption key.
docker compose -f docker-compose.attach.yml up -d
By default the optimizer binds to 127.0.0.1:8088, which is correct when LiteLLM runs on the
same host. If LiteLLM runs on another machine, set ANYRAY_OPTIMIZER_BIND=0.0.0.0 in .env,
keep :8088 reachable only over a private network or VPN, then restart the stack.
From the install clone, copy attach/litellm/anyray_optimizer.py next to your LiteLLM
config.yaml, then add:
litellm_settings:
callbacks:
- anyray_optimizer.proxy_handler_instance
Set these environment variables in the LiteLLM process:
ANYRAY_OPTIMIZER_URL=http://<anyray-host>:8088
ANYRAY_OPTIMIZER_TOKEN=<ANYRAY_OPTIMIZER_TOKEN from .env>
docker compose -f docker-compose.attach.yml ps
curl -fs http://localhost:8088/health && echo "optimizer ok"
curl -fso /dev/null http://localhost:3000/anyray-login && echo "console ok"
Open http://<anyray-host>:3000 and sign in with the admin key setup.sh printed (also in
.env as ANYRAY_ADMIN_TOKEN).
In attach mode the optimizer records each call through /v1/record, and ANYRAY_CONTENT_MODE on
the optimizer governs what content it keeps
(/v1/record).