CLIProxyAPI Session Affinity: What the Logs Prove and What They Don't
Session affinity is built into stock CLIProxyAPI v8.0.58 and is off by default. Checked against the source and a day of masked logs from a Claude Code, Codex and T3 Code setup: the binding key, why one credential can't prove stickiness, what fill-first does, and why affinity enables cache reuse but can't prove it.

routing.session-affinity: true and the proxy ties each conversation to one credential under the key provider::sessionID::model. If you only have one credential per provider, the proxy has nothing else to pick, so the logs show affinity running but can't show it keeping a session on one account. A request with no binding goes to routing.strategy, once per session and model, not once per request. fill-first never spreads load. And none of this proves a prompt-cache hit. Only cache_read_input_tokens in the response does.There is a popular setup guide for running Claude Code and Codex through a local CLIProxyAPI, with T3 Code as the front end. Three of its claims are worth checking before you copy it. It says stock CLIProxyAPI rotates credentials on every request. It says session affinity is a custom-fork feature that "preserves Anthropic's 5-minute prompt cache". And it treats a log full of affinity lines as proof that affinity works. On v8.0.58 the first claim is wrong, the second goes too far, and the third proves less than it seems to.
CLIProxyAPI is an open-source local proxy that wraps Claude Code, Codex and other providers as one OpenAI-, Claude- and Codex-compatible API. T3 Code is an open-source desktop, web and mobile app that drives the agent CLIs already installed on your machine. Session affinity is the proxy setting that keeps one conversation on one account.
This article separates what the source code and a day of real logs actually show from what they only hint at. Everything below was captured on 2026-11-07 on a single-user local install with one Claude account, one Codex account and no remote clients. Hostnames, account identifiers, keys and session IDs are removed or replaced with placeholders.
Which routing settings does CLIProxyAPI v8.0.58 actually use?

This install came from the Homebrew formula. The CLIProxyAPI quick start covers other ways to install it. The config file is ~/.cli-proxy-api/config.yaml, using schema config-version: 8. These are the fields that matter here:
routing:
strategy: "fill-first"
session-affinity: true
session-affinity-ttl: "1h"
session-affinity-subagents: true
session-affinity-rebind-on: "quota"
retry:
request-retry: 3
max-retry-interval: 30
The upstream config.example.yaml at tag v8.0.58 documents every one of these. Its defaults differ from the block above in two places. session-affinity defaults to false, and session-affinity-rebind-on defaults to "any". So when a guide says stock CLIProxyAPI "distributes traffic naively", it is describing the switch left off. The code can do better.
That file lists exactly three strategies: round-robin (the default), weighted-round-robin and fill-first. This version has no expiration-aware strategy. If a guide or video shows one, it is using a fork.
What does session affinity bind a conversation to?
The selector is SessionAffinitySelector.Pick in sdk/cliproxy/auth/selector.go, and the core of it is short. It pulls out a session ID, builds cacheKey := provider + "::" + primaryID + "::" + modelKey, and looks that key up with GetAndRefresh. If the bound credential is still available, the selector returns it and renews the TTL. If it isn't, the fallback selector (your routing.strategy) picks one, and the proxy stores that pick as the new binding.
Two things follow from that key. The model is part of it, so a conversation that switches models gets a second, separate binding. And the TTL is an idle timeout. Every hit resets it, so a conversation that keeps going never expires, while one that sits quiet for an hour does. Bindings live in memory, so restarting the proxy drops all of them.
The session ID comes from ExtractSessionID, which checks a fixed list in order. These are the entries that matter for T3 Code:
| Priority | Signal | Sent by |
|---|---|---|
| 1 | X-Claude-Code-Session-Id header | Claude Code |
| 2 | Session inside metadata.user_id | Claude Code |
| 3 | Session-Id / Session_id header | Codex and compatible clients |
| 8 | prompt_cache_key, with conversation.id as an alias | Responses API clients |
| 11–12 | Derived context identity, then a hash of the first messages | Nobody. The proxy computes it. |
The order matters in practice. The source calls an explicit ID "absolute authority", and the proxy only falls back to matching on the longest common prefix of the messages when there is no header, body field or execution-session identity at all. A client that sends no ID still gets affinity, but through content matching, which is the weaker signal.
What do the session-affinity log lines show?
With default log settings the proxy writes one line for each selection decision. With the IDs masked, they look like this:
[info] selector.go:1312 session-affinity: cache miss, new binding | session=claude:<id> auth=<cred> provider=mixed model=claude-sonnet-5-5
[info] selector.go:1226 session-affinity: cache hit | session=claude:<id> auth=<cred> provider=mixed model=claude-sonnet-5-5
[info] selector.go:1453 session-affinity: LCP cache miss, new binding | session=lcp:<hash> auth=<cred> provider=mixed model=gpt-6-sol
The capture window ran from 19:51 to 23:52 IST on 2026-11-07. In that window the log held 228 cache hit lines, 5 cache miss, new binding lines (all with an explicit claude: or codex: prefix) and 1 LCP cache miss, new binding line. The prefix is what tells you something. The proxy only writes a claude: or codex: prefix when it took the explicit-identity path, so every one of those prefixed lines means a real client session ID arrived, not a content hash. The single lcp: line came from earlier in the day. It was a test call made by hand to gpt-6-sol without a session header.

