Should Coding Agents Write Unit Tests, or Does It Waste Tokens?
Yes. In Dosu’s replays, edits, builds, and tests shared a bucket worth about half of one agent’s attributed cost, but a test is still how an unattended agent checks its own change. Keep the test and cut the setup, guessing, and noise around it.
Yes. Have the agent run the relevant existing tests on every change, and write a new test for the behavior the task changes. An agent working without you watching decides a change is finished either from its own reading of the code or from a check that comes back pass or fail, and the check tells it something its own reading cannot. Dosu's replays did not measure what writing tests costs on its own, because the bucket that holds tests also holds edits, installs, and builds. When verification looks expensive in your session logs, look at what surrounds the test before you cut it. Installing and building before a package test can start, guessing at the right command, and chasing failures the change did not cause all show up as testing cost. Cut those and keep the check.
Tests share a bucket that took 12% of Claude Code's attributed cost and 53% of Codex's, and the logs do not say they passed
Dosu replayed 49 merged pull requests from Vercel's AI SDK with Claude Code on Opus 5.5 and Codex on GPT 6.1 Sol, then used Decant to sort each session into context, planning, code, and communicating. The code bucket holds edits, builds, tests, and their output. The bucket came to 12.1% of Claude Code's attributed cost and 52.7% of Codex's.
| Agent | Code share of attributed cost | Code share of active time | Runs with a test command |
|---|---|---|---|
| Claude Code on Opus 5.5 | 12.1% | 80.4% | 46 of 49 |
| Codex on GPT 6.1 Sol | 52.7% | 49.5% | 49 of 49 |
Three conditions limit what that table shows. Costs are attributed at pinned API rates, not billed charges. Active time is estimated from gaps between messages, capped at five minutes each. The patches were not scored for correctness, so a test command in the log shows the agent tried to verify its work, not that the suite finished or the patch passed.
Claude Code spent most of its active time in the code bucket while that bucket cost little, so a slow build or suite can dominate the clock without dominating the bill. The code share also cannot isolate tests. The study found pnpm install in 32 of Codex's 49 runs, and in that monorepo, testing one package can mean installing dependencies and building the packages it depends on first. All of that lands in the same bucket.
A check gives the agent feedback it did not write itself
"Claude stops when the work looks done," according to Anthropic's Claude Code best practices. Without something to run, "looks done" is all it has, and you become the test suite. Give it a test file, a build's exit code, a linter, or a script that diffs output against a fixture, and it can make the change, run the check, read the failure, and try again.
Removing tests to save tokens moves that work to a person. The agent still finishes, on its own judgment, and every mistake waits until someone happens to notice it in review or in production.
The same guide lists how strictly you can hold an agent to a check. In the prompt, Claude decides when it is satisfied. A /goal condition has a separate model check the conversation after every turn. A Stop hook runs your script when Claude tries to end its turn and sends it back to work if the script exits with code 2, so Claude Code enforces it until the check passes or the block limit is reached. A /code-review pass has a fresh subagent review the diff. Whichever you choose, ask for the command and its output as evidence. "All tests pass" without output is a claim, and for a session nobody watched, the output is the only record.
Setup before the test runs can cost more than the test
In a monorepo, much of a test run's cost can come before the first assertion. The agent installs dependencies, builds the workspace packages the test imports, and, when it skips one of those steps, works back from a failure that looks like a broken test to the build it missed. Dosu's replays started from fresh checkouts, so expect less of this in a warm workspace. Runbooks for AI coding agents walks through one such build requirement from Dosu's session study, and why a documented build step still left agents to connect the error to its cause.
Two other costs sit inside test runs without checking your change. A suite that runs everything for a one-file change spends wall time on tests unrelated to the edit. A flaky test sends the agent back through code it did not break. Common sources include unstable fixtures, live network calls, and shared state between tests. What makes a codebase easy for AI coding agents covers both fixes: a tiered set of checks so an edit pays only for a package-scoped test, and a repeated-run comparison that tells an intermittent failure from a regression.
Write the test command down where the agent loads it
An agent that has to work out how to run tests spends tokens reading config files and trying commands that fail. The Claude Code guide lists the exact test, typecheck, and lint commands, and how to run a single test file, among what belongs in CLAUDE.md, and calls the command that runs your check "the single most useful line" in it. Write down three things:
- The command for one test file or one package.
- The build or install step that command depends on, and the error you see when it is skipped.
- The full-suite command, labeled as the one to run before merge rather than after every edit.
The second item is the one a list of commands usually leaves out. A skipped build tends to fail as an import error that does not mention the build, so write down the error next to the step that prevents it.
Keep the file short. Claude Code loads CLAUDE.md into every session, and Anthropic's memory guide recommends staying under 200 lines. In a monorepo, put package-specific commands in that package's own instruction file. AGENTS.md vs CLAUDE.md vs .cursorrules covers how each agent picks up nested files and how to share one set of commands between them.
Ask for a test of the behavior you changed
The useful new test pins the behavior in the task. If the ticket says a client should retry once on a timeout and then raise, ask for a test that simulates two timeouts and checks for one retry and the raised error. A request for more coverage invites tests nobody needed.
Anthropic's guide gives a related warning about reviews. A reviewer asked to find problems always finds something, and if you fix every finding, "you end up with extra abstraction layers and tests for cases that can't happen." Its suggested fix is a review prompt scoped to the spec that ends with "skip style." Apply the same scoping when you ask an agent to write tests, and name the behavior and the edge cases that matter for correctness.
FAQ
Does a test command in the session log mean the patch works?
No. In Dosu's study, a logged test command showed only that the agent tried to verify its work. The log did not show that the suite finished or the patch passed, and the patches were not scored. Read the exit status and output, then check whether the test covers the behavior that changed in the patch.
Should agents write new tests or only run existing ones?
Run the relevant existing tests first. Add a test when the task changes behavior that no existing test checks, and name that behavior in the request.
Is a Stop hook better than asking the agent to run tests?
They do different jobs. A line in the prompt or CLAUDE.md is a guideline the agent follows most of the time. A Stop hook is enforced by Claude Code and blocks the end of the turn until your script passes or the block limit is reached. Use the hook for a check that must run every time, and point it at a command fast enough to run on every stop.
Why does Codex spend more of its budget on code than Claude Code?
In Dosu's replay, Codex put 52.7% of attributed cost in the code bucket against 12.1% for Claude Code, and much of that was verification and the dependency installs and builds around it. The comparison is of two tool-and-model bundles with their own instructions and habits, so it does not show that one agent tests too much.
Measure the setup cost before you cut a test
Run Decant, Dosu's open source reader for local agent session logs, on a repository where verification looks expensive:
npm install --global @dosu/decant@latest
decant ls --repo .
decant economics
decant search "pnpm install" --repo .
decant economics gives the token, cost, and time breakdowns for every session in the archive, and its code bucket still mixes edits, installs, builds, and tests. The Analytics view in Decant's web UI breaks the same numbers down by repository. To separate setup from test runs, open a session that decant search returns with decant show <id> --outline. The outline lists each message's index, size, block kinds, and tool names but leaves out tool inputs, so read a shell call with decant show <id> --offset <index> --limit 1 to see its command. The output arrives in a later message with a tool_result block, so raise --limit until you reach it. Together they tell you whether the call was an install, a build, or a test run. By default, Decant makes no outbound network calls while it reads and analyzes your logs. An optional update check, off unless you turn it on, asks the npm registry for the newest version and sends no session data. Decant does keep prompts, tool inputs, and tool output in an unencrypted archive at ~/.decant/decant.db, so treat that file under your transcript retention policy.
When the logs show agents rediscovering the same setup step, write its symptom and cause where the next session will find it. Short commands belong in CLAUDE.md or AGENTS.md. For the longer explanation, set up the Dosu MCP server with npx @dosu/cli setup, and have the agent record it with write_knowledge. Sessions on the same repository and branch can read it right away with read_knowledge, and once the branch merges, Dosu checks the note as a candidate Topic for the rest of the team. Then compare the install and build commands in comparable sessions afterward to see whether agents still rediscover the step.
