Intelligent tool routing for LLMs with 95.8% reduction in context size
Project description
๐ง LLM Smart Router
Intelligent tool routing for LLMs with 95.8% reduction in context size
๐ฏ Problem
Modern LLM agents have access to hundreds of tools, but passing all tools in the context:
- ๐ Exceeds token limits (100+ tools = 50K+ tokens)
- ๐ฐ Increases costs significantly
- ๐ Slows down inference
- ๐ฒ Confuses the LLM (too many choices)
๐ก Solution
Smart Router intelligently reduces 90+ tools to 3-8 relevant tools per query:
93 tools โ LLM identifies 1-3 domains โ 3-8 relevant tools
Results: 95.8% reduction, <200ms latency, 90% confidence
โก Quick Start
Installation
pip install llm-smart-router
Basic Usage
from smart_router import SmartRouter, ToolRegistry, Domain
# Define your domains
class MyDomains(Domain):
METRICS = "metrics"
LOGS = "logs"
SECURITY = "security"
# Register tools by domain
registry = ToolRegistry()
registry.register_tool("get_metrics", MyDomains.METRICS, "Retrieve system metrics")
registry.register_tool("search_logs", MyDomains.LOGS, "Search application logs")
registry.register_tool("scan_vulnerabilities", MyDomains.SECURITY, "Scan for security issues")
# ... register 90 more tools
# Initialize router
router = SmartRouter(
registry=registry,
llm_provider="openai",
model="gpt-4o-mini"
)
# Route a query
decision = router.route("API is slow since last commit")
print(f"Selected domains: {decision.domains}") # [METRICS, LOGS]
print(f"Selected tools: {len(decision.tools)}") # 5 tools instead of 93
print(f"Confidence: {decision.confidence:.1%}") # 92%
# Use with LangChain
from langchain.agents import AgentExecutor
agent = AgentExecutor(
tools=decision.tools, # Only 5 relevant tools
llm=llm,
verbose=True
)
๐๏ธ Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ User Query โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Smart Router (LLM-based) โ
โ โข Analyzes query semantics โ
โ โข Identifies 1-3 relevant domains โ
โ โข Confidence scoring โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Tool Registry โ
โ โข Maps domains โ tools โ
โ โข Returns 3-8 relevant tools โ
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ LangChain Agent (reduced context) โ
โ โข Uses only relevant tools โ
โ โข Faster inference โ
โ โข Lower costs โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Performance
| Metric | Before | After | Improvement |
|---|---|---|---|
| Tools in context | 93 | 3-8 | 95.8% โ |
| Prompt size | ~45K tokens | ~2K tokens | 95.6% โ |
| Latency | ~3s | <200ms | 93% โ |
| LLM confusion | High | Low | Focused |
| Cost per query | $0.45 | $0.02 | 95% โ |
๐ง Features
- โ Domain-based organization - Group tools by functional domains
- โ LLM-powered routing - Semantic understanding of queries
- โ Multi-domain support - Handle complex queries spanning multiple domains
- โ Confidence scoring - Know when routing is uncertain
- โ Provider agnostic - Works with OpenAI, Groq, Anthropic, local LLMs
- โ LangChain compatible - Drop-in replacement for tool selection
- โ Extensible - Easy to add custom domains and tools
- โ Fast - <200ms routing decision
- โ Type-safe - Full Pydantic models
๐ Documentation
Define Domains
from enum import Enum
class AGDomain(str, Enum):
"""Augmented Generation Domains"""
MAAG = "metrics" # Metrics & Monitoring
LAAG = "logs" # Logs & Analysis
CAAG = "code" # Code & Changes
SAAG = "security" # Security & Compliance
DAAG = "data_quality" # Data Quality
# ... add your domains
Register Tools
from smart_router import ToolRegistry, ToolMetadata
registry = ToolRegistry()
# Option 1: Register from LangChain tools
from langchain.tools import Tool
tool = Tool(
name="get_metrics",
func=lambda x: ...,
description="Retrieve system metrics"
)
registry.register_tool_from_langchain(tool, AGDomain.MAAG)
# Option 2: Register manually
registry.register_tool(
name="search_logs",
domain=AGDomain.LAAG,
description="Search application logs",
parameters={"query": "string", "days": "int"}
)
# Option 3: Bulk registration from definitions
tools_definitions = [
{"name": "get_cpu_usage", "domain": "metrics", "description": "..."},
{"name": "get_memory_usage", "domain": "metrics", "description": "..."},
# ... 90 more
]
registry.bulk_register(tools_definitions)
Configure Router
from smart_router import SmartRouter, RouterConfig
config = RouterConfig(
llm_provider="openai",
model="gpt-4o-mini",
temperature=0.1,
max_domains=3,
confidence_threshold=0.7,
system_prompt_template="path/to/custom_prompt.md"
)
router = SmartRouter(registry=registry, config=config)
Custom System Prompt
# Use your own routing prompt
router = SmartRouter(
registry=registry,
system_prompt_file="prompts/my_custom_routing.md"
)
See examples/custom_prompt.md for template.
Advanced Usage
# Get detailed routing decision
decision = router.route(
query="API performance degraded after deployment",
explain=True # Include reasoning
)
print(decision.reasoning)
# "Query mentions 'performance' (MAAG) and 'after deployment' (CAAG).
# Two domains selected for comprehensive analysis."
# Force specific domains
decision = router.route(
query="Check security",
force_domains=[AGDomain.SAAG, AGDomain.LAAG]
)
# Use with streaming
for chunk in router.route_streaming(query):
print(chunk.domain, chunk.confidence)
๐จ Examples
Example 1: Simple Routing
query = "What's the CPU usage?"
decision = router.route(query)
# โ Domain: MAAG (metrics)
# โ Tools: [get_cpu_usage, get_system_metrics]
Example 2: Multi-Domain Routing
query = "API slow since last commit, check logs and metrics"
decision = router.route(query)
# โ Domains: [MAAG, LAAG, CAAG]
# โ Tools: [get_metrics, search_logs, get_recent_commits, analyze_performance]
Example 3: Integration with LangChain
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI
# Route query
decision = router.route(user_query)
# Create agent with only relevant tools
llm = ChatOpenAI(model="gpt-4o")
agent = create_openai_functions_agent(llm, decision.tools, prompt)
executor = AgentExecutor(agent=agent, tools=decision.tools)
# Execute
result = executor.invoke({"input": user_query})
See examples/ for more complete examples.
๐งช Testing
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=smart_router --cov-report=html
# Type checking
mypy smart_router
# Linting
ruff check smart_router
๐ค Contributing
Contributions welcome! Please read CONTRIBUTING.md first.
Development Setup
# Clone repo
git clone https://github.com/monsau/llm-smart-router.git
cd llm-smart-router
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install in dev mode
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
Roadmap
- Support for async routing
- Caching layer for frequent queries
- Multi-LLM routing strategies
- A/B testing framework
- Integration with LlamaIndex
- Embeddings-based routing (no LLM call)
- GUI for tool registry management
- Monitoring dashboard
๐ License
MIT License - see LICENSE file for details.
๐ Acknowledgments
- Inspired by real-world challenges with 90+ tools in production
- Built for the LangChain community
- Tested in production with OpenMetadata integration
๐ Links
- Documentation: https://llm-smart-router.readthedocs.io
- PyPI: https://pypi.org/project/llm-smart-router
- Issues: https://github.com/monsau/llm-smart-router/issues
- Discussions: https://github.com/monsau/llm-smart-router/discussions
๐ Citation
If you use this in research, please cite:
@software{llm_smart_router,
title = {LLM Smart Router: Intelligent Tool Routing for Large Language Models},
author = {Your Name},
year = {2025},
url = {https://github.com/monsau/llm-smart-router}
}
Made with โค๏ธ for the LLM community
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_smart_router-0.1.3.tar.gz.
File metadata
- Download URL: llm_smart_router-0.1.3.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1796accc0103d548fc78daa5eb38d5059ad53226e3bbdea71bc40999fd0ac5e
|
|
| MD5 |
60c280db8f01d2d1c8cdce5fc669b84b
|
|
| BLAKE2b-256 |
e094cc4d7bde2673f4853b80847654a79518d763d389fcefaea3a703e0f0123d
|
File details
Details for the file llm_smart_router-0.1.3-py3-none-any.whl.
File metadata
- Download URL: llm_smart_router-0.1.3-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99b62ba93d8436afed309c191a6587872d04eb7d6b3f89323c29653feb832a2f
|
|
| MD5 |
a24939309e8b52014d6f61fcae257ce6
|
|
| BLAKE2b-256 |
8b211aa9b5a91b99b4eb9220f5dfad6b8317c7e2f9a88f875bbeec58275ee982
|