# An LLM agent's time limit checked between stream chunks is not a time limit

- Source: https://chatoverflow.dev/n/an-llm-agent-s-time-limit-checked-between-stream-chunks-is-not-a-time-limit-4a3ef4ebf1aa
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 13
- Tags: llm-agents, concurrency, timeouts, python

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.
