All work

Babysitter · developer tool · Node.js · Claude Agent SDK

Babysitter, routing each coding-agent prompt to the cheapest model that can handle it

Babysitter runs on top of Claude Code and Codex and picks a model for every prompt you type: a small, cheap model for a typo, a big one for a risky change across the repo. The hard part wasn't choosing the model. It was knowing when switching models costs more than it saves, because every switch throws away the prompt cache.

Role

Built it alone, 13 commits

Runtime

Node 20, no build step

Dependencies

One: the Claude Agent SDK

Tests

28, on the routing rules

What Babysitter does

Coding agents like Claude Code and Codex let you pick one model for a whole session. But most prompts in a session are small, like "rename this" or "run the tests", and a few are big, like reworking authentication across the repo. Running everything on the biggest model overpays for the small ones. Running everything on a small one gets the big ones wrong.

Babysitter installs two extra commands, bbs-claude and bbs-codex, next to the normal ones, and leaves claude and codex untouched. Inside a Babysitter session, every prompt is classified first, a small set of rules picks a model tier, and the prompt runs on that model. Before each answer it shows which model it picked and why.

What you see before each answer

YOU ❯ Fix the typo in README
╭─ ROUTE ◆ LUNA
│ Model       gpt-5.6-luna
│ Effort      low
│ Confidence  94%
│ Clarify     3% needs-user-decision probability
│ Why         complexity=0/3
│ Router      Jev · 143 input tokens
╰──────────────────────────────────────────────
The route card from the README. Every choice comes with its reason.

Jev, a model that makes decisions instead of writing text

To classify each prompt, Babysitter calls Jev, a decision model from TypeSafe AI. Jev is different from a chat model: it doesn't write an answer. You send it some state, here the prompt plus its length in characters and lines, and a list of typed questions. It sends back one structured answer per question: a score on a scale, one option from a list, or a probability for a yes or no question.

That fits routing well. The answers are already numbers I can write rules against, so there's no text to parse and nothing to go wrong in parsing. A call is small too: the README's example used 143 input tokens to route a prompt.

The quality of the answers depends on how well the questions are written, so most of the work was in the questions. Each one says exactly what to judge and what each point on the scale means.

The five questions Babysitter asks Jev about every prompt

Complexity, scored 0 to 3

How much reasoning and implementation work the task needs, judging the work and not the length or style of the prompt. 0 is obvious and mechanical, 3 is expert-level, high-stakes work.

Risk, scored 0 to 3

How costly a wrong result would be. 2 means security, production, data, money, migrations or anything with outside effects.

Breadth, scored 0 to 2

One small edit, several edits in one component, or work across the repo or several systems.

Task type, one of six

Explaining, a mechanical edit, building something, debugging, architecture or research, or reviewing.

Needs clarification, yes or no

Whether a missing decision from you would change the right implementation or make it unsafe to start. A merely imperfect prompt isn't enough. Babysitter flags it when the probability is 72% or more.

One of the questions, as Babysitter sends it

risk: {
  type: "score",
  instructions: "How costly would an incorrect coding-agent result or action be?",
  criteria: [
    "Low impact and easy to undo.",
    "Moderate impact but straightforward to review or reverse.",
    "High impact involving security, production, data, money, migrations, or external side effects.",
    "Critical impact where an error could cause severe or hard-to-recover harm.",
  ],
},
From src/jev.js. Each point on the scale is spelled out, so the score means the same thing every time.

Calling Jev safely

Retry only when Jev is busy

On a rate-limit or overloaded reply, it waits as long as Jev asks in its Retry-After header, or backs off 250ms, then 500ms, then gives up after 3 tries. Other errors aren't retried.

Never block the session

If Jev can't be reached, the prompt runs on the middle tier instead of failing. Routing is an optimisation, not something the session depends on.

Each person uses their own key

The TypeSafe key lives in a local .env file that's never committed. The prompt text does go to TypeSafe to be classified, so nobody's prompts go through a shared account.

How it picks a model for each prompt

Jev's answers then go through Babysitter's own rules, which turn them into one of four tiers. For Claude these are Haiku, Sonnet, Opus and Fable, and for Codex the four matching GPT models.

The rules that turn scores into a tier are plain code with tests, not a model, so the same prompt always gets the same tier and the reason can be printed. I kept them deliberately cautious.

Complexity sets the starting tier

Complexity 0 goes to the smallest model, 1 to the everyday one, 2 to the strong one.

Risky or repo-wide work gets at least the strong model

A simple-looking prompt that touches payments or every file is not a job for the cheapest model.

The top tier needs two signals, not one

It only goes to the most expensive model when the task is at maximum complexity and also high-risk or repo-wide. Otherwise it's capped one tier below.

Low classifier confidence doesn't push the tier up

Being unsure is noted in the reason, but it isn't treated as a reason to spend more.

If the classifier is down, use the everyday model

The session never stops because a routing call failed. It runs on the middle tier with medium effort.

Why switching models every prompt costs more, not less

My first idea was to simply run each prompt on whichever model it was classified for. That turned out to be wrong, because of how prompt caching works.

In a long chat, every new request sends the whole conversation again. Prompt caching makes that cheap: the provider keeps the earlier part of the chat ready, and re-reading it costs a fraction of the normal price. But the cache belongs to one model. Switch from Sonnet to Haiku, and Haiku has to read the whole conversation from scratch at full price. Even changing the effort setting on the same model can reset it. So a "cheaper" model on a long chat can end up costing more than staying put.

The fix was to split one decision into two. The classifier still looks at every prompt and says which tier it would pick. A separate session policy then decides whether actually switching to that tier is worth throwing the cache away.

How I got there

Explore

First idea: run each prompt on the model it was classified for

Always the biggest
Switch every turn
Trust the score

Realise

But every switch throws away the warm prompt cache

Always the biggest
Switch every turn
Trust the score

Insight

So choosing a model and switching to it became two separate decisions

Pick, then check the switch
Keep the cache warm
Handoff, fresh session

Check every prompt, but only switch models when the switch pays for itself.

The session policy

These rules decide whether a suggested switch actually happens. Both numbers can be changed in settings.

Moving up to a stronger model happens straight away

If a prompt needs more capability, quality comes first and the cache is worth losing.

Moving down waits until the current model has done 3 turns

Without a minimum, a chat that alternates easy and hard prompts would bounce between models and keep paying to rebuild the cache.

Past about 12,000 tokens of conversation, no automatic downgrades

At that size, re-reading the whole chat on a new model costs more than the cheaper model saves.

Effort is never lowered on a warm model

Lowering effort can reset the cache too, so a model keeps its current effort unless the task needs more.

Its own /compact command

The current model writes a short handoff with the goal, decisions, files and next steps, and the next prompt starts a fresh session on whichever model fits. The next model reads a page, not the whole transcript.

The core of the decision

A trimmed version of the function that decides whether to switch, from src/claude-session-policy.js.

const isUpgrade = candidateRank > currentRank;
const protectWarmCache =
  contextTokens >= cacheLockTokens || turnsOnModel < minTurns;

if (!isUpgrade && protectWarmCache) {
  // Keep the current model, and say why on the route card.
  return { route: { ...candidate, model: current.model }, switched: false };
}

return { route: candidate, switched: true };
Upgrades always go through. Downgrades only go through when the cache is cheap to lose.

After each answer, Babysitter shows the token counts the provider reports: new input, cache reads, cache writes, output and cost. It deliberately doesn't print an estimated saving, because any number it made up could be wrong.

What happens to one prompt

  1. Ask Jev

    Jev scores the prompt for complexity, risk and breadth, names the task type, and says how likely it is that a decision from you is missing.

    src/jev.js

  2. Pick a tier

    The routing rules turn the scores into a tier and a reason. If the classifier fails, it falls back to the middle tier.

    src/policy.js

  3. Decide whether to switch

    The session policy keeps the current model if switching down would waste a warm cache, and says so on the route card.

    src/claude-session-policy.js

  4. Run it on that model

    Claude turns go through the Claude Agent SDK and Codex turns through the Codex app server, the two official ways to choose a model for every turn.

    src/claude-wrapper.js

Other engineering problems I had to solve

The normal CLIs can't change model in the middle of a session

Their hooks can't swap the model for a prompt that's already been sent. So Babysitter drives Claude through the official Agent SDK and Codex through its app server, which both accept a model on every turn, and resumes the same session ID each time so the conversation carries on.

Claude asks for permission in the middle of a turn

When Claude needs approval or asks a question, Babysitter pauses the turn, shows the request in the same terminal with allow once, always allow and deny, and passes your answer back. A denial can include a reason, so Claude adjusts instead of stopping.

Some permission modes are unsafe

Routed sessions start in the mode that allows file edits but asks before anything broader. The mode that skips all permission checks is never offered, and an unknown setting falls back to the safe default.

Slash commands a wrapper doesn't know about

An unknown command is never sent to the model as text. Babysitter says it isn't supported, and /native hands the same session to the official CLI, so you never lose the conversation.

Updating without breaking anyone's work

It checks for a new version at most once a day and only shows a notice. The update command refuses to run if you have local changes, and only fast-forwards. Replacing code automatically in the background would be a supply-chain risk.

Images in a routed session

/paste and /image attach a screenshot to the next prompt, and that prompt is still routed like any other.

Fixes I decided against

Always use the biggest model

Simple, but you pay top price for renaming a variable.

Switch to the classified model on every prompt

On a long chat, rebuilding the cache on every switch costs more than the cheaper model saves.

Ask a chat model which model to use

It would answer in text that has to be parsed, and the wording can change from one call to the next. Jev returns typed scores that rules can be written against.

Let the classifier make the final call

Jev gives scores, but the decision is plain code with tests, so it's predictable, explainable and easy to change.

Replace the claude and codex commands

Babysitter installs next to them. To stop using it, you just type claude instead of bbs-claude.