Add a 5k token buffer before the end of the context window #1289
+42
−18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
Follow-up on #1275 to give more of a buffer, just because it's currently so painful to recover from the case where you go beyond the context window and tiktoken is not an exact estimate.
Implementation
This truncates when you're within 5,000 tokens of the end of the window.
How to Test
Get a conversation near the end and confirm you never make it to within 5,000 tokens of the end of the window.
Important
Introduces a 5,000 token buffer in
index.ts
to prevent exceeding the context window, with updated tests insliding-window.test.ts
.TOKEN_BUFFER
constant inindex.ts
to introduce a 5,000 token buffer before the context window limit.truncateConversationIfNeeded()
inindex.ts
to truncate messages if withinTOKEN_BUFFER
of the token limit.sliding-window.test.ts
to reflect new buffer logic, ensuring truncation occurs when within 5,000 tokens of the limit.sliding-window.test.ts
to verify truncation when tokens are withinTOKEN_BUFFER
of the threshold.This description was created by
for 764d963. It will automatically update as commits are pushed.