feat(api): opt-in per-bank attribution for remote reranker requests#2781
Open
chethanuk wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Opt-in per-bank cost attribution for remote rerank requests. When
HINDSIGHT_API_RERANKER_SEND_BANK_AS_HEADER=true, outbound rerankPOSTs carryX-Hindsight-Bank-Id: <bank_id>. Default isfalse— a byte-identical no-op.Covers: the Cohere-compatible HTTP client (Cohere via base URL, OpenRouter, ZeroEntropy, SiliconFlow, Alibaba) and the LiteLLM proxy reranker (
LiteLLMCrossEncoder, same per-request pattern).Fixes #2729
Root cause
_CohereCompatibleRerankClient.predictposted with no per-request headers. The process-wide client only carriesAuthorization/Content-Typeand is shared across banks, so a client-default header could pin bank A's id onto bank B's requests. The existingapply_bank_attribution()body mutator can't help — the Cohere rerank wire format has no OpenAI-styleuserfield.sequenceDiagram participant R as recall / reflect participant CV as _current_bank_id (contextvar) participant C as shared rerank client<br/>(one per process, all banks) participant P as remote provider R->>CV: bind bank_id R->>C: predict(query, docs) C->>C: bank_attribution_headers()<br/>flag on + bank bound → {X-Hindsight-Bank-Id} C->>P: POST /rerank (per-request headers,<br/>never client defaults) Note over C,P: bank A and bank B requests on the same client<br/>each carry their own id — no bleedDesign notes
X-Bank-Idspelling for MCP surfaces is left to maintainers — trivial rename if preferred.reflectcurrently run with no bank bound; upstream fix(reflect): preserve bank attribution in provider calls #2764 fixes that binding orthogonally, and this feature picks it up automatically once merged.How this differs from #2740
Strictly additive and scoped to the issue's stated minimum plus the safe LiteLLM proxy path: no existing attribution/LiteLLM tests are deleted, the LiteLLM legacy response fallback is untouched, and the unrelated reflect-binding change stays in #2764. The LiteLLM SDK path is deliberately left out: passing
headers=intolitellm.arerankunconditionally is unverified against litellm's rerank signature (it may break SDK reranks even with the flag off). Tests here exercise the real env flag + contextvar end-to-end throughhttpx.MockTransport— including absent-by-default, absent-when-no-bank, auth-header survival, and two-bank per-request isolation — rather than patching the header helper.Out of scope (follow-ups)
Native Cohere SDK, LiteLLM SDK (pending kwarg verification), Google, TEI (self-hosted; attribution value low), local/FlashRank/JinaMLX/RRF (no remote spend to attribute).
How to test
Test evidence
Run on the de-stacked, squashed branch (single commit on upstream
main):hindsight-embed/tests/test_env_template.pyruff check/ruff format --checkon touched filesty checkonbank_attribution.py+cross_encoder.pygenerate-docs-skill.shidempotency