JJoeven

Reference/Python

Errors, retries, and fail-closed

Return error dicts from tools, retry only timeout/parse/rate_limit, never retry unknown_city.

Tools return data, not raised exceptions that kill the loop.

Error object

{"error": "<code>", "message": "optional"}

CodeRetry?Next action
timeoutyes (capped)same tool
rate_limityes (capped)same or backoff skip
parseyes (parse budget)remind JSON only
unknown_citynofinish cannot
not_foundmaybe oncedifferent URL
unknown_toolno after 1stop
bad_argsnomodel must fix args
forbidden_pathnodifferent path

Retry skeleton

python
RETRY = {class="tok-s">"timeout", class="tok-s">"rate_limit"}

def call_with_retry(fn, n=3):
    last = None
    for _ in range(n):
        last = fn()
        if last.get(class="tok-s">"error") not in RETRY:
            return last
    return last

Fail-closed rules

  • Missing geocode → no weather
  • Missing retrieve → no answer
  • Missing approval → no mutate
  • Missing citation open → no positive finish
  • Max steps → budget / cannot-answer, not a guess

Exceptions you still raise

Schema bugs in your code (not the model): raise ValueError("shape") in parse_action. Convert to a parse observation at the loop boundary.

Note:except Exception: pass is how production agents eat disk-full errors and keep billing tokens.