When a validation or preprocessing node fails or produces no results, downstream nodes should not execute (and waste API calls).
When a validation or preprocessing node fails or produces no results, downstream nodes should not execute (and waste API calls). Use n8n expressions to check the previous node's execution status and short-circuit the workflow.
Real-world example: A workflow validates incoming data, enriches it via an API, then sends it to an LLM. If validation fails, the enrichment and LLM calls should not run.
// Code node: "Check Upstream Status"
// Place this before any expensive API call
const prevStatus = $('Validate Data').isExecuted;
const validationResult = $('Validate Data').first().json;
if (!prevStatus || validationResult.valid === false) {
// Return empty to stop this branch, or return an error object
return [{
json: {
skipped: true,
reason: 'Upstream validation failed',
errors: validationResult.errors || ['Unknown validation error'],
apiCallsSaved: 2 // enrichment + LLM
}
}];
}
// Validation passed -- continue with the data
return [{
json: {
skipped: false,
data: validationResult.data
}
}];
```text
A simpler approach using the IF node with execution expressions:
```yaml
IF node configuration:
Condition: Expression
Value 1: {{ $('Validate Data').first().json.valid }}
Operation: equals
Value 2: true
True branch: --> [Enrich via API] --> [Send to LLM]
False branch: --> [Log Failure] --> [End]
```text
You can also use `$prevNode` for simpler chains:
```javascript
// Expression in an IF node
// Check if the previous node produced any output items
{{ $prevNode.first() !== undefined && $prevNode.first().json.error === undefined }}
```text
```yaml
Cost impact example:
Workflow: Validate --> Enrich ($0.01) --> LLM ($0.03)
Invalid input rate: 30%
Daily executions: 1000
Without status check: 1000 x ($0.01 + $0.03) = $40/day
With status check: 700 x ($0.01 + $0.03) + 300 x $0.00 = $28/day
Savings: $12/day = $360/month
```text
This pattern ensures you only spend money on API calls that will actually produce useful results.
**Related:** [Use Structured Output (JSON Mode) for Parseable Responses](../ai-and-llm-integration/01-use-structured-output-json-mode-for-parseable-responses.md) | [Configure Payload Size and Binary Data Mode for Large Files](../performance-and-large-files/01-configure-payload-size-and-binary-data-mode-for-large-files.md)
I build production n8n and Cloudflare automation for teams — the same engineering behind HarperFlow. Fixed-price, escrow-protected, US-based.