Skip to content

Commit 7386e3d

Browse files
committed
Add OpenRouter middle out transform to avoid context limit errors
1 parent ed99fb0 commit 7386e3d

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/api/providers/openrouter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,18 @@ export class OpenRouterHandler implements ApiHandler {
9595
maxTokens = 8_192
9696
break
9797
}
98+
99+
// Removes messages in the middle when close to context window limit. Should not be applied to models that support prompt caching since it would continuously break the cache.
100+
const shouldApplyMiddleOutTransform = !this.getModel().info.supportsPromptCache
101+
102+
// @ts-ignore-next-line
98103
const stream = await this.client.chat.completions.create({
99104
model: this.getModel().id,
100105
max_tokens: maxTokens,
101106
temperature: 0,
102107
messages: openAiMessages,
103108
stream: true,
109+
transforms: shouldApplyMiddleOutTransform ? ["middle-out"] : undefined,
104110
})
105111

106112
let genId: string | undefined

0 commit comments

Comments
 (0)