Tips > Data, APIs & Webhooks

Use Conditional Execution to Skip Expensive Calls When Data Has Not Changed

Before calling an expensive API (LLM, image processing, translation), check if the input data has actually changed since the last run.

TipIntermediate2 min read

Before calling an expensive API (LLM, image processing, translation), check if the input data has actually changed since the last run. Store a hash of the input alongside the previous result. If the hash matches, return the cached result without calling the API.

Real-world example: A workflow generates AI product descriptions whenever product data is updated. But the CRM sends webhook events for every field change -- including irrelevant fields like last_viewed_at. Only regenerate descriptions when the relevant fields change.

// Code node: "Check If Description Needs Regeneration"
const crypto = require('crypto');

const product = $input.first().json;

// Only hash the fields that affect the description
const relevantData = {
  name: product.name,
  category: product.category,
  features: product.features,
  price: product.price
};

const currentHash = crypto
  .createHash('md5')
  .update(JSON.stringify(relevantData))
  .digest('hex');

// previousHash comes from a DB/Sheet lookup node upstream
const previousHash = $input.first().json._previous_hash || null;

return [{
  json: {
    ...product,
    currentHash,
    needsRegeneration: currentHash !== previousHash
  }
}];
```text
Then branch with an IF node:

```yaml
IF node condition:
  {{ $json.needsRegeneration }} equals true

True branch:  [OpenAI: Generate Description] --> [Save to DB with new hash]
False branch: [Return Cached Description] --> [Done]
```text
```yaml
Cost impact:
  Product catalog: 1000 products
  CRM events/day: ~5000 (most are irrelevant field changes)
  Actual description-relevant changes: ~50/day

  Without check: 5000 OpenAI calls/day x $0.03 = $150/day
  With check:    50 OpenAI calls/day   x $0.03 = $1.50/day
  Savings: 99%
```text
This is especially impactful for LLM calls, where each invocation costs 10-100x more than a simple database lookup.

**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)

Want this running in your stack?

I build production n8n and Cloudflare automation for teams — the same engineering behind HarperFlow. Fixed-price, escrow-protected, US-based.