This evidence has limits. The log truncates the session ID, so you can't tell whether two bindings belong to two T3 threads or to one thread that switched models. And provider=mixed only says the candidate pool covered more than one provider type. It doesn't say which credentials were eligible for a given model.
Mask the session ID and the credential before you share a line like these. The same rule applies to any agent log, and logging MCP tool calls without secrets shows how to test that no token reaches a log sink.
Why can one credential never prove affinity works?
With exactly one Claude credential and one Codex credential, every request has exactly one eligible credential. A cache hit returns it. A cache miss falls back to fill-first, which returns available[0], the same credential. You get the same result whether affinity works perfectly or is completely broken. So the 228 hits show that the lookup runs and the TTL refreshes. They don't show that sessions stick.
session-affinity: cache hit lines doesn't prove a session is sticky, because any selector would return that one credential. It doesn't prove a provider cache hit either. Add a second credential and check that auth= stays the same within each session, then read cache_read_input_tokens in the response.Adding a second credential for the same provider changes that. You can then see three behaviours, and each leaves its own trace in the log:
- A new session's first request logs
cache miss, new binding, and the strategy decides where it goes. Underfill-firstit lands on the first credential every time, in ID order, until that credential is unavailable. Underround-robinit rotates, per model, on each cold pick. - Later requests in that session log
cache hitwith the sameauth=value, even if the strategy would now pick something else. - When the bound credential hits a quota or auth error, the session rebinds. With
rebind-on: "quota", transient failures (408, 5xx, an empty stream) retry that one request on another credential and leave the binding alone, while 429 and 401/402/403 rebind it. With the default"any", any failure blamed on the credential rebinds it.
One more detail interacts with the example config. An existing binding beats credential priority. If a session is bound to a lower-priority credential and a higher-priority one comes back, the session stays put. Priority only decides cold bindings, requests without a session, and rebinding after a failure.
When does CLIProxyAPI pick a new credential?
"Distribution" means three different things depending on what the request carries, and the guide uses one word for all of them.
| Situation | Who chooses the credential | How often |
|---|---|---|
| Affinity on, session ID present, binding exists | The binding | Not at all until TTL, restart or failover |
| Affinity on, session ID present, no binding | routing.strategy | Once per session and model |
| Affinity on, no ID extractable | routing.strategy | Every request |
| Affinity off | routing.strategy | Every request |
Subagent, session-affinity-subagents: true | The parent's credential | Inherited, same model |
Subagent, session-affinity-subagents: false | The strategy, over the whole pool | Every subagent session |
Here is what that means for load. With affinity on and several credentials, fill-first works as a priority queue, not a load balancer. It piles new sessions onto the first credential and only moves on when that one is blocked. round-robin spreads new sessions out, but a long session still stays on one credential for as long as it runs. If you want several accounts used evenly on every request, neither setting does that. Only turning affinity off does, and then you lose the cache reuse that affinity is there to protect.
Does session affinity preserve the prompt cache?
Anthropic's prompt-caching documentation says the cache lasts 5 minutes by default, and each use of the cached content refreshes it for free. A 1-hour option costs extra. The same page says cache entries are isolated between organizations and, on the Claude API, between workspaces. Two separate accounts are two organizations, so a request that jumps to a second account can't read the first account's cache. That is the reason to keep a session on one credential. That follows from the documentation, though. These logs didn't measure it.
Two settings are easy to mix up here. The proxy's 1-hour session-affinity-ttl is how long a binding between a session and a credential survives while idle. It has nothing to do with how long the provider keeps a cached prefix, which is 5 minutes unless the client asked for the longer option. Also, a binding log line only tells you which credential the proxy used. Whether the provider then served from cache shows up in the response usage fields. A cache_read_input_tokens value above zero is a hit. If both it and cache_creation_input_tokens are zero, nothing was cached.
With default settings, this install records neither field, because request logging is off and no usage data is stored. The accurate thing for a guide to say is that affinity keeps a session on one credential, and that this is a precondition for cache reuse. To measure reuse, turn request logging on for a while, run two turns in a row in one session, and read cache_read_input_tokens from the second response. That test was not run for this article.
To see what losing the cache costs, put your session's token counts into the Agent Cost-per-Run Estimator twice, once with caching and once without. It prices re-sent history and cache reads separately.
Does T3 Code send a session ID through the proxy?
T3 Code has no special provider type for proxies. Its README says it controls the agent CLIs set up on your computer, and for each provider instance it launches the real CLI with that instance's environment. For Codex, that means a separate CODEX_HOME whose config.toml defines a custom provider. Codex's documentation describes [model_providers.<id>] with base_url, env_key and wire_api, and says Codex keeps its state under CODEX_HOME:
model = "gpt-6-sol"
model_provider = "cliproxy"
[model_providers.cliproxy]
name = "CLIProxyAPI"
base_url = "http://127.0.0.1:8317/v1"
wire_api = "responses"
env_key = "CLIPROXY_API_KEY"
requires_openai_auth = false
For Claude Code, the instance sets ANTHROPIC_BASE_URL, which the docs describe as the way to send requests through a proxy or gateway, and ANTHROPIC_AUTH_TOKEN, which goes out as the Authorization bearer value. It also sets its own CLAUDE_CONFIG_DIR. Don't add /v1 to the base URL, because Claude Code appends /v1/messages itself. Put the proxy's client key in the instance's sensitive environment variables, never in a file you commit.

