Skip to content
This repository was archived by the owner on Jul 5, 2026. It is now read-only.

Add classifier_routing example: route nodes without an LLM call - #275

Open
jamsea wants to merge 5 commits into
mainfrom
add-classifier-routing-example
Open

Add classifier_routing example: route nodes without an LLM call#275
jamsea wants to merge 5 commits into
mainfrom
add-classifier-routing-example

Conversation

@jamsea

@jamsea jamsea commented May 29, 2026

Copy link
Copy Markdown

Summary

Adds a new example, examples/classifier_routing.py, showing how to route between Flow nodes using a classifier instead of an LLM call.

Some node decisions are trivial: a verification step that only needs a yes/no, a menu where the user picks one of a few known options. Paying for an LLM inference to make that decision adds latency and cost you don't need. This example demonstrates the idiomatic Flows pattern for making those decisions with a plain classifier (a regex here, but the classify() function is the seam to swap in a rules engine or a small binary classifier), while reserving the LLM only for inputs the classifier can't confidently handle.

How it works

The example is a tiny age-verification bot:

  • The verify node uses respond_immediately=False, so entering it triggers no LLM completion. It speaks its canned question via a tts_say pre-action and then waits for the user.
  • In a normal pipeline, the user's turn-end emits an LLMContextFrame that the LLM service consumes and responds to. That is the LLM call we want to skip.
  • ClassifierRouter is a custom FrameProcessor placed between the user context aggregator and the LLM. While in the verify node it classifies the user's answer and either:
    • routes deterministically (yes -> confirmed, no -> declined) via set_node_from_config(...) and drops the LLMContextFrame so the LLM never runs, or
    • passes the frame through to the LLM when it can't classify the input, letting the LLM improvise an answer (the follow-up / off-topic case).
  • The terminal confirmed/declined nodes also avoid the LLM: they speak a canned line and end the call with an end_conversation pre-action.

ClassifierRouter drops exactly one frame type (LLMContextFrame), and only in the deterministic branch, because that frame is precisely the LLM-run trigger. Every other frame is always pushed through.

Changes

  • New: examples/classifier_routing.py
  • examples/README.md: listed the new example under Implementation Patterns
  • Changelog fragment

Testing

  • ruff format + ruff check: clean
  • Offline smoke test: module imports, classify() returns the expected yes/no/None across cases, all three node builders produce valid NodeConfigs, and the router instantiates
  • Run it: uv run examples/classifier_routing.py (needs CARTESIA_API_KEY + GOOGLE_API_KEY), then open http://localhost:7860/client. Saying "yes"/"no" routes with no LLM call between turn-end and the reply; asking a question instead falls through to the LLM.

🤖 Generated with Claude Code

@jamsea
jamsea marked this pull request as ready for review May 29, 2026 05:45
@jamsea
jamsea requested review from Copilot and markbackman May 29, 2026 05:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new example demonstrating an idiomatic “classifier router” pattern to deterministically route between FlowManager nodes without triggering an LLM call, while still allowing an LLM fallback for unclassified inputs.

Changes:

  • New examples/classifier_routing.py example implementing a FrameProcessor that drops LLMContextFrame on deterministic routes.
  • Updated examples/README.md to list the new example under Implementation Patterns.
  • Added a Towncrier changelog fragment documenting the new example.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
examples/README.md Adds the new example to the examples index.
examples/classifier_routing.py New runnable example showing classifier-based node routing that can skip LLM inference.
changelog/275.added.md Changelog entry for the new example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/classifier_routing.py Outdated
Comment thread examples/classifier_routing.py Outdated
jamsea and others added 2 commits May 29, 2026 13:51
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@jamsea

jamsea commented May 29, 2026

Copy link
Copy Markdown
Author

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants