Skip to main content

Basic AI model router for cost optimization

Project description

APICrusher Lite

Open source AI model router that automatically reduces API costs by routing simple queries to cheaper models.

The Problem

You're using GPT-5 or Claude Opus 4.1 for everything. Even for tasks like:

  • Formatting JSON
  • Extracting emails from text
  • Basic string operations
  • Simple yes/no questions

That's like hiring a brain surgeon to apply band-aids.

The Solution

This lightweight router analyzes query complexity and automatically routes simple requests to cheaper models while preserving quality for complex tasks.

# Before: Everything goes to expensive models
response = openai.chat.completions.create(
    model="gpt-5",  # $1.25/$10 per million tokens (input/output)
    messages=[{"role": "user", "content": "Extract the email from: Contact john@example.com"}]
)

# After: Simple tasks use cheaper models automatically
from apicrusher_lite import Router

router = Router()
model = router.route("gpt-5", messages)  # Returns "gpt-5-nano" for simple tasks
response = openai.chat.completions.create(model=model, messages=messages)

Installation

pip install apicrusher-lite

Basic Usage

from apicrusher_lite import Router

# Initialize router
router = Router()

# Your messages
messages = [
    {"role": "user", "content": "What's the capital of France?"}
]

# Get optimal model for this query
optimal_model = router.route("gpt-5", messages)
print(f"Using {optimal_model} instead of gpt-5")  # "Using gpt-5-nano instead of gpt-5"

# Use with your existing OpenAI code
import openai
response = openai.chat.completions.create(
    model=optimal_model,
    messages=messages
)

How It Works

The router analyzes your messages for complexity indicators:

  • Length and structure
  • Code blocks
  • Data processing requirements
  • Reasoning complexity
  • Output format requirements

Simple queries (complexity < 0.3) get routed to cheaper models.

Supported Model Mappings (September 2025)

Original Model Simple Task Routes To Original Cost Optimized Cost Savings
gpt-5 gpt-5-nano $1.25/$10 $0.05/$0.40 96%
gpt-5-turbo gpt-5-nano $0.60/$2.40 $0.05/$0.40 92%
claude-opus-4.1 claude-3-haiku $15/$75 $0.25/$1.25 98%
claude-sonnet-4 claude-3-haiku $3/$15 $0.25/$1.25 92%
gemini-2.5-pro gemini-2.5-flash-lite $1.25/$5 $0.10/$0.40 92%
grok-4 grok-3-mini $3/$15 $1/$3 67%

Costs shown as input/output per million tokens

Examples

# Example 1: Simple extraction (routes to nano/mini model)
messages = [{"role": "user", "content": "Extract the date: Meeting on Jan 15, 2025"}]
model = router.route("gpt-5", messages)  # Returns "gpt-5-nano"

# Example 2: Complex reasoning (keeps original model)  
messages = [{"role": "user", "content": "Analyze this code for security vulnerabilities and suggest improvements: [500 lines of code]"}]
model = router.route("gpt-5", messages)  # Returns "gpt-5"

# Example 3: Check complexity score
complexity = router.analyze_complexity(messages)
print(f"Complexity: {complexity}")  # 0.1 for simple, 0.9 for complex

Testing

Run the test suite to verify functionality:

# Install development dependencies
pip install -r requirements.txt

# Run tests
python -m pytest tests/

# Or run specific test
python tests/test_router.py

The test suite includes:

  • Simple query routing validation
  • Complex query preservation tests
  • Complexity analysis verification
  • Model mapping accuracy checks

Development

# Clone the repository
git clone https://github.com/apicrusher/apicrusher-lite.git
cd apicrusher-lite

# Install in development mode
pip install -e .

# Run tests
python -m pytest tests/

Limitations

This is the basic open-source router. It does NOT include:

  • ❌ Real-time model pricing updates
  • ❌ Response caching
  • ❌ Cross-provider routing (GPT→Claude)
  • ❌ Usage analytics
  • ❌ Context compression
  • ❌ Automatic fallback for deprecated models

Want 73-99% Cost Savings?

This lite version provides basic routing within the same provider.

For enterprise features including:

  • ✅ Real-time optimization rules updated daily
  • ✅ Intelligent caching (30% hit rate)
  • ✅ Cross-provider routing (route GPT-5 queries to Claude Haiku)
  • ✅ Analytics dashboard with ROI tracking
  • ✅ Context compression (77% token reduction)
  • ✅ Model deprecation handling

Check out APICrusher Pro - from $99/month with a 7-day free trial.

Basic Router Implementation

class Router:
    def __init__(self):
        self.model_map = {
            "gpt-5": "gpt-5-nano",
            "gpt-5-turbo": "gpt-5-nano", 
            "gpt-4": "gpt-4o-mini",
            "claude-opus-4.1-20250805": "claude-3-haiku-20240307",
            "claude-sonnet-4-20250222": "claude-3-haiku-20240307",
            # ... more mappings
        }
    
    def analyze_complexity(self, messages):
        # Basic complexity analysis
        text = str(messages)
        complexity = 0.1
        
        if len(text) > 500: complexity += 0.3
        if "```" in text: complexity += 0.3  # Has code
        if any(word in text.lower() for word in ['analyze', 'explain', 'complex']):
            complexity += 0.3
            
        return min(complexity, 1.0)
    
    def route(self, model, messages):
        complexity = self.analyze_complexity(messages)
        
        if complexity < 0.3 and model in self.model_map:
            return self.model_map[model]
        
        return model

Contributing

We welcome contributions! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure tests pass (python -m pytest tests/)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

MIT License - See LICENSE for details.

Support

Disclaimer

This tool is provided as-is. Always test with your specific use cases. Some complex queries incorrectly routed to simple models may produce lower quality results.


Built by developers who were spending $8k/month on uppercase conversions. We learned our lesson.

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

apicrusher_lite-1.0.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apicrusher_lite-1.0.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file apicrusher_lite-1.0.0.tar.gz.

File metadata

  • Download URL: apicrusher_lite-1.0.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for apicrusher_lite-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aecc4ca8c6a8962eb25d567d77fb6de3a067bbe018ee686b78c104dbdc34bbd4
MD5 3065529f0010275b3fe119f743346217
BLAKE2b-256 1cccb9b03d2623c601bffbe0ff46aca821bef0d937da60e3a325b8ce1dfff737

See more details on using hashes here.

File details

Details for the file apicrusher_lite-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for apicrusher_lite-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 16e895c29139e68d8eda23e4f380866d6394eb91106f067d9a2955ed4a0b5730
MD5 9223e1e126feaf77f433967568fa67fd
BLAKE2b-256 51d158bbbca1084b457fd1d7ef2352e4dedf2829599f5613ea677f01688c1dc0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page