An agent task with no time limit can run until it burns the budget or the day: a loop that almost works, a tool that almost responds. CrewAI agents carry a max_execution_time in seconds per task, alongside max_iter and max_rpm, so time is one of the three spend controls you set before a long run. Pick the limit from the task, not from hope: a task that normally takes two minutes gets a ten-minute limit, not an hour. When the limit hits, the task should end with what it has and say so, not silently pretend it finished.
What you need
- An agent task that sometimes runs long
- A normal run time measured from the log, if you have one
- A time limit to enforce, for example ten minutes
Step 1: Measure a normal run first
Read the log for a normal run of the task and note how long it took. A limit set without a measurement is a guess.
Step 2: Set the wall-clock limit above normal, not far above
Set max_execution_time to a small multiple of the normal time, such as five times, so a genuine slowdown has room but a loop does not. A limit of an hour on a two-minute task is no limit.
Step 3: Run a task that would overrun and check the ending
Force a slow run, for example with a tool that delays, and confirm the task ends at the limit with a partial result and a message that it stopped on time, not a silent success.
Worked example: synthetic time limit
The table below is a synthetic example. It shows the expected result of the time limit; no agent was run.
| Check | Expected | Result |
|---|---|---|
| Normal run takes 2 minutes | Yes | Pass |
| Limit set to 10 minutes | Yes | Pass |
| Slow run ends at the limit | Yes | Pass |
| Partial result is marked partial | Yes | Pass |
Expected result (UNTESTED). normal runs finish in two minutes, and a slow run ends at the ten-minute limit with a marked partial result. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- A time limit ends the task, it does not fix the slowness that caused it.
- A limit too close to normal time kills legitimate slow runs.
- A partial result must be marked partial, or it will be used as if it were complete.
- Time limits, iteration caps and rate limits are three separate controls; set all three.
