detached 1425e68

fix(provider): clamp max_tokens to fit OpenRouter provider context window

- it currently rejects requests where input + max_output exceeds the
serving provider's context lmit (which may be tighter than the
model-level value)
- use the smaller of ContextWindow and MaxOutput as the cap, with a
4096-token input reserve
commit
1425e68636b67c73aaa224c138e098318be4d6d6
author
Neil Vallecer · · 2026-06-09 15:25 UTC
parent
3d031dd
signature
unsigned
2 files · +27 -0
M packages/provider/discover.go +2 -0
@@ -286,6 +286,8 @@ func DiscoverOpenRouter(ctx context.Context, baseURL string) ([]Model, error) {
286286 ctxWin := d.ContextLength
287287 if ctxWin == 0 {
288288 ctxWin = d.TopProvider.ContextLength
289+ } else if d.TopProvider.ContextLength > 0 && d.TopProvider.ContextLength < ctxWin {
290+ ctxWin = d.TopProvider.ContextLength
289291 }
290292 maxOut := 0
291293 if d.TopProvider.MaxCompletionTokens != nil {
@@ -286,6 +286,8 @@ func DiscoverOpenRouter(ctx context.Context, baseURL string) ([]Model, error) {
286 ctxWin := d.ContextLength286 ctxWin := d.ContextLength
287 if ctxWin == 0 {287 if ctxWin == 0 {
288 ctxWin = d.TopProvider.ContextLength288 ctxWin = d.TopProvider.ContextLength
289+ } else if d.TopProvider.ContextLength > 0 && d.TopProvider.ContextLength < ctxWin {
290+ ctxWin = d.TopProvider.ContextLength
289 }291 }
290 maxOut := 0292 maxOut := 0
291 if d.TopProvider.MaxCompletionTokens != nil {293 if d.TopProvider.MaxCompletionTokens != nil {
M packages/provider/openai.go +25 -0
@@ -183,6 +183,31 @@ func (c *openaiClient) buildRequest(req Request) (*oaiRequest, error) {
183183 if maxTok <= 0 {
184184 maxTok = m.MaxOutput
185185 }
186+ // Clamp max_tokens so output + minimum input fits within the context
187+ // window. Some providers (OpenRouter) enforce input + max_output <=
188+ // context_length and reject requests where the total exceeds it.
189+ // This reservation ensures basic user input (system prompt + first
190+ // message + tool definitions) has room.
191+ //
192+ // Use the smaller of ContextWindow and MaxOutput as the cap because
193+ // some providers (OpenRouter) report inflated model-level context
194+ // windows (e.g. 1000000) while the serving provider enforces a much
195+ // tighter limit (e.g. 262144). When both are wrong we still land on
196+ // the output cap, which is the actual limit reported by the provider.
197+ if m.ContextWindow > 0 && m.MaxOutput > 0 {
198+ ctxCap := m.ContextWindow
199+ if m.MaxOutput < ctxCap {
200+ ctxCap = m.MaxOutput
201+ }
202+ const reserve = 4096
203+ clamped := ctxCap - reserve
204+ if clamped < 1024 {
205+ clamped = 1024
206+ }
207+ if maxTok > clamped {
208+ maxTok = clamped
209+ }
210+ }
186211 if m.Reasoning {
187212 if maxTok > 0 {
188213 out.MaxCompletionTok = &maxTok
@@ -183,6 +183,31 @@ func (c *openaiClient) buildRequest(req Request) (*oaiRequest, error) {
183 if maxTok <= 0 {183 if maxTok <= 0 {
184 maxTok = m.MaxOutput184 maxTok = m.MaxOutput
185 }185 }
186+ // Clamp max_tokens so output + minimum input fits within the context
187+ // window. Some providers (OpenRouter) enforce input + max_output <=
188+ // context_length and reject requests where the total exceeds it.
189+ // This reservation ensures basic user input (system prompt + first
190+ // message + tool definitions) has room.
191+ //
192+ // Use the smaller of ContextWindow and MaxOutput as the cap because
193+ // some providers (OpenRouter) report inflated model-level context
194+ // windows (e.g. 1000000) while the serving provider enforces a much
195+ // tighter limit (e.g. 262144). When both are wrong we still land on
196+ // the output cap, which is the actual limit reported by the provider.
197+ if m.ContextWindow > 0 && m.MaxOutput > 0 {
198+ ctxCap := m.ContextWindow
199+ if m.MaxOutput < ctxCap {
200+ ctxCap = m.MaxOutput
201+ }
202+ const reserve = 4096
203+ clamped := ctxCap - reserve
204+ if clamped < 1024 {
205+ clamped = 1024
206+ }
207+ if maxTok > clamped {
208+ maxTok = clamped
209+ }
210+ }
186 if m.Reasoning {211 if m.Reasoning {
187 if maxTok > 0 {212 if maxTok > 0 {
188 out.MaxCompletionTok = &maxTok213 out.MaxCompletionTok = &maxTok

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close