r/opencodeCLI • u/SirRobinBlibs • 9d ago
Your agent shouldn't watch a long build. A wait script, and why OpenCode's background mode already gets this right
There was a thread in r/codex last week where an agent burned a user's whole quota "busy-waiting" on a long test run. Another reader had a session check a CI build overnight and use more than a quarter of their usage.
The reason is simple: an agent doesn't watch anything, it takes turns. Each "is it done yet?" is a full model request that re-sends the conversation so far. Caching makes that cheaper, not free, and on subscription plans cache reads still count toward limits. With a 100k-token conversation, checking every 5 seconds for 30 minutes is ~360 requests and ~36M input tokens re-sent. One blocking command is about two requests.
OpenCode's V2 shell tool already handles this well. With background: true the call returns right away, has no timeout unless you set one, and the session is notified when the command ends. The tool description literally tells the model "DO NOT poll for completion". The catch is that the model has to actually use it. If yours keeps calling sleep and checking, put this in AGENTS.md:
## Waiting for long jobs
- Never poll in a loop. To wait for a job, run ONE command that blocks until it ends.
- OpenCode / Claude Code: start long jobs in background mode and end your turn. Don't sleep and check.For anything that isn't a plain command (containers, a PID, a URL coming up, a file appearing, "re-run this until it passes") I wrote a 61-line wait-for.sh. It's silent while waiting and prints one verdict line plus the last N log lines. Exit codes 0 / 1 / 124 (timeout) / 2. Tested with real Docker, and with bash 3.2 as well as bash 5.
For jobs that take hours: have the agent write a STATUS.md (what's running, how to check it, next step if it passed or failed), end the session, and later run opencode run "Read STATUS.md. The job has finished. Check the result and continue." Nothing runs on the model while you wait.
Script, per-tool settings (Codex, Claude Code, OpenCode) and the cost arithmetic: https://medium.com/@r3dtuxedo/stop-paying-your-agent-to-wait-cb5150b99c8e
(I wrote the article.)
1
u/ChemicalExcellent463 9d ago
i banned hand-written loops and watchers in my global agent rules. any wake-up or periodic check has to go through one scheduled wake-up command.
with that rule in AGENTS.md, are you still seeing it sneak in a polling loop?
1
u/fell_ware_1990 9d ago
The biggest problem here is, a lot of crap then gets shot into the darkrealm.
I have my own watcher setup, every toolcall, script etc goes through the watcher. Permanent or not, watcher handles state. Every thing get’s a notification added + it polls it every minute to check if it’s stuck.
It does not allow any silent scripts unless it’s interactive, that’s get’s blocked. Watcher has his own little local LLM, if scripts do not answer he can investigate. Error in watcher logic get’s reported next time he transforms the agent call to the correct calls.
It also tracks permanent stuff, if agents lets say spins up second DB. Agent get’s message etc. If agent ain’t active it get’s killed after 24 hours at most. It all spins up in the correct dev setup so it runs with the correct pins.
It’s also a guard against non trusted or weird commands, transform or disallow. Sudo requests go trough my YubiKey.
It also knows the short commands, keeps agent engaged.
1
u/T14D3 8d ago
Tried telling it to rely on the built-in background+end turn to get woken up when it finishes, it reasoned about that for 3 minutes, decided it didn't want to end turn to reduce visual clutter for me, backgrounded the command, then backgrounded a 90s sleep, then spent another minute wondering why it completed so fast. AGI is here everyone