-
Notifications
You must be signed in to change notification settings - Fork 62
feat: keep requests under the endpoint byte limit by stripping oldest images #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mlikasam-askui
wants to merge
2
commits into
main
Choose a base branch
from
feat/request-byte-budget-truncation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Request size estimation shared across truncation and providers. | ||
|
|
||
| The Anthropic Messages API rejects requests whose serialized body exceeds | ||
| ~32 MB with a 400 ``BadRequestError``. Base64-encoded screenshots dominate | ||
| that payload. These helpers estimate the serialized byte size cheaply so | ||
| truncation strategies (and a provider-side safety net) can keep requests | ||
| under the limit. | ||
|
|
||
| The estimate reads cached string lengths (``len`` is O(1) on Python | ||
| strings, and base64 ``data`` is ASCII so its length equals its serialized | ||
| byte count), making a full pass O(number of blocks) rather than | ||
| O(payload size). Structural JSON overhead (field names, braces, quotes) is | ||
| not counted; it is sub-percent of image-heavy payloads and absorbed by the | ||
| threshold headroom callers apply on top of the hard limit. | ||
| """ | ||
|
|
||
| from askui.models.shared.agent_message_param import ( | ||
| Base64ImageSourceParam, | ||
| BetaThinkingBlock, | ||
| ContentBlockParam, | ||
| ImageBlockParam, | ||
| MessageParam, | ||
| TextBlockParam, | ||
| ToolResultBlockParam, | ||
| ToolUseBlockParam, | ||
| ) | ||
|
|
||
| # Hard cap on serialized request size for the Anthropic Messages API. | ||
| ANTHROPIC_MAX_REQUEST_BYTES = 30 * 1024 * 1024 | ||
|
|
||
|
|
||
| def estimate_block_bytes(block: ContentBlockParam) -> int: | ||
| """Cheaply estimate the serialized byte size of one content block. | ||
|
|
||
| Base64 image ``data`` is ASCII, so ``len`` equals its byte count and | ||
| is O(1) on Python strings. Walking blocks is therefore O(number of | ||
| blocks) rather than O(payload size), keeping the byte check cheap even | ||
| with many multi-megabyte screenshots. | ||
| """ | ||
| if isinstance(block, ImageBlockParam): | ||
| if isinstance(block.source, Base64ImageSourceParam): | ||
| return len(block.source.data) | ||
| return len(block.source.url) | ||
| if isinstance(block, TextBlockParam): | ||
| return len(block.text) | ||
| if isinstance(block, ToolResultBlockParam): | ||
| if isinstance(block.content, str): | ||
| return len(block.content) | ||
| return sum(estimate_block_bytes(nested) for nested in block.content) | ||
| if isinstance(block, ToolUseBlockParam): | ||
| return len(str(block.input)) + len(block.name) | ||
| if isinstance(block, BetaThinkingBlock): | ||
| return len(block.thinking) + len(block.signature) | ||
| # BetaRedactedThinkingBlock | ||
| return len(block.data) | ||
|
|
||
|
|
||
| def estimate_message_bytes(message: MessageParam) -> int: | ||
| """Estimate the serialized byte size of a single message.""" | ||
| if isinstance(message.content, str): | ||
| return len(message.content) | ||
| return sum(estimate_block_bytes(block) for block in message.content) | ||
|
|
||
|
|
||
| def estimate_messages_bytes(messages: list[MessageParam]) -> int: | ||
| """Estimate the serialized byte size of a message history.""" | ||
| return sum(estimate_message_bytes(message) for message in messages) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like that we estimate the Request side. I would prefere to deal with the Error. And then do the Truncation strategy.
Otherwise we don't know if the LLM Provider is inceasing theire 30 MB Limit