Jump to content
CodeRelayHub

← All digest entries

Stop Long Agent Sessions from Draining Credit

A coding agent starts with a small bug and ends up reading half the repository, rerunning tests, and explaining the same failed approach. Each action may look reasonable, but the session keeps spending without getting closer to a verified result. The fix is not simply shorter prompts. It is a workflow that limits repeated work and makes expensive decisions visible.

1. Define completion before the first tool call

Give the agent a bounded assignment: the behavior to change, the relevant directory, the acceptance test, and the conditions that require your input. Replace “fix authentication” with “repair expired-token handling in the API middleware, add a regression test, and stop after the targeted tests pass.”

Set an explicit exploration limit. For example, allow three investigative commands before the agent must summarize its findings. This is a workflow boundary, not a guarantee about spending, but it prevents open-ended repository tours.

2. Measure the session, not just individual requests

Record input tokens, output tokens, model, tool calls, and reported usage for each request where your client exposes them. Aggregate these under a session identifier. One modest request tells you little if an agent repeats it dozens of times.

Keep billing categories separate when the provider reports them, including cached input and reasoning usage. Do not assume that every endpoint or client reports identical fields, and do not add overlapping token categories together. Reconcile your local estimates against the usage records that determine your bill.

3. Put spending controls outside the prompt

“Be economical” is useful guidance, but it is not enforcement. Add limits in your client or orchestration layer: maximum requests, maximum elapsed time, bounded output, and a session spending ceiling where reliable cost data is available.

  • Check the remaining budget before starting another model request.
  • Require approval before exceeding a limit or expanding the task.
  • Count retries toward the same session budget.
  • Leave headroom because an in-flight request may finish after you decide to stop.

If your CLI lacks these controls, use a supervising wrapper or smaller manually approved runs. Do not describe a token estimate as a hard currency cap.

4. Shrink tool output before it enters context

Large command results can become expensive baggage when subsequent requests include them again. Ask for file names before file contents, matching lines before entire logs, and focused test failures before full test output. Exclude generated files, dependency directories, and build artifacts from searches unless they are directly relevant.

Preserve evidence, not noise. Keep the failing assertion, stack trace, command, and exit status. Save verbose output to a local artifact and let the agent inspect selected sections instead of repeatedly sending everything.

5. Checkpoint before the conversation becomes history-heavy

After a meaningful milestone, create a compact handoff containing the goal, changed files, verified findings, unresolved questions, and exact next command. Include test results and rejected approaches so the next run does not repeat them.

Then start a fresh session when the accumulated conversation no longer helps the next decision. Verify that the handoff captures important constraints before discarding history. Compaction itself can require another model request, so use it at milestones rather than after every exchange.

6. Match model capability to the next decision

Use a narrow, reproducible task to compare candidate models before standardizing a workflow. For example, evaluate Claude Haiku 4.5 and Claude Sonnet 5.5 on the same bounded maintenance task. Compare total session usage and verified outcomes, not response length alone. A failed cheap attempt followed by a full restart can erase the intended saving.

CodeRelay Hub provides one endpoint for routing different models, but keep your task contract and usage logging consistent when switching. Check client compatibility before relying on model-specific tool behavior or caching features.

7. Stop loops with evidence-based escalation

After two unsuccessful attempts at the same failure, require the agent to state what changed, what new evidence appeared, and why another attempt is justified. If there is no new evidence, stop and request a human decision instead of generating another speculative patch.

Close each session with a short result: completed changes, tests run, remaining risks, and recorded usage. That gives the next session a clean starting point instead of an expensive mystery.

Open a free CodeRelay Hub workspace.

Try it on a real task: open a workspace and spend the trial credit.

Sign up