Skip to content

feat: add X-Session-Id header for proxy cache routing affinity#31511

Open
songchaow wants to merge 1 commit into
anomalyco:devfrom
songchaow:feat/x-session-id-header
Open

feat: add X-Session-Id header for proxy cache routing affinity#31511
songchaow wants to merge 1 commit into
anomalyco:devfrom
songchaow:feat/x-session-id-header

Conversation

@songchaow

Copy link
Copy Markdown

Issue for this PR

Closes N/A (improvement — no existing issue)

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds an X-Session-Id header (set to the session ID) alongside the existing x-session-affinity header for all non-opencode providers.

Why: Anthropic API proxies (e.g. enterprise gateways that front multiple upstream Anthropic accounts) use X-Session-Id to implement routing affinity — pinning all requests from the same session to the same upstream account. This maximizes KV-Cache hit rate on the upstream side because the cached prompt prefix lives on that specific account.

Without this header, multi-turn conversations get scattered across different upstream accounts by the load balancer, causing frequent prompt cache misses and significantly higher latency + cost.

The header name X-Session-Id is the convention used by Anthropic-compatible proxy providers (documented in their API specs). It is sent unconditionally for non-opencode providers — providers that do not recognize it will simply ignore an unknown header, so there is no behavioral change for direct Anthropic API usage or other providers.

The change is a single line addition in packages/opencode/src/session/llm/request.ts.

How did you verify your code works?

  1. Built locally with bun run build --skip-embed-web-ui --single
  2. Set up an HTTP header inspection proxy between opencode and the API endpoint — confirmed X-Session-Id is present in all outgoing requests
  3. Ran an end-to-end cache hit test: two sequential requests with the same session ID and same system prompt showed cache_creation_input_tokens: 1538 on request 1, then cache_read_input_tokens: 1538 on request 2 — confirming the routing affinity enables cache hits
  4. Full typecheck passed (23/23 packages)

Screenshots / recordings

N/A — backend-only change, no UI impact.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@songchaow songchaow force-pushed the feat/x-session-id-header branch from 35e9346 to 9b67f1c Compare June 9, 2026 12:18
Providers like LLM++ (TimiAI) use X-Session-Id to route requests from
the same session to the same upstream account, maximizing KV-Cache hit
rate. Without this header, multi-turn conversations get scattered across
different upstream accounts, causing frequent cache misses.

The header is sent alongside the existing x-session-affinity header for
all non-opencode providers.
@songchaow songchaow force-pushed the feat/x-session-id-header branch from 9b67f1c to e1e97f4 Compare June 9, 2026 13:24
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