kmail.at
contact

elsewhere

EnglishEN
← learning

langchain · difficulty ◆◆◆

ProviderToolSearchMiddleware: Auto-Routed Tool Selection

Stop hand-rolling tool routers. Let the provider pick the relevant tools per query.

Hundreds of tools, one query. Let the provider find the three that matter instead of prompting a brittle router.

fully AI-generatedThis text is fully AI-generated and was published without human editorial review.

2026-06-11 · 8 min read

$ pip install langchain==1.3.7 langchain-core==1.4.5
langchain 1.3.7
Release
#37969
PR

New middleware

similarity / mmr
Retrieval

What it does

ProviderToolSearchMiddleware (langchain==1.3.7, PR #37969) intercepts chat model invocations and routes queries through a provider-aware tool search step before the model generates a response. Every user message first triggers a tool lookup using the model’s own provider ecosystem (OpenAI, Anthropic, Google) to surface relevant tools from your registry. Only then does the model respond, armed with the retrieved tool definitions.

Why it matters

In production RAG and agent pipelines you often have dozens or hundreds of tools. Passing all of them to every request is expensive and slow; hand-coding a tool router prompt is brittle and model-specific. This middleware offloads selection to a provider-native search step, giving cost reduction, better accuracy, and cleaner code.

Example

$ Agent query with auto tool routing
> Entering new AgentExecutor chain...

{'input': 'What's the weather in Paris?', 'output': 'The weather in Paris is sunny and 72°F.'}

> Chain finished

The middleware surfaces only the relevant tools before the model responds, cutting token cost.

Common flags

search_type="similarity"
Similarity-based tool retrieval
search_type="mmr"
Max marginal relevance for diversity
top_k=3
Retrieve the top N tools per query

History

Origin

Added in langchain==1.3.7 (released June 10, 2026), PR #37969, with release notes in langchain==1.3.7 (PR #38024).

Contrast with tool injection

Unlike manual tool schema injection into every prompt, the middleware makes routing automatic and context-aware via the provider.

Under the hood

When it shines

This pattern is especially valuable in agentic RAG, tool-augmented chat, and multi-tool assistants where the tool registry is large enough that unconditional injection becomes a real cost and latency problem.

Fun facts

Pros & cons

pros

  • + Fewer tokens per call
  • + Provider-side relevance vs brittle heuristics
  • + No manual router prompt

cons

  • − Adds a search round-trip
  • − Depends on provider ecosystem

Takeaways

  1. 1Attach ProviderToolSearchMiddleware to auto-route tools per query.
  2. 2Choose similarity or mmr search and set top_k for your registry.
  3. 3Measure token savings with usage_metadata to justify adoption.

Related commands

archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →

← all learning