{"slug": "carpool-cut-llm-costs-by-batching-tasks-and-compiling-repeated-calls-into-code", "title": "Carpool – cut LLM costs by batching tasks and compiling repeated calls into code", "summary": "A tool called carpool, released as the pip-installable carpool-agents library and a Claude Code plugin, batches independent LLM tasks into a single request and re-runs only answers that fail citation checks, cutting cost per accepted answer by about 80% versus one lean Claude call per task and by 93–96% versus one Claude Code agent call per task with no regressions. In the project's tests, 24 ops tasks that cost $0.281 as 24 lean calls cost $0.047 as one carpool call (−83%), and 48 SQuAD questions that cost $2.449 as 48 agent calls cost $0.178 as two carpool calls (−93%). The savings come from paying the shared harness context once rather than per call; the author reports preflight-only compression without grouping saved just 0.2%, and that on a small local model with almost no fixed setup savings fell to 8–51% with some quality loss.", "body_md": "If your script, pipeline or agent makes a separate model call for every item, each call pays for its fixed\nsetup again: system prompt, tool schemas, instructions. carpool puts independent tasks in one request, checks\nevery answer against the source it cites, and redoes only the ones that fail. In our tests on Claude, that cut\nthe cost per accepted answer by about 80% against one lean call per task, and by 93–96% against one Claude Code\nagent call per task, with no regressions.[1](#user-content-fn-1-d7a9571b88769719809644ff7c024e0d)\n\nIn Claude Code:\n\n```\nclaude plugin marketplace add nmayp/carpool\nclaude plugin install carpool@carpool\n```\n\nOr try the library offline, no API key needed:\n\n```\npip install carpool-agents\ncarpool bench\n```\n\n`carpool bench` uses a mock model with a fixed cost per call, so it shows how carpool works rather than what it\nsaves. For real numbers run `carpool bench --backend claude-lean` (or `claude-code`, `codex`, `ollama:<model>`, …).\n\n| set | baseline | one call per task | carpool, first run | cost per accepted answer | regressions | \n|---|---|---|---|---|---|\n| 24 ops tasks | lean call | 24 calls · 24/24 · $0.281 | 1 call · 24/24 · $0.047 | −83% | 0 | \n| 48 SQuAD questions | lean call | 48 calls · 42/48 · $0.817 | 3 calls · 46/48 · $0.195 | −78% | 0 | \n| 24 ops tasks | agent call | 24 calls · 24/24 · $1.159 | 1 call · 24/24 · $0.065 | −94% | 0 | \n| 48 SQuAD questions | agent call | 48 calls · 40/48 · $2.449 | 2 calls · 44/48 · $0.178 | −93% | 0 | \n\nA *lean call* is Claude through Claude Code with no tools and a one-line system prompt (about 1,100 tokens of\nfixed setup); an *agent call* is a full Claude Code session (about 26,000). Repeat runs came out cheaper still\nbecause the provider cached the grouped prompt, so the table quotes first runs.\n\nEvery raw JSON file, the ablation, the group-size sweep and the agent experiments are in\n[docs/RESULTS.md](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md).\n\nWhat we measured:\n\n- The saving comes from sharing the request. Shortening evidence with one call per task saved 2.5% on the ops tasks and cost 5.7% more on SQuAD.\n- The bigger each call's fixed setup is compared with one task's own text, the more carpool saves. On a small local model with almost no fixed setup, savings were 8–51% and grouping cost some quality.\n- A well-behaved interactive agent (Claude Code triaging 20 tickets) showed no measurable change, because it already reads everything in one go. When agents did spawn one subagent per file, it cost 4–6× more.\n\nThe four rules:\n\n1. Run exact work as code. Lookups, counts and filters don't need a model: in the original study, 1,000 of 1,000 order lookups were correct with no model calls.\n2. Put independent reasoning tasks in one request. Every task and source gets a stable ID, and the model returns one JSON answer and source ID per task.\n3. Send compact evidence only when it's clearly safe: when the best source leads the runner-up by at least 40% (BM25) and isn't a draft. Otherwise send the full evidence straight away.\n4. Check every answer and retry only the failures. An answer that is missing, duplicated, cites another task's source or a draft, doesn't appear in its source, sits in an unrelated passage, is an ambiguous number or a name cut short (\"London\" for \"London Exhibition\") gets its own full-evidence call.\n\nEvery call costs `H + E_i`: shared harness context `H` plus that task's evidence `E_i`.\nWith `b` separate calls you pay `b·H`. Carpooled, you pay `H` once:\n\n```\nC_group = H + Σ E_i + J_b + V_b + Σ_{i∈F} C_retry,i\n```\n\n`J_b` is the cost of grouping and formatting, `V_b` the checks, and `F` the\nretried tasks. Compression on its own barely helped: when every task still\nneeded its own call, preflight-only compression saved 0.2%. The tokens are\nsaved by sharing the request.\n\n```\npip install carpool-agents\nclaude mcp add carpool -- carpool-mcp\n```\n\nIt adds four tools: `carpool_prompt`, `carpool_verify`, `carpool_run` and `carpool_lookup`, which does exact lookups with no model call. [integrations/MCP.md](https://github.com/nmayp/carpool/blob/main/integrations/MCP.md) has the config for each client.\n\nIf a call keeps seeing the same kind of input, a model can write a plain function that handles most of it. Give\n`carpool compile` your logged calls, one JSON object per line with an `input` and an `output`:\n\n```\ncarpool compile calls.jsonl --task \"map one log line to its event id\" --out compiled/solve.py\n```\n\nThe function may abstain. It is saved only if, on logged calls it didn't learn from, its answers agree with the\nlogged ones (at least 95%, with a 95% lower bound of at least 85%). In code, `Cascade(fast, slow)` tries the\nfunction first and sends everything it abstains on to the model.\n\n| input | code answers | code + model vs model alone | cost to build | break-even | \n|---|---|---|---|---|\n| log lines to event ids | 100% | 100% vs 100% | $0.13 | 18 calls | \n| banking messages to 77 intents | 5% | 78% vs 78% (refused by the gate) | $0.60 | not worth it | \n| questions over passages | 17%, half wrong | 88% vs 96% (would have been refused) | $0.79 | none | \n\nIt works on regular text and doesn't on anything that needs meaning. One run, small test sets;\n[details and caveats](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#12-compiling-logged-calls-into-code-carpool-compile). The generated file runs\nas you, so read it before you deploy it.\n\n**With carpool.** Tasks the compiled file answers skip the model; carpool groups the rest. The loop:\n\n```\ncarpool run tasks.jsonl --backend claude-code > answers.jsonl\ncarpool compile tasks.jsonl --answers answers.jsonl --task \"answer a question from its evidence\" --out compiled/solve.py\ncarpool run new-tasks.jsonl --backend claude-code --compiled compiled/solve.py\n```\n\n`carpool bench --compiled compiled/solve.py` adds a third row to the comparison. Where the code answered two thirds\nof the tasks, total tokens fell 52% against carpool alone; where it answered 22%, there was no measurable saving\n([§13](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#13-compiled-code-in-front-of-carpool)). The first compile on that data was refused by the\ngate, over a format mismatch in the log, and the write-up says what was changed afterwards.\n\nPaste the five rules in [`integrations/AGENTS.md`](https://github.com/nmayp/carpool/blob/main/integrations/AGENTS.md) into your `AGENTS.md`, `.cursorrules`, `GEMINI.md` or system prompt.\n\n```\npip install carpool-agents\n```\n\nThese backends report the provider's real usage: `claude-code`, `codex`, `gemini-cli`, `anthropic`,\n`openai:<model>` (any OpenAI-compatible endpoint: OpenAI, OpenRouter, Groq, vLLM, LM Studio via `OPENAI_BASE_URL`),\n`gemini:<model>`, `ollama:<model>`, plus `cmd:\"<any cli>\"` (estimated).\n\n```\ncarpool prompt my_tasks.jsonl | pbcopy\n```\n\nPaste into ChatGPT, Claude.ai or Gemini, then save the reply:\n\n```\ncarpool parse my_tasks.jsonl reply.txt\n```\n\nIt prints the accepted answers and lists which tasks need a retry, and why.\n\n``` python\nfrom carpool import Task, Source, run_carpool, AnthropicBackend\n\ntasks = [\n    Task(\"T01\", \"Which team maintains the fire pump at Mirefield?\", [\n        Source(\"T01-a\", \"The fire pump at Mirefield is maintained by the Halvard Controls Group.\"),\n        Source(\"T01-b\", \"The Mirefield canteen closes at 15:00 on Fridays.\"),\n    ]),\n    # ... more independent tasks\n]\n\nres = run_carpool(tasks, AnthropicBackend())   # or CommandBackend(\"codex exec -\")\nfor a in res.answers.values():\n    print(a.task_id, a.answer, a.source_id, a.route, a.fallback_reason)\nprint(res.ledger.n_calls, res.ledger.total_tokens, res.ledger.uncached_tokens)\n```\n\nExact operations with a contract check:\n\n``` python\nfrom carpool import Index\norders = Index(rows, key=\"order_id\", required=(\"status\",))  # raises if a key repeats or a status is missing\norders.get(\"W123\", \"status\"); orders.count(status=\"pending\")\n```\n\nTasks file format (`.jsonl`, one task per line; `answers` is optional and only used for scoring):\n\n```\n{\"id\": \"T01\", \"question\": \"...\", \"sources\": [{\"id\": \"T01-a\", \"text\": \"...\", \"trusted\": true}], \"answers\": [\"...\"]}\n```\n\n**Isn't this the same as a batch API?** No. Batch APIs (like Anthropic's Message Batches) run many separate\nrequests later at a discount; each request still pays its full fixed part. carpool cuts the number of requests.\nThe two stack: `--backend anthropic-batch` sends carpool's grouped requests through the Batches API.\n\n**Doesn't putting many tasks in one prompt hurt quality?** It can, so carpool checks every answer and redoes\nfailures one by one. On Claude Code we saw 0 regressions on both task sets, and more answers were exactly right\n(44–45/48 vs 40/48). On a 1.5B local model, groups of 8 caused 4 regressions against 7 improvements. Measure\non your own tasks: `bench/real.py` reports regressions for you.\n\n**Will it make my everyday Claude Code sessions cheaper?** Probably not by much. Claude Code already reads many\nitems in one go. The big savings are in code that calls a model once per item. When agents did spawn one\nsubagent per file in our tests, it cost 4–6 times more for the same result\n([details](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#8-does-the-fan-out-hook-change-what-an-agent-does)).\n\n**Doesn't prompt caching already solve this?** Partly. Caching makes the repeated fixed part cheaper but not\nfree, and it doesn't help output or uncached tokens. In our runs, uncached input plus output also fell by about 80–96%.\n\n**How many tasks per request?** On Claude Code, cost kept falling up to 48. The best size\nworks out as b* = √(H / (φ(H+E))), where φ is how much each extra task raises the redo rate; `carpool run --adaptive` learns it from the retries it sees. Small local models need\nsmall groups.\n\n**Do I need an API key?** No. `carpool prompt` and `carpool parse` work with a chat subscription, the MCP tools\n`carpool_prompt` and `carpool_verify` use the agent you're already talking to, and `--backend claude-code` uses\nyour Claude Code login.\n\n**How is this different from caveman?** caveman makes the model write fewer words; carpool makes you pay for\nfewer requests. They work on different costs, so you can use both.\n\n- All Claude numbers are from Claude Sonnet 5.5 through Claude Code, on short extractive answers, 24 and 48 tasks per set. The fair comparison is against one lean call per task (about 80%); against one full agent call per task it's 93–96%. On a small local model (Ollama, Qwen2.5-1.5B) the savings were small, because a raw API call has almost no fixed part to share, and grouping in 8s cost 4 regressions on SQuAD against 7 improvements ([results](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#7-a-small-local-model-ollama-qwen25-15b-on-4-cpu-cores) ). Codex, Gemini and OpenAI-compatible backends exist but haven't been benchmarked.\n- Prices are list prices from the CLI, not an invoice. Prompt-cache state changes the cost of a run (repeats are listed separately).\n- Zero regressions in 48 tasks bounds the true regression rate at about 6% with 95% confidence, not 0%.\n- The verifier fix was designed after inspecting the first SQuAD run, then evaluated on a fresh, disjoint holdout. Both runs are reported.\n- Groups can fail together. Stable IDs, per-task source checks and individual fallback limit the damage but don't rule it out.\n- The original idea and first results came from the working paper [*Lowering the Cost of Agent Work at a Fixed Quality Target*](https://github.com/nmayp/carpool/blob/main/docs/PAPER.md) (Codex CLI: −94.6% total tokens, 24/24). Our Claude Code numbers independently reproduce the same pattern.\n\n-  Real results on Claude Code ([docs/RESULTS.md](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md) )\n-  Results on a local Ollama model ([docs/RESULTS.md](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#7-a-small-local-model-ollama-qwen25-15b-on-4-cpu-cores) )\n-  Results on Codex, Gemini CLI and larger local models (`python bench/real.py --backend codex` ; send a PR with the JSON)\n-  Message Batches backend: `--backend anthropic-batch` for another 50% off non-urgent work\n-  `carpool stats results/<file>.json` summarises a saved benchmark\n-  MCP server ([integrations/MCP.md](https://github.com/nmayp/carpool/blob/main/integrations/MCP.md) )\n- Batching vs compact-evidence ablation (batching drives the savings)\n-  `carpool compile` : logged model calls into a plain function, behind a gate ([results](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#12-compiling-logged-calls-into-code-carpool-compile) )\n-  Learning group sizes automatically from retry rates (`carpool run --adaptive` ,[results](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#5-adaptive-group-size-learned-during-the-run) )\n- A hook that notices \"N subagents for N tiny questions\" and suggests a carpool (ships with the plugin)\n-  Measure whether the hook's note changes agent behaviour: it doesn't, because agents launch subagents in one batch before the note arrives ([results](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#8-does-the-fan-out-hook-change-what-an-agent-does) )\n-  A hook that acts before the first subagent launch, without overruling users who ask for subagents ([results](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#9-a-hook-that-acts-before-the-first-launch) )\n-  Find out why plugins with any hook on the subagent tool fanned out in 1 of 14 runs (against 6 of 8 without), even a hook that does nothing ([results](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#10-is-it-just-having-a-hook-registered) )\n\nBenchmark results from other clients and models help most; failures are as welcome as successes. See\n[CONTRIBUTING.md](https://github.com/nmayp/carpool/blob/main/CONTRIBUTING.md). Please follow the [code of conduct](https://github.com/nmayp/carpool/blob/main/CODE_OF_CONDUCT.md), and report\nsecurity problems privately ([SECURITY.md](https://github.com/nmayp/carpool/blob/main/SECURITY.md)).\n\nTo cite carpool, use the \"Cite this repository\" button (from [CITATION.cff](https://github.com/nmayp/carpool/blob/main/CITATION.cff)).\n\ncarpool builds on earlier work; the core idea of several tasks in one request is not new.\n\n- **Batch prompting.**[Cheng et al., 2023](https://arxiv.org/abs/2301.08721) ([code](https://github.com/hkunlp/batch-prompting) )\nput several samples in one prompt and reported cost falling almost in proportion to the batch size, with accuracy\ndropping on larger batches. carpool uses the same idea and adds what a production pipeline needs\naround it: an ID for every task and source, a check of every answer against the source it cites, a redo of\nonly the failed tasks, and a group size learned from the redo rate.\n- **Many questions in one prompt.** Studies such as[*Evaluating LLMs with Multiple Problems at once*](https://arxiv.org/abs/2406.10786) and[*How Accurate Are LLMs at Multi-Question Answering*](https://arxiv.org/abs/2509.21732) measure how accuracy\nchanges as questions are added. That risk is why carpool checks every answer and reports regressions instead\nof assuming grouping is free.\n- **Provider batch APIs and parallel-call libraries.** Anthropic's Message Batches and similar APIs, and\nlibraries such as[LiteLLM](https://docs.litellm.ai/docs/completion/batching) , still send one request per\ntask: they make each request cheaper or faster, not fewer. carpool can send its grouped requests through\nthe Batches API (`--backend anthropic-batch` ).\n- **Prompt caching** makes the repeated fixed part of each request cheaper but not free. Our Claude Code costs\nare the CLI's own figures, cache discounts included (see the FAQ).\n- **Prompt compression,** such as[LLMLingua](https://arxiv.org/abs/2310.05736) , shortens the input of each\nrequest. carpool's compact-evidence step is a simple version of that with a checked fallback; on its own it\nchanged cost by −2.5% to +5.7% in our ablation ([results](https://github.com/nmayp/carpool/blob/main/docs/RESULTS.md#2-squad-48-first-run-ablation-and-group-size-sweep) ).\n- **Answers with citations.** Checking that an answer is supported by the passage it cites follows work on\nattributed answers such as[ALCE](https://arxiv.org/abs/2305.14627) . carpool's checker is deliberately\nsimple and rule-based (exact text, numbers with units, names), so it costs no extra model calls.\n\nInspired by [caveman](https://github.com/JuliusBrussee/caveman) (fewer output tokens), Pomerado (callable\noperations), The Token Company (input compression) and Confident AI (agent evals). caveman reduces what the\nmodel writes; carpool reduces how many requests you pay for.\n\nMIT licensed.\n\n## Footnotes\n\n1. \nClaude Sonnet 5.5 via Claude Code at list prices; 24 fictional operations tasks and 48 public SQuAD questions, first runs. Details and every raw result: [docs/RESULTS.md](docs/RESULTS.md) §1–3 and §11.[↩](#user-content-fnref-1-d7a9571b88769719809644ff7c024e0d)", "url": "https://wpnews.pro/news/carpool-cut-llm-costs-by-batching-tasks-and-compiling-repeated-calls-into-code", "canonical_source": "https://github.com/nmayp/carpool", "published_at": "2026-11-27 13:52:18+00:00", "updated_at": "2026-11-27 14:14:01.502774+00:00", "lang": "en", "topics": ["large-language-models", "ai-agents", "ai-tools", "developer-tools"], "entities": ["carpool", "carpool-agents", "Claude", "Claude Code", "Anthropic", "SQuAD", "BM25", "exa"], "also_reported_by": [], "takeaway": "Grouping independent tasks into one request shares the fixed harness context once instead of per call, cutting cost per accepted answer 78–94% in the reported tests, with verification and full-evidence retries limited to failed answers.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/carpool-cut-llm-costs-by-batching-tasks-and-compiling-repeated-calls-into-code", "markdown": "https://wpnews.pro/news/carpool-cut-llm-costs-by-batching-tasks-and-compiling-repeated-calls-into-code.md", "text": "https://wpnews.pro/news/carpool-cut-llm-costs-by-batching-tasks-and-compiling-repeated-calls-into-code.txt", "jsonld": "https://wpnews.pro/news/carpool-cut-llm-costs-by-batching-tasks-and-compiling-repeated-calls-into-code.jsonld"}}