Skip to content

feat: Add OpenAITokenCounter using their input tokens api - #12237

Merged
sjrl merged 4 commits into
mainfrom
add-openai-token-counter
Aug 4, 2026
Merged

feat: Add OpenAITokenCounter using their input tokens api#12237
sjrl merged 4 commits into
mainfrom
add-openai-token-counter

Conversation

@sjrl

@sjrl sjrl commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Related Issues

  • fixes #issue-number

Proposed Changes:

Follow up to #12195

Add OpenAITokenCounter, which uses OpenAI's input token counting API to return model-specific counts for
Haystack ChatMessage objects and optional tool schemas. Unlike local estimates, it supports OpenAI's exact
accounting for request formatting, images, files, and tools.

Here is an example:

from haystack.dataclasses import ChatMessage
from haystack.token_counters import OpenAITokenCounter

counter = OpenAITokenCounter("gpt-5-mini")
count = counter.count([ChatMessage.from_user("Hello!")])

I also added docs pages for the new token counter.

How did you test it?

Added new tests.

Notes for the reviewer

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@sjrl sjrl self-assigned this Aug 4, 2026
@vercel

vercel Bot commented Aug 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Preview Aug 4, 2026 2:00pm

Request Review

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/core/pipeline
  pipeline.py
  haystack/token_counters
  __init__.py
  openai_counter.py 104
Project Total  

This report was generated by python-coverage-comment-action

@sjrl
sjrl marked this pull request as ready for review August 4, 2026 11:28
@sjrl
sjrl requested a review from a team as a code owner August 4, 2026 11:28
@sjrl
sjrl requested review from anakin87 and removed request for a team August 4, 2026 11:28
Comment thread pyproject.toml

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only a minor comment

Comment thread test/token_counters/test_openai_counter.py Outdated
@sjrl
sjrl enabled auto-merge (squash) August 4, 2026 14:00
@sjrl sjrl changed the title feat: Add OpenaiTokenCounter using their input tokens api feat: Add OpenAITokenCounter using their input tokens api Aug 4, 2026
@sjrl
sjrl merged commit c07313f into main Aug 4, 2026
26 checks passed
@sjrl
sjrl deleted the add-openai-token-counter branch August 4, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants