Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

feat(api): advertise context window on /v1/models so clients stop truncating (#282)

Tashfeen67d799e15d48
diff --git a/server/src/__tests__/routes/proxy-auto-model.test.ts b/server/src/__tests__/routes/proxy-auto-model.test.ts
index 321a7d49..6d69527d 100644
--- a/server/src/__tests__/routes/proxy-auto-model.test.ts
+++ b/server/src/__tests__/routes/proxy-auto-model.test.ts
@@ -135,6 +135,33 @@ describe('Virtual "auto" model', () => {
}
});
+ // #282: clients read a model's context window from /v1/models; advertise it
+ // under both `context_window` and the OpenRouter-convention `context_length`,
+ // and give "auto" the largest window among connected models so clients don't
+ // fall back to a conservative ~16k default and truncate long inputs.
+ it('advertises context_length and a non-null auto context window (#282)', async () => {
+ const { status, body } = await request(app, 'GET', '/v1/models', undefined, authHeaders());
+ expect(status).toBe(200);
+
+ const auto = body.data.find((m: any) => m.id === 'auto');
+ expect(auto.context_window).toBe(auto.context_length);
+ expect(typeof auto.context_window).toBe('number');
+ expect(auto.context_window).toBeGreaterThan(0);
+
+ // Every real model mirrors context_window into context_length.
+ const connected = body.data.filter((m: any) => m.id !== 'auto' && m.available);
+ expect(connected.length).toBeGreaterThan(0);
+ for (const m of connected) {
+ expect(m.context_length).toBe(m.context_window);
+ }
+
+ // Auto's advertised ceiling is the max window among connected models.
+ const maxConnected = Math.max(
+ ...connected.filter((m: any) => m.context_window != null).map((m: any) => m.context_window),
+ );
+ expect(auto.context_window).toBe(maxConnected);
+ });
+
it('treats model:"auto" as auto-route instead of a 400', async () => {
const origFetch = global.fetch;
diff --git a/server/src/routes/proxy.ts b/server/src/routes/proxy.ts
index ff5601d2..169c6fd4 100644
--- a/server/src/routes/proxy.ts
+++ b/server/src/routes/proxy.ts
@@ -153,6 +153,20 @@ proxyRouter.get('/models', (req: Request, res: Response) => {
const onlyAvailable = q === '1' || q === 'true' || q === 'yes';
const listed = onlyAvailable ? models.filter(m => m.available === 1) : models;
+ // "auto" routes to whichever model fits, so its honest ceiling is the largest
+ // context window among models that can serve a request right now. Advertising
+ // null here makes OpenAI-compatible clients (opencode, Continue) fall back to
+ // their own conservative default — commonly ~16k — and truncate long inputs
+ // before they ever reach us (#282). Computed over currently-available models
+ // (not the query-filtered `listed`) so the ceiling is honest even on the
+ // default unfiltered list; null only when nothing is connected.
+ const availableContextWindows = models
+ .filter(m => m.available === 1 && m.context_window != null)
+ .map(m => m.context_window as number);
+ const autoContextWindow = availableContextWindows.length > 0
+ ? Math.max(...availableContextWindows)
+ : null;
+
res.json({
object: 'list',
data: [
@@ -162,7 +176,11 @@ proxyRouter.get('/models', (req: Request, res: Response) => {
created: 0,
owned_by: 'freellmapi',
name: 'Auto (router picks the best available model)',
- context_window: null,
+ context_window: autoContextWindow,
+ // `context_length` is OpenRouter's field name and the one most
+ // OpenAI-compatible clients read; emit both so whichever a client
+ // looks for is populated. Additive — clients ignore unknown fields.
+ context_length: autoContextWindow,
available: true,
unavailable_reason: null,
},
@@ -173,6 +191,7 @@ proxyRouter.get('/models', (req: Request, res: Response) => {
owned_by: m.platform,
name: m.display_name,
context_window: m.context_window,
+ context_length: m.context_window,
// Non-standard but additive: OpenAI clients ignore unknown fields.
available: m.available === 1,
unavailable_reason: m.available === 1 ? null : (m.enabled === 1 ? 'no_key' : 'disabled'),