/v1, and T3 keeps the token as a stored secret that it never shows again.
Because the real CLIs send the requests, the headers and body fields that affinity reads are the CLIs' own. The log backs this up. claude: and codex: prefixes appear, and those only exist when a client sends an explicit identity. What the log can't confirm is how it behaves per thread. The truncated IDs hide whether each T3 thread gets its own session, and you'd need to capture headers to know which one carried the ID.
Corrections for the guide you started from
- Session affinity is in stock CLIProxyAPI v8.0.58 and is off by default. Only expiration-aware scheduling needs a fork.
- Replace the "enable WebSockets" line with the real v8 field,
upstream.codex.websocket-compression. - Change "preserves the 5-minute cache" to "keeps a session on one credential, a precondition for cache reuse".
- Say that
fill-firstdoes not spread load, and that bindings are per session and model. - Add that bindings live in memory and disappear on restart.
- "Switches seamlessly" is only true inside one provider's pool, and with
rebind-on: "quota"transient errors keep the binding on purpose. - The binary is
cliproxyapi. If a guide spells it with an extrap, the command will fail when you paste it.
Apply this now
Run cliproxyapi --help and note the version. Compare your routing block with the config.example.yaml for that exact tag. Grep your log for session-affinity:, count the hits, new bindings and lcp: lines, and look at the prefix on every session= value. Keep three things as evidence: the version string, the counts, and one line for each prefix type with the session ID and credential masked.
If you run several accounts, know that pooling subscription accounts through a local proxy may break a provider's terms. This article didn't check any provider's terms. Read them before you add a second credential, and count a suspended account as a real cost of this setup.
FAQ
Do bindings survive a proxy restart?
No. The binding cache lives in memory, so after a restart every session starts cold and the routing strategy places it again. If that first request lands on a different credential, it also misses the provider's cache.
Is fill-first a good choice with affinity on?
It is predictable. New sessions go to the first credential until it is blocked. That makes it a poor choice if you want accounts used evenly. Use round-robin to spread new sessions, or weighted-round-robin with a top-level weight field in each credential file when your accounts have different quotas.
What happens when I switch models mid-conversation?
The model is part of the binding key, so the new model gets its own binding, possibly on a different credential.
Why would a request show an lcp: session?
The request had no explicit session header or body field, so the proxy matched it on the start of the message content instead. That works, but it is the fallback. A test call made by hand is one way to get one, as happened here.
T3 shows a usage error but requests succeed. Is routing broken?
The setup guide reports a JSON-RPC -32600 message in T3's usage panel for a Codex instance with no ChatGPT login. It reads that as T3's own usage check failing, not routing. The check that matters is an HTTP 200 on the model request in the proxy log.
Primary references
- CLIProxyAPI v8.0.58,
config.example.yaml: theroutingblock, its defaults and the semantics ofsession-affinity-rebind-onandsession-affinity-subagents. sdk/cliproxy/auth/selector.goat v8.0.58:SessionAffinitySelector.Pick, the binding key,FillFirstSelector.Pickand theExtractSessionIDpriority list.- Anthropic prompt caching: the 5-minute default lifetime, refresh on use, the 1-hour option, usage fields and organization/workspace isolation.
- Claude Code environment variables:
ANTHROPIC_BASE_URL,ANTHROPIC_AUTH_TOKENandCLAUDE_CONFIG_DIR. - Codex advanced configuration: custom
model_providersentries andCODEX_HOME. - T3 Code and its GitHub repository: what T3 Code is, the providers it supports and that it drives the agent CLIs installed on your machine.
- CLIProxyAPI documentation and the Homebrew formula: installing the proxy.
- Claude Code overview and Codex documentation: the two agent CLIs routed through the proxy.