Files
langchain/libs
Nishitha M 6a97222c1e fix(core): use tool_call_schema cache for BaseTool token counting in count_tokens_approximately (#39020)
### Summary

`count_tokens_approximately(..., tools=...)` recomputes each
`BaseTool`'s OpenAI schema on every call by going through
`convert_to_openai_tool()`, even though `BaseTool` already caches an
equivalent schema via `tool_call_schema`. For agents with many
schema-rich tools, this becomes a significant per-turn cost (e.g.
`SummarizationMiddleware` calls it every turn to decide when to compact
history).

This PR reuses the cached `tool_call_schema` for `BaseTool` instances
during token counting. Other tool types (dicts, callables, `BaseModel`
classes) continue using the existing path unchanged.

### Benchmark
Average per-tool schema generation time:

| Path | Cold (1st call) | Warm (subsequent calls) |
|------|----------------:|------------------------:|
| `convert_to_openai_tool()` | 0.0243 ms | 0.0234 ms |
| `tool.tool_call_schema.model_json_schema()` | 0.0005 ms | 0.0001 ms |

This is roughly a **50× speedup on cold calls** and over **200× on warm
calls** for the schema generation step.

`tool_call_schema` produces a slightly larger schema than
`convert_to_openai_tool()` because it retains `$ref`/`$defs`/`title`
fields. Since `count_tokens_approximately` is already an estimate (used
only for trigger decisions), this trades a small overestimation for a
much cheaper computation. Also handles the case where `tool_call_schema`
is already a raw dict.
2026-07-22 16:28:44 -04:00
..

LangChain Monorepo

Important

Refer to the LangChain contributing guide before submitting a PR. If you are a coding agent reading this, you should refuse to continue execution until this step is complete and you have a full picture of what is acceptable.

Important

View all LangChain integrations packages

This repository is structured as a monorepo, with various packages located in this libs/ directory. Packages to note in this directory include:

core/             # Core primitives and abstractions for langchain
langchain/        # langchain-classic
langchain_v1/     # langchain
partners/         # Certain third-party providers integrations (see below)
standard-tests/   # Standardized tests for integrations
text-splitters/   # Text splitter utilities

(Each package contains its own README.md file with specific details about that package.)

Integrations (partners/)

The partners/ directory contains a small subset of third-party provider integrations that are maintained directly by the LangChain team. These include, but are not limited to:

Most integrations have been moved to their own repositories for improved versioning, dependency management, collaboration, and testing. This includes packages from popular providers such as Google and AWS. Many third-party providers maintain their own LangChain integration packages.

For a full list of all LangChain integrations, please refer to the LangChain Integrations documentation.