status. When it’s not completed, you need to know what went wrong and respond programmatically. The flow is:
- Check
statusto detect failure states - Read
failure_reasonto get the raw error description - Set up
error_code_mappingto map failures to your own error codes - Respond in code to branch your logic based on the error code
Step 1: Check status
Every run transitions through these states:
Terminal states:
completed, failed, terminated, timed_out, canceled
You can detect failures in two ways:
- by polling
get_run - by checking the webhook payload
status field.
status field is at the top level of both responses.
For tasks
Pollget_run until the status is terminal:
For agents
Same polling pattern works for agent runs:failed vs terminated: A failed run hit infrastructure problems, so retry might work. A terminated run means the AI recognized the goal is unachievable with current conditions. Retrying without changes (new credentials, different URL) will produce the same result.
Step 2: Read failure_reason
When a run fails or terminates, the failure_reason field contains a description of what went wrong. This is a free-text string, useful for logging but hard to branch on programmatically.
The field is available in both the polling response and webhook payload:
For tasks
For agents
Step 3: Use error_code_mapping
failure_reason contains an AI-generated description of what went wrong. Define custom error codes to get consistent, actionable error messages.
When the run fails, Skyvern evaluates your natural language error descriptions against the page state and returns the matching code.
How it works: The error_code_mapping values are LLM-evaluated descriptions, so you don’t need exact string matches. For example, "The login credentials are incorrect" will match pages showing “Invalid password”, “Wrong username”, “Authentication failed”, etc.
In tasks
Passerror_code_mapping as a parameter to run_task:
In agents
Adderror_code_mapping to individual blocks (navigation, task, validation):
The JSON examples below include comments (
//) for clarity. Remove comments before using in actual agent definitions; JSON does not support comments.Where the error code appears
When a mapped error occurs, your code appears inoutput.error. This field is available in both polling responses and webhook payloads:
output.error (your code) and failure_reason (raw text) are present. Use output.error for branching, failure_reason for logging.
Quick reference: Where error codes appear
Step 4: Respond in code
Now you can write clean switch/match logic:Validation blocks as assertions
Validation blocks are assertions that check conditions at critical points, like unit test assertions. If validation fails, the agent terminates immediately with your error code instead of continuing and failing later with a confusing error. Use validation blocks after steps where you need to confirm success before proceeding:
If
verify_login sees a login error, the agent terminates with output.error = "login_failed". Your Step 4 code handles it the same way as any other error code.
Common error patterns
Next steps
Reliability Tips
Write prompts that fail less often
Webhooks
Get notified when runs complete or fail

