Skip to content

feat: complexity-based tier selection (ComplexityClassifier interface + default heuristic) #36

Open

Description

@devops-bda

Wave 2 — High Impact

Problem

All requests are currently routed to whichever provider/model the caller specifies. Simple prompts (short, no tools) are sent to the same expensive model as complex multi-turn reasoning tasks. Routing simple prompts to cheaper model tiers (Haiku, gpt-4o-mini, Flash) is the single largest cost multiplier after caching — potentially 8–60x reduction on those requests.

Reference: docs/LLM-COST-OPTIMIZATION.md — Intelligent Model Routing section.

Proposed Design

// types.go or router.go
type ComplexityTier int

const (
    TierFast      ComplexityTier = iota // Haiku, gpt-4o-mini, Flash
    TierBalanced                         // Sonnet, gpt-4o, Pro
    TierPowerful                         // Opus, o1, Pro Deep Research
)

type ComplexityClassifier interface {
    Classify(req *Request) ComplexityTier
}

Default heuristic (middleware/complexity.go):

  • TierFast: prompt < 500 tokens AND no tools defined
  • TierPowerful: tools present OR prompt > 2000 tokens
  • TierBalanced: everything else

WithComplexityClassifier(classifier ComplexityClassifier, tierModels map[ComplexityTier]string) router option maps tiers to model names, overriding the request's model field before dispatch.

Why this is a library concern

The tier→model mapping is provider-agnostic. The heuristic default is universally applicable. Service-level code (cai-llm-router) wires concrete model names per agent; the library provides the interface and classification logic.

Acceptance Criteria

  • ComplexityClassifier interface and ComplexityTier constants defined
  • Default heuristic classifier implemented with the thresholds above
  • WithComplexityClassifier router option implemented
  • Classifier is skippable (nil = no tier routing, preserves existing behaviour)
  • Unit tests for default heuristic covering boundary conditions

Activity

  1. added
    enhancement
    on May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions