Files
langchain/libs/core
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
..
2026-06-12 14:54:25 -04:00
2026-07-20 16:36:00 -04:00
2026-07-20 16:36:00 -04:00

🦜🍎 LangChain Core

PyPI - Version PyPI - License PyPI - Downloads Twitter

Looking for the JS/TS version? Check out LangChain.js.

To help you ship LangChain apps to production faster, check out LangSmith. LangSmith is a unified developer platform for building, testing, and monitoring LLM applications.

Quick Install

uv add langchain-core

🤔 What is this?

LangChain Core contains the base abstractions that power the LangChain ecosystem.

These abstractions are designed to be as modular and simple as possible.

The benefit of having these abstractions is that any provider can implement the required interface and then easily be used in the rest of the LangChain ecosystem.

⛰️ Why build on top of LangChain Core?

The LangChain ecosystem is built on top of langchain-core. Some of the benefits:

  • Modularity: We've designed Core around abstractions that are independent of each other, and not tied to any specific model provider.
  • Stability: We are committed to a stable versioning scheme, and will communicate any breaking changes with advance notice and version bumps.
  • Battle-tested: Core components have the largest install base in the LLM ecosystem, and are used in production by many companies.

📖 Documentation

For full documentation, see the API reference. For conceptual guides, tutorials, and examples on using LangChain, see the LangChain Docs. You can also chat with the docs using Chat LangChain.

📕 Releases & Versioning

See our Releases and Versioning policies.

💁 Contributing

As an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.

For detailed information on how to contribute, see the Contributing Guide.

Resources

  • LangChain Academy — comprehensive, free courses on LangChain libraries and products, made by the LangChain team
  • Code of Conduct — community guidelines and standards