Skip to content

fix: budget tool images against the selected model context window - #4032

Open
uiharuayako wants to merge 1 commit into
openBitFun:mainfrom
uiharuayako:codex/fix-tool-image-context-budget
Open

uiharuayako wants to merge 1 commit into
openBitFun:mainfrom
uiharuayako:codex/fix-tool-image-context-budget

Conversation

@uiharuayako

Copy link
Copy Markdown
Contributor

Fixes #4031.

Summary

Tool-result images previously added 850 estimated tokens each, regardless of their pixel dimensions. Repeated screenshot reads could therefore pass the local context check and exceed the model's input budget.

Read the dimensions from the encoded attachment, including when replaying existing sessions. At the shared send boundary, budget the actual request's messages, tools and images against the selected model's context window, with room reserved for output. Known vision processors use model-specific rules. Custom model IDs use the existing tile estimate.

When space is tight, replace the oldest images with text in the outgoing copy. Keep the latest screenshot at full resolution when that is sufficient. Otherwise prepare a proportional preview or return crop/batch guidance before transport. Tool-call IDs and stored attachments are preserved.

Verification

All 268 local tests below passed (86 adapter, 28 core and 154 configuration tests).

  • cargo test --locked -p openbitfun-ai-adapters --features vision-input --lib -- client::image_budget vision_tokens providers::openai::message_converter providers::gemini::message_converter client::tests::aggregated client::responses_websocket
  • cargo test --locked -p openbitfun-core --no-default-features --features agent-runtime,git --lib -- context_budget restored_tool_results image_projection configured_image_limit image_input_requirement auto_compression_pressure prompt_suggestion elision multi_channel_pool_preserves_history_protocol_credentials_and_actual_usage
  • cargo test --locked -p openbitfun-config-contracts --lib
  • cargo check --locked -p openbitfun-ai-adapters --no-default-features --lib
  • pnpm run check:core-boundaries, pnpm run i18n:audit and git diff --check

The PNG regression measures increments of 250 tokens at 512×512 and 3,250 at 2048×2048 with the model-neutral estimate. A separate regression uses a screenshot with small lettering to check all three budget outcomes and the original attachment bytes. Local HTTP tests inspect outgoing image dimensions, model selection and preflight rejection.

AI-assisted, tested locally with synthetic requests and loopback providers. Live provider usage and remote-device scenarios were not exercised.

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.

[Bug]: 工具返回的图片固定按 850 token 估算,多轮读图可能超出上下文窗口

1 participant