Resource optimization

Not every request needs GPT-4. The resource optimization pattern analyzes task complexity and routes simple tasks to cheaper/faster models while reserving expensive models for complex work. This can cut costs by 60-80% with minimal quality loss.

Tiered model routing

In practice, 60-80% of requests are simple enough for a fast, cheap model. If those requests are 10x cheaper to process, your overall cost drops dramatically with minimal quality impact on the easy tasks.

patterns/16_resource_optimization.py
python
class ResourceOptimizer:
    def analyze_complexity(self, task):
        """Score task complexity 1-10."""
        score = 0
        word_count = len(task.split())
        score += 3 if word_count > 50 else 2 if word_count > 20 else 1

        complex_keywords = [
            "analyze", "comprehensive", "compare", "evaluate"
        ]
        for keyword in complex_keywords:
            if keyword.lower() in task.lower():
                score += 2

        return min(10, score)

    def choose_strategy(self, task, complexity):
        if complexity <= 3:
            return "simple"   # Cheap, fast model
        elif complexity <= 6:
            return "standard"  # Mid-tier model
        else:
            return "complex"   # Premium model + tools

    def process_task(self, task):
        complexity = self.analyze_complexity(task)
        strategy = self.choose_strategy(task, complexity)
        # Route to appropriate processing pipeline
        if strategy == "simple":
            return self.process_simple(task)
        elif strategy == "standard":
            return self.process_standard(task)
        else:
            return self.process_complex(task)

Complexity scoring determines which processing strategy to use.

Quiz: Quiz

Loading practice…

Matching exercise: Match resource optimization concepts

Loading practice…

Flashcards: Flashcards

Loading practice…

You can now route tasks to the right model tier based on complexity. That wraps up our knowledge and communication module.

Checkpoint: Knowledge & communication check

Loading practice…