Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(sdk): preserve proxy authentication alongside provider credentials (#3450)

trs-80ca4b47a3fc63
diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.ts
index c3f5dc528..9b837ef3b 100644
--- a/sdk/typescript/src/client.ts
+++ b/sdk/typescript/src/client.ts
@@ -510,6 +510,7 @@ export class HeadroomClient implements HeadroomClientInterface {
...options.headers,
};
if (this.apiKey) {
+ headers["X-Headroom-Proxy-Token"] = this.apiKey;
// Don't override provider auth headers
if (!headers["Authorization"] && !headers["x-api-key"]) {
headers["Authorization"] = `Bearer ${this.apiKey}`;
diff --git a/sdk/typescript/test/client-expanded.test.ts b/sdk/typescript/test/client-expanded.test.ts
index 63e08e9e9..f982470a1 100644
--- a/sdk/typescript/test/client-expanded.test.ts
+++ b/sdk/typescript/test/client-expanded.test.ts
@@ -2,7 +2,7 @@
* Tests for expanded HeadroomClient — chat.completions, messages, metrics, CCR, etc.
* Uses mocked proxy (no real server needed).
*/
-import { describe, it, expect, vi, beforeEach } from "vitest";
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { HeadroomClient } from "../src/client.js";
const mockFetch = vi.fn();
@@ -17,6 +17,46 @@ function jsonResponse(data: any, ok = true) {
};
}
+describe("provider requests authenticate to the proxy separately", () => {
+ beforeEach(() => {
+ mockFetch.mockReset();
+ mockFetch.mockResolvedValue(jsonResponse({}));
+ for (const name of ["HEADROOM_API_KEY", "OPENAI_API_KEY", "ANTHROPIC_API_KEY"]) {
+ vi.stubEnv(name, "");
+ }
+ });
+ afterEach(() => vi.unstubAllEnvs());
+
+ it.each([
+ ["openai", "explicit"], ["anthropic", "explicit"],
+ ["openai", "environment"], ["anthropic", "environment"],
+ ["openai", "proxy-only"], ["anthropic", "proxy-only"],
+ ["openai", "provider-only"], ["anthropic", "provider-only"],
+ ])("sends the correct credentials for %s (%s)", async (provider, source) => {
+ const hasProxy = source !== "provider-only";
+ const hasProvider = source !== "proxy-only";
+ const fromEnv = source === "environment";
+ if (fromEnv) {
+ vi.stubEnv("HEADROOM_API_KEY", "proxy-token");
+ vi.stubEnv(provider === "openai" ? "OPENAI_API_KEY" : "ANTHROPIC_API_KEY", "provider-key");
+ }
+ const client = new HeadroomClient({
+ baseUrl: "http://proxy:8787",
+ apiKey: hasProxy && !fromEnv ? "proxy-token" : undefined,
+ providerApiKey: hasProvider && !fromEnv ? "provider-key" : undefined,
+ });
+ const params = { model: "test-model", messages: [{ role: "user" as const, content: "hello" }] };
+ await (provider === "openai" ? client.chat.completions.create(params) : client.messages.create(params));
+
+ const headers = new Headers(mockFetch.mock.calls[0][1].headers);
+ expect(headers.get("X-Headroom-Proxy-Token")).toBe(hasProxy ? "proxy-token" : null);
+ expect(headers.get("Authorization")).toBe(
+ hasProvider ? (provider === "openai" ? "Bearer provider-key" : null) : "Bearer proxy-token",
+ );
+ expect(headers.get("x-api-key")).toBe(hasProvider && provider === "anthropic" ? "provider-key" : null);
+ });
+});
+
describe("HeadroomClient constructor", () => {
it("accepts extended options", () => {
const client = new HeadroomClient({