# How we get each inbound lead to the right rep

Every 15 minutes, our Machine agent scores fresh HubSpot leads, gives each one an owner by territory, segment or round-robin, pings that rep in Slack, and passes anything unclear to a person.

Canonical page: https://themachine.myapps.ai/use-cases/lead-routing

Someone fills in your demo form. Right now they are curious, maybe even
comparing you with a rival in the next tab. An hour from now they have moved
on. Speed is most of the game with inbound, yet in many teams the lead just
waits: for somebody to open the queue, recall which region belongs to whom,
and work out who is next in the rotation. Asking a person to do that well,
every quarter hour, from nine to six, is asking a lot.

So we gave the job to an agent on Machine. Four times an hour it pulls the
new leads out of HubSpot, picks an owner for each, tells that rep in Slack,
and sets aside any lead it can't place with confidence so a human can decide.
Those are its only moves: it gives leads an owner and sends messages. Deleting
or merging records is off the table.

- **Team:** Machine
- **Runs on:** A schedule that fires every 15 minutes
- **Connected systems:** HubSpot · Slack
- **Mode:** New session per run · sets owners and sends pings, no deletes or merges

## The problem

Our routing logic is spread across three layers. Regions map to territory
owners. Certain company sizes and industries go to specialists. Anything
left over drops into a round-robin pool. Whoever works the queue has to keep
all three straight, apply them without slips, and still move quickly enough
that the prospect hasn't gone cold by the time the rep hears about it.

Most teams fall back on a fixed rule chain built inside the CRM. It holds up
until real life happens: a territory is carved in two, a rep takes parental
leave, a lead straddles two segments. Then leads quietly pile up, land on
whatever default owner the builder picked years ago, or get a reply days late
when somebody finally spots them. None of that is quick. Worse, the leads that
genuinely need a person's call are the ones nobody hears about.

## What we built

A schedule wakes an agent on Machine every 15 minutes. Each run gets its own
fresh cloud computer with limited access to HubSpot. The agent collects every
inbound lead that hasn't been routed yet and scores how keen it looks. Then it
checks the territory rules and the segment rules. If neither claims the lead,
it goes to whoever in the round-robin pool has had the fewest leads this week.

Once it has an owner, the agent sets that owner in HubSpot, drops a note in
the rep's Slack channel, and highlights strong-intent leads so they get called
first. When it can't be sure (the territory field is blank, no segment fits,
or two reps are dead level) the lead goes to a separate Slack channel where a
person assigns it. No lead is ever deleted or merged.

## How it works

### Wake up every 15 minutes

A **cron trigger** is just a timer. Each time it fires, Machine starts a new
**session**: a clean cloud computer, or sandbox, that exists only for that run
and keeps nothing on disk. Instead of a local list, the agent leans on HubSpot
itself. A routing-status field on each lead says "done", so the following run
skips anything already handled and only sees leads that are truly new.

### Hand it the rulebook

Who owns which region, which specialist covers which segment, and who sits in
the rotation are kept as the agent's **skills** and **memory**: a playbook it
carries into every run, rather than logic hidden in a CRM flow. Redraw a
territory or add a new rep to the pool, edit the playbook, and the next run
already follows the updated rules. Nothing in HubSpot needs rebuilding.

### Plug in HubSpot and Slack

The agent reaches your tools through a **connector**, a narrow connection whose
keys stay on our servers, so the model never handles a raw token. With it, the
agent can:

- **Look up fresh leads in HubSpot.** It sees stage, region, headcount,
industry, job title and how engaged the person has been.
- **Record an owner and a "routed" flag in HubSpot.** Those two fields are all
it ever writes. It cannot remove or combine records.
- **Message Slack.** The rep gets an assignment note, hot leads get their own
marker, and anything unplaceable gets posted to an escalation channel.

### Draw the lines

Routing follows the written rules and nothing else. Under no circumstances will
the agent delete a lead or merge two together. When a lead is unclear or falls
outside every rule, it does not pick someone at random; it hands the lead to
a person. Credentials are stored encrypted in the **Secrets Manager** and only
handed to the agents you grant them to.

### Route, ping, escalate

Put together, each quarter hour brings one sweep of the queue. Every new lead
reaches the correct rep along with a short note on why it went to them. Hot
ones are flagged so they jump the line. The truly unclear ones sit in their
own channel until a person assigns them. A lead can go one cycle at most
without an owner or someone knowing the reason.

> **The pattern**
> A **cron** every 15 minutes starts a clean session that reaches HubSpot through
> a limited **connector** and talks to Slack through a **channel**. Territory,
> segment and rotation rules are the agent's **skills** and **memory**. It
> routes and pings strictly by those rules, and any lead it isn't sure about
> goes to a person.

## Guardrails

Because this agent changes CRM records, we kept what it may write very small
and made its route to a human obvious:

- **No deleting, no merging.** It may fill in a lead's owner and the routing
  flag. Removing a lead, combining duplicates or editing any other field is
  outside its reach, however sure the rules make it feel.
- **Unclear goes to a person.** If no territory fits, no segment fits and the
  round-robin can't break a tie, the lead is posted in a dedicated Slack
  channel for someone to assign. The agent never makes up a default owner.
- **Locked-down keys.** The HubSpot key is stored encrypted in the **Secrets
  Manager** and only handed to the agents you grant it to, at the moment the
  sandbox needs it.
- **Rules live in the repo.** Territories, segments, the rotation and the
  intent-scoring criteria are versioned files. Changing them means a reviewed
  **change request**, not clicking around a CRM workflow editor.

## The outcome

- **Every 15 min:** A fresh pass over new leads, all day long
- **0:** Leads the agent has deleted or merged, ever
- **3-way routing:** Territory, then segment, then round-robin, in a single run

Leads no longer sit around waiting to be noticed. Within 15 minutes each one
is with the right rep, with a line explaining why and a flag if it deserves a
call first. The agent does the sorting and the pinging. Any lead it can't
place with confidence still belongs to a human.
