Skip to content

fix(proxy): account for media tokens in context estimation#3250

Merged
lbjlaq merged 2 commits into
lbjlaq:mainfrom
CarlitoDon:fix/media-token-estimation
Jul 15, 2026
Merged

fix(proxy): account for media tokens in context estimation#3250
lbjlaq merged 2 commits into
lbjlaq:mainfrom
CarlitoDon:fix/media-token-estimation

Conversation

@CarlitoDon

Copy link
Copy Markdown
Contributor

Summary

  • account for OpenAI image_url and audio_url content blocks during context token estimation
  • account for Gemini inlineData media payloads during context token estimation
  • prevents media-heavy payloads from bypassing compression thresholds and reaching Gemini as oversized requests

Validation

  • cargo check

Copilot AI review requested due to automatic review settings July 15, 2026 00:23
@CarlitoDon
CarlitoDon force-pushed the fix/media-token-estimation branch from 8f43b8a to 165f4c6 Compare July 15, 2026 00:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_url and audio_url content 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
CarlitoDon force-pushed the fix/media-token-estimation branch from 17e85f9 to 7044932 Compare July 15, 2026 00:33
@lbjlaq
lbjlaq merged commit bf73afd into lbjlaq:main Jul 15, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants