Skip to content

Commit 630890a

Browse files
authored
feat(openrouter): default OpenRouter attribution headers (#10109)
1 parent e7576ee commit 630890a

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@langchain/openrouter": patch
3+
---
4+
5+
feat(openrouter): default OpenRouter attribution headers
6+
7+
`ChatOpenRouter` now sends `HTTP-Referer` and `X-Title` headers by default for OpenRouter app attribution. `siteUrl` defaults to `"https://docs.langchain.com/oss"` and `siteName` defaults to `"langchain"`. Users can still override both values.

libs/providers/langchain-openrouter/src/chat_models/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,19 @@ export class ChatOpenRouter extends BaseChatModel<
206206
/** OpenRouter plugins to enable (e.g. web search). */
207207
plugins?: ChatOpenRouterParams["plugins"];
208208

209-
/** Your site URL — used for rankings on openrouter.ai. */
210-
siteUrl?: string;
209+
/**
210+
* Application URL for OpenRouter attribution. Maps to `HTTP-Referer` header.
211+
*
212+
* See https://openrouter.ai/docs/app-attribution for details.
213+
*/
214+
siteUrl: string;
211215

212-
/** Your site/app name — used for rankings on openrouter.ai. */
213-
siteName?: string;
216+
/**
217+
* Application title for OpenRouter attribution. Maps to `X-Title` header.
218+
*
219+
* See https://openrouter.ai/docs/app-attribution for details.
220+
*/
221+
siteName: string;
214222

215223
/** Extra params passed through to the API body. */
216224
modelKwargs?: Record<string, unknown>;
@@ -254,8 +262,8 @@ export class ChatOpenRouter extends BaseChatModel<
254262
this.route = fields.route;
255263
this.provider = fields.provider;
256264
this.plugins = fields.plugins;
257-
this.siteUrl = fields.siteUrl;
258-
this.siteName = fields.siteName;
265+
this.siteUrl = fields.siteUrl ?? "https://docs.langchain.com/oss";
266+
this.siteName = fields.siteName ?? "langchain";
259267
this.modelKwargs = fields.modelKwargs;
260268
this.streamUsage = fields.streamUsage ?? true;
261269
}
@@ -274,8 +282,8 @@ export class ChatOpenRouter extends BaseChatModel<
274282
return {
275283
Authorization: `Bearer ${this.apiKey}`,
276284
"Content-Type": "application/json",
277-
...(this.siteUrl ? { "HTTP-Referer": this.siteUrl } : {}),
278-
...(this.siteName ? { "X-Title": this.siteName } : {}),
285+
"HTTP-Referer": this.siteUrl,
286+
"X-Title": this.siteName,
279287
};
280288
}
281289

libs/providers/langchain-openrouter/src/chat_models/tests/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ describe("ChatOpenRouter constructor", () => {
7777
expect(model.streamUsage).toBe(true);
7878
});
7979

80+
it("defaults siteUrl and siteName for OpenRouter attribution", () => {
81+
const model = new ChatOpenRouter({ model: "openai/gpt-4o" });
82+
expect(model.siteUrl).toBe("https://docs.langchain.com/oss");
83+
expect(model.siteName).toBe("langchain");
84+
});
85+
86+
it("allows user to override siteUrl and siteName", () => {
87+
const model = new ChatOpenRouter({
88+
model: "openai/gpt-4o",
89+
siteUrl: "https://my-custom-app.com",
90+
siteName: "My Custom App",
91+
});
92+
expect(model.siteUrl).toBe("https://my-custom-app.com");
93+
expect(model.siteName).toBe("My Custom App");
94+
});
95+
8096
it("throws OpenRouterAuthError when no API key is available", () => {
8197
const original = process.env.OPENROUTER_API_KEY;
8298
delete process.env.OPENROUTER_API_KEY;

libs/providers/langchain-openrouter/src/chat_models/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,19 @@ export interface ChatOpenRouterParams
7171
apiKey?: string;
7272
/** Base URL for the API. Defaults to "https://openrouter.ai/api/v1". */
7373
baseURL?: string;
74-
/** Your site URL — used for OpenRouter rankings / rate limits. */
74+
/**
75+
* Application URL for OpenRouter attribution. Maps to `HTTP-Referer` header.
76+
* Defaults to `"https://docs.langchain.com/oss"`.
77+
*
78+
* See https://openrouter.ai/docs/app-attribution for details.
79+
*/
7580
siteUrl?: string;
76-
/** Your site name — shown on the OpenRouter leaderboard. */
81+
/**
82+
* Application title for OpenRouter attribution. Maps to `X-Title` header.
83+
* Defaults to `"langchain"`.
84+
*
85+
* See https://openrouter.ai/docs/app-attribution for details.
86+
*/
7787
siteName?: string;
7888
/** Stable identifier for end-users, used for abuse detection. */
7989
user?: string;

0 commit comments

Comments
 (0)