Skip to content

perf(platform): name a foreign-model agent exec for replica cache affinity - #3490

Merged
yannickmonney merged 1 commit into
mainfrom
perf/agent-cache-affinity
Sep 26, 2026
Merged

yannickmonney merged 1 commit into
mainfrom
perf/agent-cache-affinity

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Why

A self-hosted model served by several replicas keeps each conversation's prompt prefix in one replica's cache. A balancer that picks a replica by load alone (least connections) sends the next turn of the same agent exec to whichever replica is idle, and that replica prefills the whole conversation again.

Measured on a live two-replica deployment: one 42-turn agent exec changed replica 20 times and re-prefilled about 285K tokens, roughly 7 of its 49 minutes of model time.

What

A managed Claude Code turn on a model that is not Claude now names its exec in every request:

  • ANTHROPIC_CUSTOM_HEADERS carries x-bf-eh-x-tale-cache-affinity: <exec id>, appended to any header the harness already sends.
  • The sandbox LLM gateway (Bifrost 1.6.11) forwards x-bf-eh-* headers to the provider with the prefix stripped, so the upstream sees X-Tale-Cache-Affinity: <exec id>.
  • A balancer can hash that header to keep the exec on one replica, for example Caddy's lb_policy header X-Tale-Cache-Affinity { fallback least_conn }. An upstream that ignores it is unaffected.

Nothing changes for a Claude model, for an exec without an id, or for a bring-your-own credential.

Tests

  • lib/harnesses/exec-builder.test.ts: the header is set, an existing custom header is kept, and none is sent for a Claude model, an exec without an id, or a BYO credential.
  • golden-exec.test.ts: no golden changes, since no golden case pairs a foreign model with an exec id.
  • @tale/platform lint and typecheck pass, lint:manual passes, and a row was added to tests/manual/reference/automation.md.

Whether a multi-replica balancer actually keeps the turn on one replica is a deployment check. It is verified on the router that consumes the header, which hashes it with a least-connections fallback.

…inity

A self-hosted model served by several replicas keeps each conversation's
prompt prefix in one replica's cache. A balancer that picks by load alone
sends later turns of the same agent exec to the other replica, which
prefills the whole conversation again: one observed 42-turn exec changed
replica 20 times and spent about a seventh of its model time re-reading
its own context.

A managed Claude Code turn on a model that is not Claude now names its
exec in every request (ANTHROPIC_CUSTOM_HEADERS carries
x-bf-eh-x-tale-cache-affinity: <exec id>). The gateway forwards that
header upstream as X-Tale-Cache-Affinity, so a balancer can hash it to
keep the turn on one replica; an upstream that ignores it is unaffected.
A Claude model, an exec without an id and a bring-your-own credential
send nothing new.
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Verification of the client half: Claude Code 2.1.282 run against a local header-recording endpoint with ANTHROPIC_CUSTOM_HEADERS=$'x-existing: 1\nx-bf-eh-x-tale-cache-affinity: exec-probe-1' sent both headers on its /v1/messages request. The gateway half was checked in the Bifrost transports/v1.6.11 source: x-bf-eh-* headers are collected in lib/ctx.go and applied upstream by providerUtils.SetExtraHeaders in the OpenAI provider. I did not test the CLI version pinned in the sandbox image here. That, and the balancer keeping a turn on one replica, still need a live check after release.

@yannickmonney
yannickmonney merged commit 8da98ae into main Sep 26, 2026
58 checks passed
@yannickmonney
yannickmonney deleted the perf/agent-cache-affinity branch September 26, 2026 15:31
yannickmonney added a commit that referenced this pull request Sep 26, 2026
Brings in #3490 and #3491. chat-surface.tsx takes main's new
branch-selection import path and fork comment; chatDraftKey stays in
lib/draft-key.ts, unchanged.

Main's round added CHAT-F44, NAV-F25, PROJ-F25, SET-F46, TASK-F19 and
TASK-B6 while this branch's unmerged boxes held the same IDs. Main's
boxes keep theirs; this branch's move to CHAT-F46, NAV-F41, PROJ-F28,
SET-F48, TASK-F28 and TASK-B7, and their references follow. The
conflicted suites' counts are recounted.
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.

1 participant