langchain - ✅(Solved) Fix Support OpenAI token count API [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#36661Fetched 2026-04-11 06:12:19
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
labeled ×3issue_type_added ×1

PR fix notes

PR #36675: langchain_openai: use OpenAI token count API in get_num_tokens_from_m…

Description (problem / solution / changelog)

fixes #36661 so right now get_num_tokens_from_messages uses tiktoken which is just an estimate and also throws NotImplementedError for any model that isn't gpt-3.5 or gpt-4. openai has an actual token count api now so this just uses that instead, same way langchain-anthropic does it. also tools were just being ignored with a warning earlier, now they are getting forwarded to the api call properly. falls back to tiktoken if the api call fails so nothing breaks for existing users. I have also added 9 unit tests covering the api path, tools forwarding, and the tiktoken fallback.

Changed files

  • libs/partners/openai/langchain_openai/chat_models/base.py (modified, +87/-85)
  • libs/partners/openai/tests/unit_tests/test_token_counts.py (modified, +201/-1)
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a feature request, not a bug report or usage question.
  • I added a clear and descriptive title that summarizes the feature request.
  • I used the GitHub search to find a similar feature request and didn't find it.
  • I checked the LangChain documentation and API reference to see if this feature already exists.
  • This is not related to the langchain-community package.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Feature Description

OpenAI now has a token count API that allows for accurate input token counting: https://developers.openai.com/api/reference/resources/responses/subresources/input_tokens/methods/count

Should the langchain-openai library use this? (Similar to the approach the langchain-anthropic lib uses? )

Use Case

I am trying to ensure accurate token counts are reported.

Proposed Solution

No response

Alternatives Considered

No response

Additional Context

No response

extent analysis

TL;DR

The langchain-openai library should consider utilizing OpenAI's token count API for accurate input token counting.

Guidance

  • Review the OpenAI token count API documentation to understand its usage and benefits.
  • Investigate how the langchain-anthropic library implements token counting to determine if a similar approach can be applied to langchain-openai.
  • Evaluate the potential impact of integrating the token count API on the existing functionality of the langchain-openai library.
  • Consider reaching out to the maintainers of the langchain-openai library to discuss the feasibility of implementing this feature.

Example

No code example is provided as the issue is a feature request and does not include specific implementation details.

Notes

The implementation of the token count API may require changes to the langchain-openai library's architecture and testing framework to ensure accurate token counting.

Recommendation

Apply workaround: Utilize the OpenAI token count API in the langchain-openai library to improve token counting accuracy, as it provides a more reliable method for counting input tokens.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING