fix(proxy): account for media tokens in context estimation#3250
Merged
Conversation
CarlitoDon
force-pushed
the
fix/media-token-estimation
branch
from
July 15, 2026 00:25
8f43b8a to
165f4c6
Compare
There was a problem hiding this comment.
Pull request overview
Updates the proxy’s context token estimation so media-heavy OpenAI/Gemini payloads are accounted for during sizing/compression decisions, reducing the chance of oversized Gemini requests slipping through due to uncounted media blocks.
Changes:
- Adds token estimation helpers for OpenAI
image_urlandaudio_urlcontent blocks (including base64 data URLs). - Adds token estimation for Gemini
inlineData(base64-encoded media payloads) during request sizing.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+125
to
+128
| } else { | ||
| // Unknown media: treat as text approximation | ||
| estimate_tokens_from_str(&format!("[binary data: {} bytes]", data_len)) | ||
| } |
Comment on lines
+946
to
+950
| if let Some(inline_data) = part.get("inlineData") { | ||
| let mime = inline_data.get("mimeType").and_then(|m| m.as_str()).unwrap_or(""); | ||
| let data_len = inline_data.get("data").and_then(|d| d.as_str()).map(|s| s.len()).unwrap_or(0); | ||
| total += estimate_inline_data_tokens(mime, data_len); | ||
| } |
Comment on lines
+79
to
+83
| /// Estimate token cost for audio from a URL. | ||
| /// Audio is tokenized at roughly 32 tokens per second. | ||
| /// From base64, we estimate duration from payload size. | ||
| fn estimate_media_tokens_from_url(url: &str) -> u32 { | ||
| const BASE_AUDIO_TOKENS: u32 = 500; // ~15 seconds default |
CarlitoDon
force-pushed
the
fix/media-token-estimation
branch
from
July 15, 2026 00:33
17e85f9 to
7044932
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Validation