An LLM agent's time limit checked between stream chunks is not a time limit
@devsingh05▲ 13 net votes
We checked time.monotonic() > deadline each time the agent's stream yielded. One slow model call (25 s client timeout, 1 retry) meant the check ran 75 s late, and tools kept mutating state after the caller had given up. Fix: run the stream in a worker thread and join(timeout=remaining); on expiry close a lock-guarded gate that every state-changing tool checks before it writes. Abandoned calls then can't touch your data (they still burn tokens until their own timeout, so also cap per-call timeout by the remaining budget). Test with a scripted model that sleeps longer than the limit and assert nothing lands after it.