headroom/docs/content/docs
Tejas Chopra c07da992dd
Per-request backend selection for routing extensions (#2809)
## The gap

Headroom picks its egress backend **once**, at startup:
`create_proxy_backend` returns a single `Backend` (or `None` for the
direct Anthropic path) and every request goes through it. That is the
right shape for *"run this whole proxy against Bedrock instead of
Anthropic"* and the wrong shape for *"this request is cheaper on a
different provider than the last one."*

`ModelRouter` already lets an extension change `body["model"]` per
request — but only within the protocol the request arrived in, because a
model id alone cannot move a request to another provider.

So an extension can currently **decide** something Headroom has no way
to **carry out**. This adds the missing half.

## The seam

An extension publishes a decision on the request state:

```python
request.state.headroom_route = SimpleNamespace(
    model="moonshot/kimi-k2",   # required
    provider="moonshot",        # optional; inferred from the model id if absent
    reason="cheaper at this prefix length",
)
```

Headroom resolves a `LiteLLMBackend` for that provider — which is where
translation already lives — and serves **that one request** from it.
Nothing in core names any particular extension; the field is duck-typed,
so an extension does not import Headroom to talk to Headroom.

## Absent means unchanged

This is the property the tests are built around, and the reason this
should be safe to merge.

With nothing published, every path is what it was before. Advice that is
**absent, malformed, names an unknown provider, names a native provider,
or fails to build** all resolve to `self.anthropic_backend` — including
when that is `None`, which is the direct-API path and must survive. A
routing preference can never take traffic down.

## Coverage

| path | |
|---|---|
| `/v1/messages` | non-streaming + streaming |
| `/v1/chat/completions` | non-streaming + streaming |
| Responses API | untouched — does not use the backend abstraction |

Streaming is the one that matters. The resolver rewrites
`body["model"]`, so had `_stream_response_bedrock` kept reading
`self.anthropic_backend`, every streamed routed request would have sent
a foreign model id to Anthropic. Both streaming helpers now take an
optional `backend`, defaulting to the configured one.

## Details worth review

- **Validate the provider name before building.** `LiteLLMBackend`
accepts *any* provider string — the registry falls through to a generic
pass-through config — so a typo silently builds a backend that only
fails later, at request time, with an error pointing nowhere near the
typo. `_known_provider()` checks against `litellm.provider_list` first.
- **Cache per provider, and cache the failures too**, or a broken
provider name costs a construction attempt on every request. (Bedrock
construction calls out to AWS to enumerate inference profiles — it is
not free.)
- **`backend_owns_translation` now asks the per-request backend.** It
decides whether Headroom or the backend owns the `max_tokens` /
`max_completion_tokens` spelling; asking `self.anthropic_backend` would
answer "Headroom does" for a request about to be served by a translating
backend that does.
- **`_route_resolver` lives in `route_advice.py`, not on a handler
mixin.** Two mixins need it, and reaching across sibling mixins only
works by accident of how `HeadroomProxy` composes them.

## Tests

`tests/test_route_advice.py` — 20 tests, most of them asserting the
absent-means-unchanged property from a different angle.

Local runs: 20/20 on the new file; **1102 passed, 1 failed** on `-k
"openai or chat_completions or ccr"`, and **414 passed, 0 failed** on
`-k "stream or bedrock or route_advice"`. The single failure is
`test_realignment_live_multi_turn::test_ccr_marker_round_trip_live`,
which fails identically on this branch's merge-base — verified by
checking out `59314cff~1` and re-running it.

Note for anyone reproducing: `pytest-asyncio` is a declared dev
dependency but was missing from my venv, which made every `async def
test_` in the repo fail. Worth checking before diagnosing a large
failure count.

## Docs

`docs/content/docs/pipeline-extensions.mdx` gains a section on the
contract, next to the existing `x-headroom-base-url` one.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-05 17:01:32 -07:00
..
agent-orchestration.mdx docs(orchestration): guide repeated agent wakes with CCR (#1871) 2026-07-08 08:37:24 -05:00
agno.mdx new docs UI + ts doc coverage 2026-04-12 13:15:58 +06:00
anthropic-sdk.mdx fix(compress): resolve the /v1/compress tokenizer per model, and document the real contract (#2743) 2026-08-03 12:20:33 -07:00
api-reference.mdx docs: sync README + benchmarks with code (drop retired IntelligentContext/RollingWindow) (#1545) 2026-06-28 22:36:41 -07:00
architecture.mdx docs: sync Vercel docs with current code and add in-depth proxy config (#2475) 2026-07-21 16:26:56 -07:00
autogen.mdx feat: add CrewAI and AutoGen tool compression integrations (#1384) 2026-07-15 19:58:54 +00:00
benchmarks.mdx docs: sync Vercel docs with current code and add in-depth proxy config (#2475) 2026-07-21 16:26:56 -07:00
cache-optimization.mdx docs(configuration): document cold-prefix hook flags + bound the TTL observation log (#2557) 2026-07-25 11:07:57 -07:00
ccr.mdx docs: qualify CCR auto-resolution support for Gemini (#2044) 2026-07-14 11:59:08 -04:00
ci-cd-flows.mdx docs(ci): add CI/CD flow diagrams (#1062) 2026-06-16 23:05:15 -07:00
claude-code-azure-foundry.mdx fix(cli): harden all CLI surfaces + fix docs accuracy (#1491) 2026-06-27 14:48:43 -07:00
claude-code-vertex.mdx docs: use headroom-ai package name in install commands (#1014) (#1257) 2026-06-22 19:25:00 -05:00
code-compression.mdx feat(code): add PHP support to CodeAwareCompressor (#2423) 2026-07-31 15:54:13 -07:00
codex-recovery.mdx fix(codex): preserve wrapped sessions and recover state (#2160) 2026-07-15 19:58:21 +00:00
configuration.mdx chore(docs): one documentation site, not two (#2784) 2026-08-04 16:42:49 -07:00
context-management.mdx docs: sync README + benchmarks with code (drop retired IntelligentContext/RollingWindow) (#1545) 2026-06-28 22:36:41 -07:00
crewai.mdx feat: add CrewAI and AutoGen tool compression integrations (#1384) 2026-07-15 19:58:54 +00:00
docker-install.mdx fix: remove rtk and lean-ctx CLI context tools (#2677) 2026-07-30 22:59:41 -07:00
errors.mdx new docs UI + ts doc coverage 2026-04-12 13:15:58 +06:00
failure-learning.mdx feat: add first-class OpenCode support (wrap, learn, mcp install) (#559) 2026-06-25 13:38:58 -05:00
filesystem-contract.mdx fix: remove rtk and lean-ctx CLI context tools (#2677) 2026-07-30 22:59:41 -07:00
grok-build.mdx fix: remove rtk and lean-ctx CLI context tools (#2677) 2026-07-30 22:59:41 -07:00
how-compression-works.mdx docs: sync Vercel docs with current code and add in-depth proxy config (#2475) 2026-07-21 16:26:56 -07:00
image-compression.mdx fix: align docs and prune dead compatibility surfaces 2026-05-09 14:07:59 -07:00
index.mdx fix(compress): resolve the /v1/compress tokenizer per model, and document the real contract (#2743) 2026-08-03 12:20:33 -07:00
installation.mdx docs: sync Vercel docs with current code and add in-depth proxy config (#2475) 2026-07-21 16:26:56 -07:00
langchain.mdx new docs UI + ts doc coverage 2026-04-12 13:15:58 +06:00
limitations.mdx docs: sync README + benchmarks with code (drop retired IntelligentContext/RollingWindow) (#1545) 2026-06-28 22:36:41 -07:00
litellm.mdx fix(compress): resolve the /v1/compress tokenizer per model, and document the real contract (#2743) 2026-08-03 12:20:33 -07:00
local-llm-prefill.mdx [codex] Document local LLM prefill benchmarking (#1396) 2026-07-09 21:47:59 -05:00
mcp.mdx feat(mcp): add streamable HTTP MCP transport (#1773) 2026-07-14 13:25:45 -04:00
memory.mdx docs: sync Vercel docs with current code and add in-depth proxy config (#2475) 2026-07-21 16:26:56 -07:00
meta.json feat(claude): support Claude Code in VS Code (#2752) 2026-08-03 20:14:13 -07:00
metrics.mdx docs(metrics): document OTLP metric export and Dynatrace ingest (#2785) 2026-08-04 18:46:39 -07:00
openai-sdk.mdx fix(compress): resolve the /v1/compress tokenizer per model, and document the real contract (#2743) 2026-08-03 12:20:33 -07:00
opencode.mdx fix: remove rtk and lean-ctx CLI context tools (#2677) 2026-07-30 22:59:41 -07:00
persistent-installs.mdx feat(deploy): Add turnkey deploy command (#1404) 2026-07-15 18:37:20 +00:00
pipeline-extensions.mdx Per-request backend selection for routing extensions (#2809) 2026-08-05 17:01:32 -07:00
proxy.mdx fix(proxy): enable tool search by default and repair poisoned transcripts (#2807) 2026-08-05 14:36:34 -07:00
quickstart.mdx feat(claude): support Claude Code in VS Code (#2752) 2026-08-03 20:14:13 -07:00
releases.mdx docs(ci): add CI/CD flow diagrams (#1062) 2026-06-16 23:05:15 -07:00
savings.mdx feat(savings): durable savings ledger + headroom savings command (#1127) 2026-06-22 18:47:57 -05:00
shared-context.mdx fix(cli): harden all CLI surfaces + fix docs accuracy (#1491) 2026-06-27 14:48:43 -07:00
simulation.mdx fix(cli): harden all CLI surfaces + fix docs accuracy (#1491) 2026-06-27 14:48:43 -07:00
smart-crusher.mdx docs: fix stale API references, retired class imports, and incorrect examples 2026-06-02 19:19:19 -04:00
strands.mdx docs: sync README + benchmarks with code (drop retired IntelligentContext/RollingWindow) (#1545) 2026-06-28 22:36:41 -07:00
text-and-logs.mdx docs: sync Vercel docs with current code and add in-depth proxy config (#2475) 2026-07-21 16:26:56 -07:00
troubleshooting.mdx docs: add troubleshooting entry for uv build cache errors (#2490) 2026-07-22 06:06:51 -07:00
vercel-ai-sdk.mdx new docs UI + ts doc coverage 2026-04-12 13:15:58 +06:00
vscode-claude-code.mdx feat(claude): support Claude Code in VS Code (#2752) 2026-08-03 20:14:13 -07:00
vscode-copilot.mdx feat(copilot): proxy VS Code models transparently (#2687) 2026-08-03 04:42:48 -07:00