I built a pipeline that reads public posts from Reddit and X and sends each one through three model stages: triage, qualify and draft. Every one of those calls runs as a headless Claude Code session on my subscription, about 1,000 tokens a call, and the process that starts the session deletes the Anthropic key variables from the environment it hands over. The metered API is still in the code. To reach it, an operator sets a boolean in the config file, passes two flags on the command line, exports a key under a variable only this project reads, and types yes at a dollar estimate.
I built it that way because the rule had already been tried. My standing instructions to coding agents say "NEVER make a paid API call (Anthropic or any metered service) without first giving a dollar estimate and getting an explicit yes for that specific run." That line was added after an agent, working in a project with no key configured, searched other projects' environment files, copied an Anthropic key it found there, and ran paid pipelines on it while the flat-rate plan sat unused in the same session. An instruction is something an agent reads and weighs against its task. A variable that is absent from its process is not there to weigh. So the default runner has no key to bill, and the API runner is built only when four separate opt-ins and a typed word all agree.
This is about the pipeline's model runner: the fenced default path, the gate in front of the API, the cost log and a batch mode.
source: the pipeline repo named in outline 38: llm.py lines 22-47 (child_env), 61-103 (positive_budget, api_authorized), 231-270 (CLI runner argv and cost note), 281-324 (ApiRunner, reservation, x-api-key); __main__.py lines 78-95 (make_runner), 138-150 and 175-177 (worker), 368-375 (estimate and typed yes); README.md lines 90-92; config.json line 20 · apiTwoPaths.data.ts
The subprocess environment
Every subprocess the package starts gets its environment from one function. It copies the parent's environment and drops a fixed list of names. This is a reduction of it, with the project key renamed, run as shown:
SECRET_ENV_VARS = ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "PROJECT_ANTHROPIC_API_KEY")def child_env(source, drop=()): blocked = set(SECRET_ENV_VARS) | set(drop) return {k: v for k, v in source.items() if k not in blocked}shell = {"PATH": "/usr/bin", "HOME": "/Users/me", "ANTHROPIC_API_KEY": "sk-ant-placeholder", "PROJECT_ANTHROPIC_API_KEY": "sk-ant-placeholder", "ANTHROPIC_BASE_URL": "https://proxy.example"}print(sorted(child_env(shell)))['ANTHROPIC_BASE_URL', 'HOME', 'PATH']The first two are the variables through which Claude Code authenticates with a key or token instead of the logged-in plan. The third is this project's own key, listed so that a key exported for a deliberate API run can't reach a subscription session started from the same shell. The CLI runner also drops two nested-session markers, so the claude it starts doesn't think another Claude Code session is driving it. ANTHROPIC_BASE_URL passes through on purpose: the comment above the list calls it a routing override, not a credential, and the CLI needs it when one is configured.
The same function builds the environment for the claude CLI, the browser bridge that runs the searches and the background worker. The worker starts a collection and assessment cycle 30 minutes after the last one finishes, and its comment says it must not inherit an Anthropic credential.
Inside the subprocess, the runner passes --tools "", an empty MCP server config with --strict-mcp-config, --setting-sources "" so no settings file is loaded, --no-session-persistence, its own system prompt with the dynamic sections excluded, and a JSON schema for the reply. It has no shell to run grep in, and a grep through other projects' environment files is how the key in the incident was found.
Four opt-ins and a typed yes
The README states the API path in one sentence: it "runs only when api.enabled is true in config.json, --api and --budget-usd are passed on that run, PROJECT_ANTHROPIC_API_KEY is set in that shell, and you type yes at the estimate." (The source names the variable after the project; it is renamed here.) The factory that builds runners checks the first two together and refuses with a message that names both config steps:
API runner is off. Set api.enabled to true in config.json AND pass --api with --budget-usd for this run.Two of the conditions were tightened after bugs. api.enabled must be the boolean true, checked with is True, because the string "false" used to open the gate (a non-empty string is truthy in Python). The config loader now refuses anything but a boolean there. The budget must be a finite number above zero: nan passed the old <= 0 check and then made every later comparison false, inf removed the bound, and booleans are refused because Python counts them as integers. The key is read from the project's variable and nowhere else; with it unset, the runner raises "Set PROJECT_ANTHROPIC_API_KEY in this shell for the API runner; no other secret store is read." The API runner's constructor also refuses unless it is passed authorized=True, and the factory is the only caller that passes it.
The four live in three places. One is a file, two are on one command line, and one is in the environment of one shell. Each can be set by mistake: a committed config edit, a flag recalled from shell history, a key left exported. The agent that works on this pipeline is told, in the first invariant of its loop file, "Never pass --api, never set api.enabled, never set PROJECT_ANTHROPIC_API_KEY, never read any .env, rc file, or keychain. If a step seems to need the API, stop and write the ask into the report instead." If an agent ignores that, the config file, the key and the prompt are still unset.
The prompt comes after the estimate check. The assess command prints the plan and an estimate, and if the estimate is over --budget-usd it raises before asking anything ("raise the budget or lower --limit"). Otherwise it asks "Spend up to $X on the Anthropic API for N calls? Type yes to continue:" with the budget and call count filled in. The answer is stripped and lowercased and compared to yes. The tests type no, n, an empty line, Yes please and y, and each one prints "Cancelled; nothing was sent." YES with spaces proceeds.
A review by a different model asked for the gate to be tested as a matrix instead of scattered examples, so one test tries every combination: seven values for api.enabled (True, False, "true", "false", 1, None and a missing block), --api on or off, six budgets (1.0, 0.0, -1.0, nan, inf, -inf) and the key present or empty. That's 7 × 2 × 6 × 2 = 168 combinations. The test asserts that exactly one builds the API runner, enabled=True --api=True budget=1.0 key=True, and that every other one either stays on the CLI runner or raises. The gate file has 11 tests, and none makes a network call:
Ran 11 tests in 0.088sOKsource: the pipeline repo named in outline 38: tests/test_gate_matrix.py lines 20, 22, 35-51 (ENABLED, BUDGETS, the matrix test); each cell’s outcome from running that loop over make_runner, __main__.py lines 78-95, with refusals from llm.py lines 61-74, 94-103, 296-298 · gateMatrix.data.ts
Once the API runner exists, each call reserves its worst-case cost before it is sent. That is the full 2,048-token output allowance plus the input, with the input counted at 3 characters a token (low on purpose) and 512 tokens added for the message envelope. If spend so far plus the reservation would pass the budget, the call is refused. After the reply the real usage is priced and added to the running spend. (The first version reserved 800 output tokens, and the request allowed 2,048. A long reply could have gone past the budget.)
What the number means
The Claude Code CLI reports a total_cost_usd with each JSON reply even on a subscription. The runner stores it with every assessment row, next to the input and output tokens, and attaches a note: "list-price equivalent reported by Claude Code; billed to the plan, not per call." The spend report groups those rows by runner, so the subscription calls, the batch answers and any API calls are summed separately.
The CLI number is a shadow price: what the same calls would have cost on the API at list price. If someone proposes moving a stage to the API, the log already holds what that stage's past work would have cost. For the CLI runner, assess --dry-run prints the call count and estimated tokens (the code assumes 900 in and 200 out for triage, 1,100 and 350 for qualify, 1,000 and 300 for draft) and leaves the dollar field empty, because the plan has no per-call price.
Batch mode
The batch command doesn't call a model at all. It writes each stage's pending prompts to data/batches/<stage>-<version>.json with the stage's system prompt, its JSON schema, the items and a line of instructions. A Claude Code session hands the file to a subagent (or a person answers it), and ingest reads the answers back. Each answer is validated against the schema before it is saved, and the whole file is refused if the stage's prompt has changed since the batch was written: "Batch was built for prompt version X; current is Y. Regenerate it." It adds no cost, since the session doing the answering is one I'm already running.
It suits a backlog, or a stage re-run after a prompt change. It can't serve anything that needs an answer in the next cycle, because nothing happens until a session picks the file up. With the runner set to batch, the background worker skips its automatic assessment pass.
The background worker can't take the API path at all: it asks for a runner without --api, and a config that names the API runner without that flag is refused. The fence holds against the session inside the subprocess.
What to do with your own pipeline
If your code calls a model through a CLI subscription, build the child's environment without any variable that can bill a key, your own project's included, and pass no tools. Log the CLI's reported cost per call as a list-price equivalent. Put the API behind conditions kept in a file, on the command line and in the shell, plus a confirmation a person types, and test every combination. In this pipeline that test has 168 cells, and one of them builds the API runner.