DEV Community

Not every voice command needs an LLM: building a two-tier intent router

I'm building Liebchen, a voice assistant that runs entirely on my own machine using Ollama and LangGraph.

Early on I hit an obvious problem. Saying "lock the computer" shouldn't require a language model to think about it.

The problem

Sending every request to a local LLM is slow and wasteful on a personal machine. A command like "turn the volume up" has exactly one correct action.

Asking a model to work that out each time adds delay and a small chance it gets it wrong.

The idea: two tiers

Every request first goes through a router in ai/router.py:

  • Fast path. A table of regex patterns, each paired with a handler function. If the text matches, the handler runs immediately with no LLM involved.
  • LLM path. If nothing matches, the request goes to a stateful LangGraph agent backed by a local model, which can call tools and use memory.

How the fast path is built

Each pattern is tagged with an intent type: system commands (shutdown, restart, lock, sleep), apps, media (volume, mute, screenshot), info (time, date, battery), search, and stop.

A simplified version of the table looks like this:

_INTENT_PATTERNS = [
    ( r "<pattern for lock>" , lambda m : RouteResult ( IntentType . FAST_SYSTEM , _execute_system ( "lock" ) , handled = True ) ) ,
    ( r "<pattern for volume up>" , lambda m : RouteResult ( IntentType . FAST_MEDIA , _execute_media ( "volume_up" ) , handled = True ) ) ,
    ( r "<pattern for time>" , lambda m : RouteResult ( IntentType . FAST_INFO , _execute_info ( "time" ) , handled = True ) ) ,
]

The function route(text) walks the table and returns a RouteResult. The handled flag tells the caller whether the fast path did the job or whether to pass the text on to the LLM.

Why regex and not a classifier

For a small, fixed set of commands, patterns are predictable. The same input always produces the same action, which matters for things like shutdown or lock.

There is also nothing to train, and adding a new command is one new line in the table.

One detail I like: stopping

The router has a STOP intent. If I say stop while the assistant is mid-answer, it halts and replies that it's listening again.

A deterministic fast path is a good place for this, since it doesn't have to wait for the model to finish.

What I learned

Use the cheapest tool that is reliably correct, and keep the expensive one for what only it can do. Rules handle the commands I can list in advance, and the LLM handles everything else.

Code: github.com/snehalmarteen2005/jarvis-2.0

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.