Skip to content

Commit f4ca87a

Browse files
fix(openrouter): use endpoint context limits
1 parent 56eb23d commit f4ca87a

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/agents/pi-embedded-runner/openrouter-model-capabilities.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,48 @@ describe("openrouter-model-capabilities", () => {
9292
});
9393
});
9494

95+
it("uses endpoint-specific OpenRouter context length when top_provider reports one", async () => {
96+
await withOpenRouterStateDir(async () => {
97+
vi.stubGlobal(
98+
"fetch",
99+
vi.fn(
100+
async () =>
101+
new Response(
102+
JSON.stringify({
103+
data: [
104+
{
105+
id: "nvidia/nemotron-3-super-120b-a12b:free",
106+
name: "Nemotron 3 Super 120B Free",
107+
architecture: { modality: "text->text" },
108+
context_length: 1_000_000,
109+
top_provider: {
110+
context_length: 262_144,
111+
max_completion_tokens: 262_144,
112+
},
113+
pricing: { prompt: "0", completion: "0" },
114+
},
115+
],
116+
}),
117+
{
118+
status: 200,
119+
headers: { "content-type": "application/json" },
120+
},
121+
),
122+
),
123+
);
124+
125+
const module = await importOpenRouterModelCapabilities("top-provider-context-length");
126+
await module.loadOpenRouterModelCapabilities("nvidia/nemotron-3-super-120b-a12b:free");
127+
128+
expect(
129+
module.getOpenRouterModelCapabilities("nvidia/nemotron-3-super-120b-a12b:free"),
130+
).toMatchObject({
131+
contextWindow: 262_144,
132+
maxTokens: 262_144,
133+
});
134+
});
135+
});
136+
95137
it("preserves explicit OpenRouter tool support metadata", async () => {
96138
await withOpenRouterStateDir(async () => {
97139
vi.stubGlobal(

src/agents/pi-embedded-runner/openrouter-model-capabilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ interface OpenRouterApiModel {
4949
max_completion_tokens?: number;
5050
max_output_tokens?: number;
5151
top_provider?: {
52+
context_length?: number;
5253
max_completion_tokens?: number;
5354
};
5455
pricing?: {
@@ -174,7 +175,7 @@ function parseModel(model: OpenRouterApiModel): OpenRouterModelCapabilities {
174175
input,
175176
reasoning: supportedParameters?.includes("reasoning") ?? false,
176177
...(supportedParameters ? { supportsTools: supportedParameters.includes("tools") } : {}),
177-
contextWindow: model.context_length || 128_000,
178+
contextWindow: model.top_provider?.context_length ?? model.context_length ?? 128_000,
178179
maxTokens:
179180
model.top_provider?.max_completion_tokens ??
180181
model.max_completion_tokens ??

0 commit comments

Comments
 (0)