promptdojo_

Status codes and error handling — what AI's API calls do when the wire blinks — step 7 of 9

AI wrote a retry helper that re-tries on any non-2xx status. That's wrong — 4xx errors mean the request itself is broken, and retrying with the same payload just hammers the API. Only 5xx errors are retry-worthy.

Fix the condition on line 4 so the function only retries when the status is in the 5xx family. For 4xx, it should return immediately with the string "client error".

The helper makes up to 3 attempts. With three 4xx responses queued, expected output is:

client error

(one attempt, no retries)

The break is on lines 4, 5 — but read the whole snippet first.

full-screen editor opens — close anytime to keep reading.