llm-router-builder
Skill1 starsChanged 5 months agoApache-2.0View on GitHub
Builds an LLM router that selects the right model or prompt based on task complexity, cost, or latency. Use when optimizing AI API costs or routing queries to specialized models.
In this repository58 other files it gives its agents.
- agent-chain-designerSkill
- api-wrapper-builderSkill
- embeddings-builderSkill
- evaluation-harnessSkill
- fine-tuning-preparerSkill
- ml-model-trainerSkill
- model-benchmarkerSkill
- prompt-engineerSkill
- rag-pipeline-builderSkill
- synthetic-data-generatorSkill
- vector-db-integratorSkill
- csv-processorSkill
- database-schema-designerSkill
- data-pipeline-builderSkill
- etl-workflow-designerSkill
- json-transformerSkill
- pandas-data-cleanerSkill
- report-generatorSkill
- sql-query-optimizerSkill
- time-series-analyzerSkill
- visualization-generatorSkill
- backup-strategySkill
- ci-cd-optimizerSkill
- docker-compose-generatorSkill
- github-actions-pipelineSkill
- kubernetes-deploymentSkill
- load-balancer-configSkill
- log-aggregator-setupSkill
- monitoring-dashboardSkill
- nginx-config-generatorSkill
- security-hardeningSkill
- ssl-certificate-managerSkill
- terraform-module-builderSkill
- api-security-auditorSkill
- compliance-checkerSkill
- dependency-checkerSkill
- incident-responderSkill
- jwt-security-auditorSkill
- owasp-checkerSkill
- penetration-test-plannerSkill
- secret-detectorSkill
- threat-model-designerSkill
- vulnerability-scannerSkill
- accessibility-auditorSkill
- api-route-builderSkill
- auth-flow-builderSkill
- css-animation-crafterSkill
- form-validatorSkill
- graphql-schema-designerSkill
- html-component-builderSkill
- nextjs-app-scaffoldSkill
- performance-auditorSkill
- pwa-setupSkill
- react-component-generatorSkill
- responsive-layout-systemSkill
- seo-optimizerSkill
- tailwind-design-systemSkill
- websocket-integrationSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 5 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 37
Document cost per 1M tokens and typical latency for each.
- Distinctive: line 43
Cascading: Always try cheap model first.
- Distinctive: line 47
If cheap model returns low-quality (detected by length, refusal, validation failure, or a judge), retry with next tier.
- Distinctive: line 48
Always have a "best effort" expensive fallback.
- Distinctive: line 81
Cache poisoning: Only cache when the answer was high-quality (validated or judged good).
- Very specific: line 82
Cost tracking: Always log model, input_tokens, output_tokens, cost for every call.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Overview
Builds an intelligent LLM router that chooses the cheapest/fastest/ most appropriate model or prompt for each incoming request. Covers routing strategies (complexity classifier, embedding similarity, regex/rules, cascading), model tier design, fallback logic, caching, and a complete, production-ready Python router implementation supporting OpenAI + Anthropic + local models.
When to Use This Skill
- High LLM spend and you want to reduce cost without hurting quality too much.
- Different queries have very different complexity (simple FAQ vs deep reasoning).
- You have access to multiple models (cheap fast ones + expensive smart ones) and want to use the right one per task.
- Building a multi-model gateway or agent that needs to be cost-aware.
Prerequisites
- Access to at least two LLMs with different cost/quality/latency profiles.
- A way to classify or score incoming queries (or willingness to build one).
- (Recommended) Embedding model for similarity-based routing.
- Logging of token usage and quality signals.
Steps
- Define model tiers:
- Tier 0 (free/fast): small local model, rule-based, or cached answers.
- Tier 1 (cheap): GPT-4o-mini, Claude 3 Haiku, Gemini Flash, etc.
- Tier 2 (smart/expensive): GPT-4o, Claude 3.5 Sonnet, o1, etc.
- Document cost per 1M tokens and typical latency for each.
- Routing strategies (pick one or combine):
- Rules / regex: Simple, zero-cost (e.g., "if query contains 'code' → coding model").
- Complexity classifier: Fine-tune or prompt a small model to output "simple | medium | complex".
- Embedding similarity: Embed the query and route based on similarity to known hard/easy example sets.
- Cascading: Always try cheap model first; if confidence low or output fails validation, escalate to expensive model.
- Task type: Route coding questions to a coding-specialized model, math to a math model, etc.
- Fallback & escalation:
- If cheap model returns low-quality (detected by length, refusal, validation failure, or a judge), retry with next tier.
- Always have a "best effort" expensive fallback.
- Caching:
- Exact match cache for identical queries.
- Semantic cache (embed query, retrieve similar past answers if similarity > threshold).
- TTL and invalidation strategy.
- Implementation:
- Clean
Routerclass withroute(query) -> model_nameandcomplete(query, **kwargs). - Pluggable classifiers and caches.
- Logging of every decision + cost.
- Optional: async support.
- Clean
- Output:
- Complete
llm_router.pywith multiple strategies implemented. - Example model tier config (JSON or dataclass).
- Complexity classifier prompt or fine-tuning dataset sketch.
- Caching implementation (exact + semantic).
- Dashboard or log analysis script to see routing distribution and savings.
- Complete
Examples
A full router that:
- Uses a small classifier (or LLM judge) to label complexity.
- Routes simple queries to Haiku / 4o-mini.
- Routes complex reasoning to Sonnet / 4o.
- Falls back on validation failure.
- Uses semantic cache for repeated questions. With usage example and cost savings simulation.
Edge Cases & Error Handling
- Classifier is wrong: The cascade/fallback catches most mistakes.
- Cache poisoning: Only cache when the answer was high-quality (validated or judged good).
- Cost tracking: Always log
model,input_tokens,output_tokens,costfor every call.
Verification
- The router code runs and makes decisions.
- On a test set of easy/medium/hard queries, it routes the majority of easy ones to cheap models and hard ones to expensive models.
- Quality on the test set is close to "always use expensive" while cost is significantly lower.
- Fallback triggers correctly on low-quality cheap outputs.
- Cache hits reduce cost/latency on repeated queries.
- Success: You have a measurable reduction in LLM spend with acceptable quality impact, and the system is easy to tune and monitor.
References
- LLM Routing Papers & Techniques
- Semantic Caching
- OpenAI + Anthropic SDKs
- LiteLLM (great for multi-provider abstraction)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

