An agent left running overnight can turn a small coding task into a long chain of paid decisions. It reads files, proposes changes, runs tools, interprets failures, and tries again. Even when the repository barely changes, model requests can keep accumulating. Before you leave a loop unattended, take this six-step pit stop to estimate its cost and give it a reliable stopping point.
1. Define what the overnight loop must finish
Write a completion condition that the runner can actually check. “Improve the tests” is open-ended. “Add tests for the invoice parser, run the specified test command, and stop after producing a patch” gives the agent a finish line.
Add explicit boundaries: which directories it may edit, which commands it may execute, and which decisions require human approval. A blocked dependency should produce a handoff note, not an invitation to explore the entire codebase until morning.
2. Calculate a token budget before starting
Estimate input and output separately. Input includes instructions, conversation history, file contents, and tool results sent back to the model. Output includes generated responses and tool-call arguments. Check the provider’s billing rules for any additional token categories.
Estimated cost = input tokens × input rate + output tokens × output rate. Normalize the rates to the same token unit before calculating.
For an illustrative calculation, suppose 100 requests each contain 10,000 input tokens and generate 1,000 output tokens. That totals one million input tokens and 100,000 output tokens. At hypothetical rates of $2 per million input tokens and $8 per million output tokens, the bill would be $2.80. These are example rates, not SwiftRelay prices or a model quote.
3. Measure context growth during a supervised lap
Run a short, supervised sample before leaving the agent alone. Record usage for each request rather than trusting the first request as representative. Later turns may resend a much larger conversation, including stack traces, search results, and earlier patches.
- Limit tool output to relevant excerpts.
- Exclude generated files and unrelated directories from searches.
- Replace stale investigation history with a concise working summary when the runner supports it.
- Track cached and uncached input separately if billing distinguishes them.
Summarization itself can consume tokens. Compare total usage across the sample, and keep original logs outside the prompt so shortening context does not erase the audit trail.
4. Stop retries from becoming an overnight spending loop
Separate transport retries from task retries. A temporary request failure may justify another attempt. Repeating the same failing test without changing the proposed fix usually does not.
Set explicit limits for request retries, repeated tool failures, and total agent turns. Stop when the same error signature recurs without meaningful progress. Check your client’s retry behavior: automatic retries and your own retry wrapper can stack, creating more attempts than you intended.
Do not assume an interrupted request is free. Reconcile actual usage afterward, and preserve enough request metadata to investigate unexpected charges.
5. Enforce the budget outside the agent’s prompt
“Spend no more than five dollars” is an instruction, not a spending control. Put enforcement in the runner or a gateway you control. Check the accumulated cost before each new request, cap output tokens, and stop launching work when the remaining allowance is insufficient.
Allow headroom for requests already in flight. If you run several workers, they need a shared budget rather than independent copies of the same allowance. Add a wall-clock deadline and a manual kill switch.
SwiftRelay is a practical option for routing supported agent clients through one pay-as-you-go relay with per-token billing. It works with Claude Code, Codex, Gemini CLI, and OpenAI-compatible SDKs. Sign up for SwiftRelay to use the small gift credit for a bounded trial, and verify the selected model’s current rates before your overnight run.
6. Reconcile the morning bill against useful work
Compare actual usage with your estimate, then inspect the patch, test results, and stopping reason. Count useful completed tasks alongside total spend. A low bill is not a successful run if the agent silently abandoned the assignment.
Keep the request totals, failure patterns, and budget settings with the run record. Adjust the next run from that evidence. The goal is not an agent that stays busy all night; it is an agent that finishes useful work within a boundary you chose.