LLM routing — decide whether a query needs a tool or can be answered directly using hidden-state probes.
Project description
ToolGate
LLM routing using hidden-state probes — decide in milliseconds whether a query needs a tool (calculator, search, API) or can be answered directly from the model's memory.
Calling an external tool for every query is expensive and slow. ToolGate reads the model's internal representations before generating a response and predicts tool necessity with high accuracy — skipping unnecessary tool calls and reducing API costs.
How it works
- A language model processes the input prompt and produces hidden states.
- A lightweight logistic probe (trained on labeled examples) reads those hidden states and outputs a probability that a tool is needed.
- If
p > tau, the query is routed to the tool; otherwise the model answers directly.
Installation
pip install toolgate
# With the FastAPI server:
pip install "toolgate[server]"
Quickstart
from toolgate import ToolGateConfig, ToolProbe, ToolGate
from toolgate.data import load_json_dataset
cfg = ToolGateConfig(model_name="Qwen/Qwen2.5-1.5B-Instruct", tau=0.5)
probe = ToolProbe(cfg)
prompts, labels = load_json_dataset("data/toy_when2tool.json")
probe.train(prompts, labels)
probe.save()
gate = ToolGate(probe)
result = gate.generate("What is 918273 * 33?", max_new_tokens=60)
print(result["tool_needed"]) # True
print(result["response"])
REST API server
uvicorn toolgate.server:app --reload --port 8000
Open http://localhost:8000 for the live dashboard showing:
- Per-query routing decisions with confidence scores
- Real-time cost comparison (with vs without ToolGate)
- Query history with latency
Benchmark
Evaluated on the toy_when2tool dataset:
| Metric | Value |
|---|---|
| Accuracy | 100% |
| Tool-call rate | 50% |
| Avg latency | ~800ms (RTX 5050) |
| Cost reduction | ~49% vs always-calling |
Training your own probe
# Collect labeled examples: (prompt, label) where label=1 means tool needed
prompts = ["What is 12 * 7?", "Who wrote Hamlet?"]
labels = [1, 0]
probe = ToolProbe(cfg)
probe.train(prompts, labels)
probe.save("my_probe.pkl")
Project structure
toolgate/
├── __init__.py # Public API
├── config.py # ToolGateConfig dataclass
├── probe.py # ToolProbe — hidden-state logistic classifier
├── gate.py # ToolGate — end-to-end routing + generation
├── data.py # Dataset loading utilities
└── integrations/
├── langchain.py # LangChain integration
└── raw_hf.py # Raw HuggingFace integration
server.py # FastAPI server + dashboard
Citation
If you use ToolGate in research, please cite:
@software{toolgate2025,
author = {Tayade, Tanishq},
title = {ToolGate: LLM Routing via Hidden-State Probes},
year = {2025},
url = {https://github.com/tanishqtayade/toolgate}
}
License
MIT
Project details
Release history Release notifications | RSS feed
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 toolgate_probe-0.1.0.tar.gz.
File metadata
- Download URL: toolgate_probe-0.1.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7aa39c7cfe325fa50e9df5b997b215f60c32879d0bde39fe60f22ff665786cb
|
|
| MD5 |
8c803a2c2d608670326164f93e51580c
|
|
| BLAKE2b-256 |
bfd1896a11eb734fb5dcac53ea4d49cbef85995c70c0348de941544d32d8366f
|
File details
Details for the file toolgate_probe-0.1.0-py3-none-any.whl.
File metadata
- Download URL: toolgate_probe-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 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 |
9154020ac72b8a9815a5ad60ce9b9533b255a68adbd0626863bf1f66c36d90ff
|
|
| MD5 |
ee5d2bdb18697854b55c078ceefa26e2
|
|
| BLAKE2b-256 |
38398cdfb704daf409d477539374ba0bad06005db819202914fc9d944e799a5a
|