Skip to content

Comments

Improve token usage estimate for images#12419

Merged
etraut-openai merged 6 commits intomainfrom
etraut/triage-codex-issue-11845
Feb 21, 2026
Merged

Improve token usage estimate for images#12419
etraut-openai merged 6 commits intomainfrom
etraut/triage-codex-issue-11845

Conversation

@etraut-openai
Copy link
Collaborator

@etraut-openai etraut-openai commented Feb 21, 2026

Fixes #11845.

Adjust context/token estimation for inline image data:*;base64,... URLs so we
do not count the raw base64 payload as model-visible text.

What changed:

  • keep the existing JSON-length estimator as the baseline
  • detect only inline base64 data: image URLs in message and function-call
    output content items
  • subtract only the base64 payload bytes (preserving data URL prefix + JSON
    overhead)
  • add a fixed per-image estimate of 340 bytes (~85 tokens at the repo’s
    4-bytes/token heuristic)

This avoids large overestimates from MCP image tool outputs while leaving normal
image URLs (https://, file://, non-base64 data: URLs) unchanged.

Tests:

  • message image data URL estimate regression
  • function-call output image data URL estimate regression
  • non-base64 image URLs unchanged
  • non-base64 data: URLs unchanged
  • data:application/octet-stream;base64,... adjusted
  • multiple inline images apply multiple fixed costs
  • text-only items unchanged

@etraut-openai etraut-openai changed the title Adjust base64 image estimator to trim payload payload bytes Improve token usage estimate for images Feb 21, 2026
@etraut-openai
Copy link
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link
Contributor

Codex Review: Didn't find any major issues. 👍

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Security Review

Here are some automated security review suggestions for this pull request.

Reviewed commit: a884977d63

View security finding report

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@etraut-openai
Copy link
Collaborator Author

@codex review

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 243b92a804

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Security Review

Here are some automated security review suggestions for this pull request.

Reviewed commit: 243b92a804

View security finding report

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@etraut-openai etraut-openai merged commit 3586fcb into main Feb 21, 2026
53 of 55 checks passed
@etraut-openai etraut-openai deleted the etraut/triage-codex-issue-11845 branch February 21, 2026 22:25
@github-actions github-actions bot locked and limited conversation to collaborators Feb 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context remaining/token estimate overcounts MCP image data URLs (base64)

2 participants