Skip to content

Add hybrid analyzer combining heuristic and LLM judgment - #1

Merged
krugis merged 2 commits into
mainfrom
claude/llm-analyzer-scoring-enhance-dpyu36
Jul 12, 2026
Merged

Add hybrid analyzer combining heuristic and LLM judgment#1
krugis merged 2 commits into
mainfrom
claude/llm-analyzer-scoring-enhance-dpyu36

Conversation

@krugis

@krugis krugis commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

Introduces a new hybrid analyzer mode that blends deterministic heuristic signals with LLM judgment, combining the explainability and reliability of heuristic analysis with the accuracy of LLM classification on ambiguous prompts.

Key Changes

  • New HybridAnalyzer (internal/analyzer/hybrid.go):

    • Always runs the heuristic analyzer first (cheap, never fails)
    • Optionally refines results with LLM judgment when available
    • Blends complexity scores: w * llm_score + (1-w) * heuristic_score where w is configurable (default 0.5)
    • Overrides category only when heuristic had low confidence (detected general)
    • Falls back gracefully to heuristic result on any LLM error
    • Records raw LLM complexity in signals for auditability
  • LLMAnalyzer improvements:

    • Exported Classify() method (was private classify()) for use by HybridAnalyzer
    • Enhanced prompt with few-shot examples (6 worked examples spanning all categories with non-round complexity values: 0.05, 0.12, 0.22, 0.38, 0.64, 0.93) to eliminate round-number clustering and calibrate scoring across the full range
  • Configuration:

    • Added analyzer.llm.hybrid_weight config field (0..1, default 0.5)
    • Added ModeHybrid constant and validation for new mode
    • Updated config validation to require LLM model for both llm and hybrid modes
    • Added environment variable support: ROUTE42_ANALYZER_LLM_HYBRID_WEIGHT
  • Factory pattern:

    • Updated New() to instantiate HybridAnalyzer when mode is hybrid
  • Type definitions:

    • Added NameHybrid constant for analyzer identification
  • Comprehensive test suite (internal/analyzer/hybrid_test.go):

    • Tests complexity blending with LLM success
    • Tests category override when heuristic detects general
    • Tests fallback to heuristic on LLM errors
    • Tests weight edge cases (0.0 and 1.0)
    • Tests default weight handling for invalid values
    • Tests elimination of round-number clustering problem
  • Documentation:

    • Updated analyzer.md, config.md, and README.md with hybrid mode explanation
    • Documented few-shot prompting approach and its benefits
    • Clarified cost, fallback behavior, and signal recording

Notable Implementation Details

  • Hybrid mode is a strict superset of heuristic mode's reliability: any LLM failure returns the heuristic result unchanged
  • The few-shot prompt examples use non-round complexity values to prevent the LLM from clustering predictions around 0, 0.5, and 1.0
  • Heuristic signals are always present; LLM complexity is added to signals only on successful classification for auditability
  • Weight validation clamps invalid values (≤0) to default 0.5, ensuring safe operation

https://claude.ai/code/session_01AatiioR5QBGq6NPvHYbLHj

claude added 2 commits July 12, 2026 10:22
Implement the LLM Analyzer Enhancement spec with:

1. Few-shot prompting (§3.1): Replaced zero-shot classification with 6
   worked examples spread across all categories and non-round complexity
   values (0.05, 0.12, 0.22, 0.38, 0.64, 0.93). This fixes the round-number
   clustering problem where the LLM was selecting only the 3 anchor values
   (0, 0.5, 1) from the zero-shot instruction.

2. HybridAnalyzer (§3.2-3.5): New analyzer that blends heuristic signals
   with LLM judgment. The heuristic runs unconditionally (cheap, <1ms), and
   LLM output refines complexity via weighted blend and overrides category
   only when heuristic fell back to "general" (low confidence). On any LLM
   failure, returns heuristic result unchanged for reliable degradation.

3. Config & factory updates: Added analyzer.llm.hybrid_weight config
   (default 0.5), ModeHybrid constant, and factory wiring.

4. Exported LLMAnalyzer.Classify() for HybridAnalyzer to use without
   triggering LLMAnalyzer's own fallback logic.

5. Comprehensive unit tests covering blending behavior, weight extremes
   (0 and 1), LLM failures, and avoidance of round-number clustering.

6. Updated llm_ollama_test.go tolerance for the longer few-shot preamble.

This fixes all four findings from the test cycle:
- Few-shot eliminates round-number clustering (finding #3)
- Hybrid mode adds explainability back via heuristic signals (finding #4)
- LLM failure handling is now explicit in hybrid mode (finding #1)
- Few-shot with JSON format improves parse reliability (finding #2)

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AatiioR5QBGq6NPvHYbLHj
…w-shot

- docs/analyzer.md:
  - Update analyzer comparison table to mention few-shot for llm mode
  - Add hybrid analyzer section with blend formula, category override rules, signals
  - Explain how hybrid mode fixes round-number clustering and explainability issues
  - Update config example to show hybrid_weight

- docs/config.md:
  - Add 'hybrid' to analyzer.mode enum
  - Add hybrid_weight field documentation (0..1, default 0.5)
  - Update llm.model validation to note it's required for both llm and hybrid modes
  - Add ROUTE42_ANALYZER_LLM_HYBRID_WEIGHT to environment variables table

- README.md:
  - Update analyzer table to add hybrid mode
  - Update analyzer configuration example with hybrid_weight

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AatiioR5QBGq6NPvHYbLHj
@krugis
krugis merged commit c7202de into main Jul 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants