> Agent-ready spec from Diff of the Day (2026-12-04, playbook 1). Give this file to your coding agent (Claude Code, Codex, Cursor) and ask it to build the P0 requirements first.

# Spec: Two-tier model router with cost per merged pull request

## Problem Statement
A small team pays closed-model prices for every agent task, including routine ones. Bending Spoons reports that sending most tasks to open-weight models and keeping closed models for hard cases and supervision cut cost per pull request, while pull requests per engineer rose. The team has no measure of cost per merged pull request, so it cannot tell whether routing helps.

## Goals
- Log cost and outcome for 100% of agent pull requests.
- Send at least 50% of tasks to the cheaper tier on low-risk paths.
- Escalate to the stronger tier on two failed test runs.
- Report cost per merged pull request weekly, with the method used for each cost line.

## Non-Goals
- Perfect routing: a rule on paths and test results is enough, and a learned router needs data you do not have yet.
- Replacing the closed model: it stays for hard tasks and for supervision, as in the Kilo account.
- Comparing with Bending Spoons numbers: its cost lines mix estimates and real GPU cost, so only your own before and after is fair.

## User Stories
- As a developer, I want routine tasks to run on the cheap tier so that my budget lasts longer.
- As a team lead, I want cost per merged pull request each week so that I can see if routing pays.
- Edge case: when the cheap tier passes tests but touches a path marked high risk, the stronger tier reviews the diff before merge.

## Requirements
### P0
- [ ] Cost log with task id, tier, tokens or GPU seconds, test result and merge result. Accept: every agent pull request has one row.
- [ ] Router rule: low-risk path list goes to tier 1, everything else goes to tier 2. Accept: a test with 5 sample tasks routes as expected.
- [ ] Escalation after two failed test runs. Accept: a failing sample task ends on tier 2.
### P1
- [ ] Weekly report with cost per merged pull request and merged pull requests per engineer. Accept: the report states which cost lines are estimates and which are measured.
- [ ] Supervisor step where tier 2 reads tier 1 diffs on high-risk paths. Accept: the log shows the review result.
### P2
- [ ] Try a second tier 1 model and compare. Accept: the report shows both on the same tasks.

## Success Metrics
- Cost per merged pull request, before and after routing.
- Share of tasks finished on tier 1 without escalation.
- Merged pull requests per engineer per week.

## Open Questions
- Which paths are low risk in your repo?
- Do you self-host tier 1 or rent it, and how do you count idle GPU time?
- Will more, smaller pull requests inflate the per-pull-request number, as some commenters on the Kilo post argue?

## Timeline
Day 1: cost log and router rule. Day 2: escalation, report and a first week of data.

## Agent Instructions
Suggested stack: A thin proxy or script in front of your agent tool, a CSV or small database for the log, and one open-weight model served with a standard local server.
Build order: cost log, router rule, escalation, report, supervisor step.
Safety: Keep API keys in environment variables, give tier 1 no more repo access than tier 2, and never route secrets or customer data to a model you have not approved.
Done when: a weekly report shows cost per merged pull request with the tier split for at least one full week.

## Sources
- https://blog.kilo.ai/p/bending-spoons-cut-ai-cost-per-pr
- https://x.com/fedesimio/status/2128535157896622356
- https://huggingface.co/blog/qwen4