Skip to content

Conversation

@christian-bromann
Copy link
Member

Add BigToolMiddleware for Dynamic Tool Selection

This PR introduces a new BigToolMiddleware that enables intelligent tool selection for agents with large tool sets (1000+ tools), preventing context window explosion while maintaining functionality.

When agents have access to hundreds or thousands of tools, sending all tool descriptions with every model request can:

  • Exhaust context windows (50,000+ tokens for 1000 tools)
  • Increase API costs significantly
  • Slow down response times
  • Reduce available context for actual conversation

Solution

The BigToolMiddleware dynamically selects only the most relevant tools for each query using configurable strategies:

  • Keyword Matching: Selects tools based on keyword overlap between query and tool names/descriptions
  • Semantic Similarity: Uses text similarity to find the most relevant tools
  • Custom Logic: Allows users to implement their own tool selection algorithms

Usage Examples

Basic Keyword Strategy

import { bigToolMiddleware } from "langchain/middleware";

const agent = createAgent({
  model: "openai:gpt-4",
  tools: [...manyTools], // 1000+ tools
  middleware: [
    bigToolMiddleware({
      strategy: "keyword",
      maxTools: 20,
      keywordConfig: {
        keywords: ["file", "database", "api", "search"],
        matchDescriptions: true,
        minMatches: 1
      }
    })
  ]
});

Semantic Similarity Strategy

const semanticMiddleware = bigToolMiddleware({
  strategy: "semantic",
  semanticConfig: {
    threshold: 0.4,
    maxTools: 15
  }
});

Custom Selection Logic

const customMiddleware = bigToolMiddleware({
  strategy: "custom",
  customSelector: async (tools, query, context) => {
    // Your custom logic here
    return tools.filter(tool => someCustomLogic(tool, query)).slice(0, 10);
  }
});

@vercel
Copy link

vercel bot commented Sep 16, 2025

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

Project Deployment Preview Comments Updated (UTC)
langchainjs-docs Ready Ready Preview Comment Sep 18, 2025 1:32am
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Sep 18, 2025 1:32am

@changeset-bot
Copy link

changeset-bot bot commented Sep 16, 2025

⚠️ No Changeset found

Latest commit: 0f3cf6a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Base automatically changed from cb/mcp-updates to cb/agent-refinements September 18, 2025 16:25
Base automatically changed from cb/agent-refinements to v1 September 18, 2025 18:38
@christian-bromann
Copy link
Member Author

Closing in favor of #9050

@christian-bromann christian-bromann deleted the cb/big-tool-middleware branch September 26, 2025 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants