Inversion of control for agent pipelines and MCP servers
Keeping a long agent job's goal and state in SQLite, splitting perception from judgment, and moving an MCP server's reasoning into the user's own assistant host.

On this page
Give an agent a long, repetitive job in a loop and it will work reliably at first, then lose track after a context compaction or rate limit and report the job mostly done. Server-side AI features fail from the opposite direction: a single call behind an API key soon grows into a pipeline with a critic and fallback route until inference cost dominates the service.
Autonomous loops fit short exploratory tasks, and server-side inference is necessary to meter multi-tenant traffic. Both break down on durable batch jobs over thousands of items and on personal services where API costs quickly outrun the problem. Each calls for inversion of control: deterministic code owns the goal, state, and rules, while the model stays a bounded function the program calls. Behind that boundary, long runs survive crashes and rate limits, and a server can delegate reasoning to whichever assistant host you already pay for.
I used this design in two personal projects. The first is a TypeScript pipeline over SQLite that recreates the catalog of Drake’s, a menswear company, as made-to-measure designs from the fabrics and options of Proper Cloth, a custom clothier. The second is Wardrobe, a Cloudflare Worker MCP server that prepares four outfits from my closet each morning inside Claude, Codex, or ChatGPT.
Keeping the goal in SQLite
The catalog job needed durable progress across thousands of items and hundreds of fabrics, so I anchored the specification on one rule: “A long job must never depend on an agent keeping a goal in its head.” Every item lives in SQLite as a row with a status, and TypeScript owns every loop. Workers claim rows atomically with UPDATE ... RETURNING, startup resets in-flight rows after a crash, and a rate limit returns the row uncounted and pauses the run rather than failing unprocessed items.
Each item gets one bounded model call: headless Claude Code on my subscription, constrained to a JSON schema and read-only access to local images, with session persistence off. A rejected response gets one retry with the validation error verbatim, and a second failure routes the item to review.
Code handles everything outside language and vision: garment mapping excludes out-of-scope pieces upfront, a weighted heuristic builds a 30-fabric shortlist per item, and deterministic checks assign season and verify fiber, care, and construction rules. The model picks only from that shortlist and Proper Cloth’s real option tables, a closed vocabulary checked on every response. That pipeline turned 3,640 items into about 1,100 curated designs in a day, with completion defined by zero pending rows.
Splitting perception from judgment
Generating product photographs for those designs exposed the same boundary inside a single check. Asked whether a rendered shirt matched its design, a vision model was lenient, passing pictures whose cloth scale, texture or color was wrong.
Instead of tightening the prompt, I kept perception in the model and moved judgment into code. The model returns bounding boxes for a cloth patch and the chest width to establish scale. Pixel-level code then measures color palette, pattern repeat in centimeters, and fine texture inside those boxes against Proper Cloth’s swatch and garment drawing. Across roughly 1,900 checked photographs, failing any measurement rejected the picture regardless of the model’s assessment.
Moving reasoning out of the server
Wardrobe applies the same split across the network. Its first server-side architecture looked conventional: a composer model, a critic to trigger redrafts, and a fallback route behind an API key. Each part was defensible on its own, yet together they multiplied one daily request into a pipeline that outran its monthly budget within days, steep unit economics for choosing shirts. Swapping in a cheaper model would have left the multiplier intact, so I removed language-model calls from the server.
What remained were the deterministic state ledger, outfit validator, and data contracts that had already survived cutting roughly 175,000 lines from an earlier specification-built version generated by my agent platform. Built on those pieces, the Cloudflare Worker runs strictly as a fact service: storing wardrobe data, supplying the day’s weather and plans, checking rules, saving outfits, and generating photographs only on explicit request. All reasoning runs on my existing subscriptions inside Claude, Codex, or ChatGPT, whichever host is open.
Keeping three hosts consistent
Handing reasoning to external assistants works only if Claude, Codex, and ChatGPT act as one product across interactive chats and scheduled runs. Three server-side mechanisms enforce that consistency.
First, the workflow lives in one versioned procedure returned by an MCP start tool; client instructions and scheduled jobs carry only a pointer to it, and the save endpoint rejects any outfit set built from stale instructions. Second, the validator remains the sole arbiter of facts, blocking proposals that conflict with laundry status, wear history, or hard rules. Third, optimistic concurrency tokens on saves keep a background run and an interactive session from overwriting each other. When one provider hits a quota limit, a scheduled task on another host runs the same procedure unchanged against the same server.
Chris Chabot, October 2026